iOS YouTube共享扩展程序未提供URL

时间:2017-05-13 15:15:50

标签: ios swift youtube ios8-share-extension

我在共享扩展程序中有以下代码来获取共享的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应用为itemProvider.hasItemConformingToTypeIdentifier("public.url")返回false。

以下是我的共享扩展的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视频的网址?

2 个答案:

答案 0 :(得分:2)

我的第一个建议是找出实际提供的类型 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) }

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

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常量而不是原始字符串: - )

答案 1 :(得分:1)

Youtube将提供一个kUTTypePlainText。 要获取URL,必须将NSSecureCoding转换为String,然后使用该字符串构建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 

          })
        }
      }
    }
  }
}