iOS 8 Share extension loadItemForTypeIdentifier:options:completionHandler: completion closure not executing

guptron picture guptron · Jun 16, 2014 · Viewed 10.2k times · Source

I'm using the loadItemForTypeIdentifier:options:completionHandler: method on an NSItemProvider object to extract a url from Safari via a Share extension in iOS 8.

In Objective-C, this code and works and the block runs.

[itemProvider loadItemForTypeIdentifier:(@"public.url" options:nil completionHandler:^(NSURL *url, NSError *error) {
    //My code
}];

In Swift, it looks very similar, however the closure doesn't run. Also, itemProvider.hasItemConformingToTypeIdentifier("public.url") returns YES so there must be a valid object to parse the url from inside the itemProvider.

itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (urlItem, error) in
    //My code
})

The Info.plist NSExtension portion is exactly the same for both Objective-C and Swift version and looks like this:

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
            <integer>1</integer>
        </dict>
        <key>NSExtensionPointName</key>
        <string>com.apple.share-services</string>
        <key>NSExtensionPointVersion</key>
        <string>1.0</string>
    </dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
</dict>

What am I doing wrong?

Answer

pJes2 picture pJes2 · Oct 14, 2014

call

self.extensionContext!.completeRequestReturningItems([], completionHandler: nil) 

at the end of completionHandler instead of calling it at the end of didSelectPost()