Ios 运行苹果&x27;的默认操作扩展代码引发异常或不执行任何操作
当我尝试运行苹果的默认操作扩展代码时,它要么什么也不做,要么崩溃。如何修复这两个bug 安装程序Ios 运行苹果&x27;的默认操作扩展代码引发异常或不执行任何操作,ios,iphone,swift,ios-extensions,Ios,Iphone,Swift,Ios Extensions,当我尝试运行苹果的默认操作扩展代码时,它要么什么也不做,要么崩溃。如何修复这两个bug 安装程序 在Xcode 7中创建新的操作扩展目标(语言=Swift,操作类型=无用户界面) 在模拟器中运行扩展,选择Safari作为要运行的应用程序 在Safari中导航到 调用您刚刚创建的扩展(您需要点击更多按钮以在活动表中启用它) 错误1:extensionContext是nil 在Safari中,轻触操作按钮,然后轻触扩展按钮1-5次。最终,分机将在此线路上崩溃: self.extensionConte
extensionContext
是nil
在Safari中,轻触操作按钮,然后轻触扩展按钮1-5次。最终,分机将在此线路上崩溃:
self.extensionContext!.completeRequestReturningItems([resultsItem], completionHandler: nil)
日志将显示:
致命错误:在展开可选值时意外发现nil
例外情况是:EXC\u BAD\u指令
原因是extensionContext
是nil
为什么会发生这种情况,我如何修复它
Bug 2:当它不崩溃时,它什么也不做
当应用程序没有崩溃时,似乎什么都没有发生。根据代码,背景色似乎应该改为红色或绿色,但在我尝试过的任何网站上都没有
有没有你见过的网站的例子?如何改进代码,使其真正发挥作用
我尝试过的事情 尝试1:在上下文中传递 由于代码在块内运行,因此我没有通过self.extensionContext引用上下文,而是尝试将其传递给需要它的函数(
itemloadecompletedwithprepreprocessingresults(\u:context:)
和doneWithResults(\u: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
}
}
这里也有同样的问题。。实际上,我可以在模拟器上运行我的项目,但只有当我尝试在设备上运行项目时,才会出现这个问题。我发现这并不是因为我在使用swift 2,因为我也在objC中重写了所有扩展代码,而且它也不起作用