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

时间:2016-12-18 22:03:45

标签: ios share-extension apple-news

你可以帮助我支持苹果新闻分享吗,

My Share Extension 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>

如何在与苹果新闻分享某些内容的同时看到我的分享扩展?

2 个答案:

答案 0 :(得分:1)

好的,我把它排除了。您需要将扩展​​程序配置为允许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
}];

答案 1 :(得分:1)

这是我与您的魔术PLIST一起使用的粗略Swift 4版本。似乎可以在新闻和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
    }
}