在我的应用程序中,我集成了共享扩展。当我共享 safari 页面时,我可以获取 url。但是当我去亚马逊网站或应用程序时。当我点击任何产品的共享选项时,我的共享扩展名没有显示。这是我的 info.plist
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>TRUEPREDICATE</string>
<key>NSExtensionJavaScriptPreprocessingFile</key>
<string>GetURL</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
这是我的.js
var GetURL = function() {};
GetURL.prototype = {
run: function(arguments) {
arguments.completionFunction({"URL": document.URL});
}
};
var ExtensionPreprocessingJS = new GetURL;
这就是我访问 url 和图像的方式
private func getURL() {
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? NSDictionary else { return }
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary,
let urlString = results["URL"] as? String,
let url = NSURL(string: urlString) {
self.url = url
}
}
})
} else {
print("error")
}
let propertyListImage = String(kUTTypeImage)
if itemProvider.hasItemConformingToTypeIdentifier(propertyListImage) {
itemProvider.loadItem(forTypeIdentifier: propertyListImage, options: nil, completionHandler: { (item, error) -> Void in
self.imgURL = item as! NSURL
guard let dictionary = item as? NSDictionary else { return }
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary,
let urlString = results["URL"] as? String,
let url = NSURL(string: urlString) {
self.imgURL = url
}
}
})
} else {
print("error")
}
}