Charlie Fish
Charlie Fish

Reputation: 20546

iOS YouTube Share Extension Not Providing URL

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?

Upvotes: 4

Views: 2411

Answers (2)

Albastroiu Radu
Albastroiu Radu

Reputation: 11

Youtube will provide a kUTTypePlainText. To obtain an URL you have to convert from NSSecureCoding to String and then build the URL with that string.

func getUrl() {
  if let content = extensionContext!.inputItems[0] as? NSExtensionItem {

    let contentTypePlain = kUTTypePlainText as String

    if let contents = content.attachments {
      for attachment in contents {
        if attachment.hasItemConformingToTypeIdentifier(contentTypePlain) {
          attachment.loadItem(forTypeIdentifier: contentTypePlain, options: nil, completionHandler: { data, error in
            let urlString = data as! String
            let url = URL(string: urlString)!

            // USE URL 

          })
        }
      }
    }
  }
} 

Upvotes: 1

Scott Thompson
Scott Thompson

Reputation: 23701

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 :-)

Upvotes: 4

Related Questions