iOS分享扩展如何支持苹果新闻



您能在支持Apple News共享的情况下帮助我,

我的共享扩展名信息。plist包含:

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsAttachmentsWithMaxCount</key>
            <integer>10</integer>
            <key>NSExtensionActivationSupportsText</key>
            <true/>
            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
            <integer>1</integer>
            <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
            <integer>10</integer>
        </dict>
    </dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
</dict>

在分享Apple News的某些内容时,我如何看到我的分享扩展?

好吧,我对此进行了整理。您需要配置扩展名,以允许public.plain-textpublic.url类型的内容。Apple News发送了一个带有两个附件的ItemProvider,首先是文章摘要的普通文本,其次是文章本身的Web URL。您必须接受和处理两者。

尝试这些扩展属性。他们使用谓词查找所需的URL类型附件(假设这是您想要的):

<key>NSExtensionActivationDictionaryVersion</key>
        <integer>2</integer>
        <key>NSExtensionActivationUsesStrictMatching</key>
        <integer>2</integer>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>NSExtensionActivationRule</key>
            <string>SUBQUERY(extensionItems, $e, (
            SUBQUERY($e.attachments, $a, ANY $a.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url").@count == 1
            )).@count == 1
            </string>
            <key>RequestsOpenAccess</key>
            <true/>
        </dict>

和沿着这些行的代码以找到适当的URL附件,假设这是您想要的位:

NSExtensionItem *inputItem = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider;
for (itemProvider in [inputItem.userInfo valueForKey:NSExtensionItemAttachmentsKey]) {
    if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *) kUTTypeURL]) {
        break;
    }
}
if (!itemProvider) {
    // Handle error here
    return;
}
[itemProvider loadItemForTypeIdentifier:(NSString *) kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) {
    // Handle the URL here
}];

这是我与魔术plist一起使用的粗糙Swift 4版本。似乎在新闻和野生动物园中起作用。

func getUrl(callback: @escaping ((URL?) -> ())) {
    guard let items = extensionContext?.inputItems,
        let item = items.first as? NSExtensionItem,
        let attachments = item.attachments else {
            callback(nil)
            return
    }
    var found = false
    for attachment in attachments {
        if let provider = attachment as? NSItemProvider {
            if provider.hasItemConformingToTypeIdentifier("public.url") {
                found = true
                provider.loadItem(forTypeIdentifier: "public.url", options: nil) { (url, error) in
                    if let shareURL = url as? URL {
                        callback(shareURL)
                    } else {
                        print("error getting url: (error)")
                        callback(nil)
                    }
                }
            }
        }
    }
    if !found {
        callback(nil)
        return
    }
}

相关内容

  • 没有找到相关文章

最新更新