I have the following code in my share extension to get the URL that is shared.
if let item = extensionContext?.inputItems.first as? NSExtensionItem {
for (index, _) in (item.attachments?.enumerated())! {
if let itemProvider = item.attachments?[index] as? NSItemProvider {
if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil, completionHandler: { (url, error) -> Void in
if let shareURL = url as? NSURL {
// send url to server to share the link
print (shareURL.absoluteString!)
For some reason the iOS YouTube app returns false for itemProvider.hasItemConformingToTypeIdentifier("public.url")
.
And below is my Info.plist for the share extension.
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>
SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
).@count == 1
).@count == 1
</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
How can I get the URL for the YouTube video shared in my share extension?
My first recommendation is to find out what types are actually provided by the
itemProvider
:
if let item = extensionContext?.inputItems.first as? NSExtensionItem {
for (index, _) in (item.attachments?.enumerated())! {
if let itemProvider = item.attachments?[index] as? NSItemProvider {
// print out the registered type identifiers so we can see what's there
itemProvider.registeredTypeIdentifiers.forEach { print String(describing: $0) }
In this case, according to your comments, you are able to get plain text or kUTTypePlainText
and that text contains a URL so:
if let item = extensionContext?.inputItems.first as? NSExtensionItem {
for (index, _) in (item.attachments?.enumerated())! {
if let itemProvider = item.attachments?[index] as? NSItemProvider {
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypePlainText) {
itemProvider.loadItem(forTypeIdentifier: kUTTypePlainText, options: nil, completionHandler: { (string, error) -> Void in
if let string = (string as? String), let shareURL = URL(string) {
// send url to server to share the link
print (shareURL.absoluteString!)
My second recommendation is to always use the kUTType constants instead of raw strings :-)