我试图弄清楚如何让我的共享扩展程序访问网页并操纵其内容。我关注了the documentation
但我一定遗漏了一些东西,因为我没有看到任何符合 kUTTypePropertyList
的项目,因为我认为我应该这样做。
我的 plist 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>WebOutLoudExtension</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.bretlester.WebsiteReader.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionJavaScriptPreprocessingFile</key>
<string>ExtensionPreProcessing</string>
</dict>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
</dict>
</plist>
我的 ExtensionPreProcessing 看起来像这样:
ExtensionPreprocessingJS = {
run : function(arguments) {
arguments.completionFunction({"hello":"world"});
},
finalize : function(arguments) {
document.body.style.backgroundColor = 'red';
}
};
最后,我的 ShareViewController 看起来像这样:
class ShareViewController: UIViewController, NSExtensionRequestHandling {
override func viewDidLoad() {
let identifierType = NSString(format: kUTTypePropertyList, NSUTF8StringEncoding)
for (item: NSExtensionItem) in self.extensionContext?.inputItems as [NSExtensionItem] {
for (itemProvider: NSItemProvider) in item.attachments as [NSItemProvider] {
if itemProvider.hasItemConformingToTypeIdentifier(identifierType) {
println("why doesn't this happen?")
}
}
}
}
override func viewDidAppear(animated: Bool) {
}
override func beginRequestWithExtensionContext(context: NSExtensionContext) {
super.beginRequestWithExtensionContext(context)
let identifierType = NSString(format: kUTTypePropertyList, NSUTF8StringEncoding)
//println(self.extensionContext?.inputItems)
for (item: NSExtensionItem) in context.inputItems as [NSExtensionItem] {
for (itemProvider: NSItemProvider) in item.attachments as [NSItemProvider] {
if itemProvider.hasItemConformingToTypeIdentifier(identifierType) {
println("why doesn't this happen?")
}
}
}
}
}
注意我的 println 语句。他们从不执行。我基本上只是在寻找某种确认工作正常的方式。我的共享扩展显示在 Safari 中,但我认为我的自定义 javascript 没有运行。希望有人能发现我做错了什么。提前致谢
最佳答案
你是对的,JS 没有运行。这是因为您的 Info.plist 配置。 NSExtensionJavaScriptPreprocessingFile
需要是 NSExtensionAttributes
的子键,而不是 NSExtensionActivationRule
的子键。就像是
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionJavaScriptPreprocessingFile</key>
<string>ExtensionPreProcessing</string>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
关于操作网页的 iOS 共享扩展,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28670274/