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

IOS share extension how to support apple news

你能帮我解决支持苹果新闻分享的问题吗,

我的分享扩展 info.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
}];

这是我的粗略 Swift 4 版本,我将与您的神奇 PLIST 一起使用。似乎在新闻和 Safari 中都有效。

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