iOS 未提供 YouTube 共享扩展 URL

iOS YouTube Share Extension Not Providing URL

我的共享扩展中有以下代码来获取共享的 URL。

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

出于某种原因,iOS YouTube 应用 returns 对于 itemProvider.hasItemConformingToTypeIdentifier("public.url") 是错误的。

下面是我的 Info.plist 共享扩展。

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

如何获取在我的共享扩展中共享的 YouTube 视频的 URL?

我的第一个建议是找出 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: [=10=]) }

在这种情况下,根据您的评论,您可以获得纯文本或 kUTTypePlainText 并且该文本包含 URL 所以:

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

我的第二个建议是始终使用 kUTType 常量而不是原始字符串:-)

Youtube 将提供一个 kUTTypePlainText。 要获得 URL,您必须从 NSSecureCoding 转换为字符串,然后使用该字符串构建 URL。

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 

          })
        }
      }
    }
  }
}