当我尝试为操作扩展运行 Apple 的默认代码时,它要么什么都不做,要么崩溃。如何修复这两个错误?
设置
- 在 Xcode 7 中创建一个新的动作扩展目标(语言 = Swift,动作类型 = 无用户界面)
- 在模拟器中运行扩展程序,选择 Safari 作为要运行的应用程序。
- 在 Safari 中导航到https://google.com
- 调用您刚刚创建的扩展程序(您需要点击更多按钮以在活动表中启用它)。
错误1:extensionContext
是nil
在 Safari 中点击操作按钮,然后点击扩展按钮 1-5 次。最终扩展将在这一行崩溃:
self.extensionContext!.completeRequestReturningItems([resultsItem], completionHandler: nil)
日志会说:
致命错误:在展开可选值时意外发现 nil
例外是:EXC_BAD_INSTRUCTION
原因extensionContext
是nil
。
为什么会发生这种情况,我该如何解决?
错误 2:当它不崩溃时,它什么也不做
当应用程序没有崩溃时,似乎什么也没有发生。根据代码,背景颜色似乎应该变为红色或绿色,但在我尝试过的任何网站上都不会发生这种情况。
有没有你看到它工作的网站的例子?如何改进代码以使其实际执行某些操作?
我尝试过的事情
尝试 1:在上下文中传递
由于代码在块内运行,而不是通过 self.extensionContext 引用上下文,我尝试将其传递给需要它的函数(itemLoadCompletedWithPreprocessingResults(_:context:)
和doneWithResults(_:context:)
)。
这似乎更稳定(到目前为止它只崩溃过一次),但它仍然没有修改背景颜色。
参考
作为参考,这里是默认代码ActionRequestHandler.swift
:
import UIKit
import MobileCoreServices
class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
var extensionContext: NSExtensionContext?
func beginRequestWithExtensionContext(context: NSExtensionContext) {
// Do not call super in an Action extension with no user interface
self.extensionContext = context
var found = false
// Find the item containing the results from the JavaScript preprocessing.
outer:
for item: AnyObject in context.inputItems {
let extItem = item as! NSExtensionItem
if let attachments = extItem.attachments {
for itemProvider: AnyObject in attachments {
if itemProvider.hasItemConformingToTypeIdentifier(String(kUTTypePropertyList)) {
itemProvider.loadItemForTypeIdentifier(String(kUTTypePropertyList), options: nil, completionHandler: { (item, error) in
let dictionary = item as! [String: AnyObject]
NSOperationQueue.mainQueue().addOperationWithBlock {
self.itemLoadCompletedWithPreprocessingResults(dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! [NSObject: AnyObject])
}
found = true
})
if found {
break outer
}
}
}
}
}
if !found {
self.doneWithResults(nil)
}
}
func itemLoadCompletedWithPreprocessingResults(javaScriptPreprocessingResults: [NSObject: AnyObject]) {
// Here, do something, potentially asynchronously, with the preprocessing
// results.
// In this very simple example, the JavaScript will have passed us the
// current background color style, if there is one. We will construct a
// dictionary to send back with a desired new background color style.
let bgColor: AnyObject? = javaScriptPreprocessingResults["currentBackgroundColor"]
if bgColor == nil || bgColor! as! String == "" {
// No specific background color? Request setting the background to red.
self.doneWithResults(["newBackgroundColor": "red"])
} else {
// Specific background color is set? Request replacing it with green.
self.doneWithResults(["newBackgroundColor": "green"])
}
}
func doneWithResults(resultsForJavaScriptFinalizeArg: [NSObject: AnyObject]?) {
if let resultsForJavaScriptFinalize = resultsForJavaScriptFinalizeArg {
// Construct an NSExtensionItem of the appropriate type to return our
// results dictionary in.
// These will be used as the arguments to the JavaScript finalize()
// method.
let resultsDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: resultsForJavaScriptFinalize]
let resultsProvider = NSItemProvider(item: resultsDictionary, typeIdentifier: String(kUTTypePropertyList))
let resultsItem = NSExtensionItem()
resultsItem.attachments = [resultsProvider]
// Signal that we're complete, returning our results.
self.extensionContext!.completeRequestReturningItems([resultsItem], completionHandler: nil)
} else {
// We still need to signal that we're done even if we have nothing to
// pass back.
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
}
// Don't hold on to this after we finished with it.
self.extensionContext = nil
}
}