IOS share extension how to support apple news

1k views Asked by At

Can you please help me in the matter of supporting apple news sharing ,

My Share Extension info.plist contains :

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

How can i see my share extension while share some thing from apple news ?

2

There are 2 answers

1
Chad Robinson On BEST ANSWER

OK I sorted this out. You need to configure your Extension to allow content for both public.plain-text and public.url types. Apple News sends an ItemProvider with two attachments, first a plain-text piece with the article summary, and second a Web URL to the article itself. You must accept and process both.

Try these extension attributes. They use a predicate to find the required URL type attachment (assuming that's what you want):

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

And code along these lines to find the proper URL attachment, again, assuming that's the bit you want:

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
}];
0
sudo On

Here's my rough Swift 4 version I'm using together with your magic PLIST. Seems to work in both News and 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
    }
}