gpt4 book ai didi

操纵网页的 iOS 共享扩展

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:41 25 4
gpt4 key购买 nike

我正在尝试弄清楚如何让我的共享扩展程序访问网页并操作其内容。我关注了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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com