我在NSItemProvider对象上使用loadItemForTypeIdentifier:options:completionHandler:方法,通过iOS 8中的Share扩展从Safari中提取url.
在Objective-C中,此代码和工作以及块运行.
[itemProvider loadItemForTypeIdentifier:(@"public.url" options:nil completionHandler:^(NSURL *url, NSError *error) {
//My code
}];
Run Code Online (Sandbox Code Playgroud)
在Swift中,它看起来非常相似,但是关闭不会运行.此外, itemProvider.hasItemConformingToTypeIdentifier("public.url")返回YES所以必须有一个有效的对象来解析内部的URL itemProvider.
itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (urlItem, error) in
//My code
})
Run Code Online (Sandbox Code Playgroud)
Info.plist NSExtension部分与Objective-C和Swift版本完全相同,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?