从今天的扩展程序打开包含应用程序的崩溃

时间:2014-12-19 08:45:42

标签: ios swift uikit lldb ios-extensions

我今天创建了扩展程序并添加了一个打开包含应用程序的按钮。内部扩展视图控制器:

@IBAction func pressed() {
    extensionContext?.openURL(NSURL(string:"myApp://")!, completionHandler: nil)
}

因此,我还将URL Scheme添加到包含app plist:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myApp</string>
        </array>
    </dict>
</array>

Widget工作正常,但是当我尝试按下widget时,我的应用程序崩溃,包含应用程序AppDelegate.swift。无法到达application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?)内的断点。 回溯:

* thread #1: tid = 0x27fb0, 0x00000001861bd698 CoreFoundation`CFStringCreateCopy + 36, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00000001861bd698 CoreFoundation`CFStringCreateCopy + 36
    frame #1: 0x00000001019f7954 libswiftFoundation.dylib`Foundation._convertNSStringToString (ObjectiveC.NSString) -> Swift.String + 152
    frame #2: 0x000000010030172c myApp`@objc myApp.AppDelegate.application (myApp.AppDelegate)(ObjectiveC.UIApplication, openURL : ObjectiveC.NSURL, sourceApplication : Swift.String, annotation : Swift.Optional<Swift.AnyObject>) -> Swift.Bool + 104 at AppDelegate.swift:0
    frame #3: 0x000000018ad172f0 UIKit`-[UIApplication _applicationOpenURL:payload:] + 216
    frame #4: 0x000000018ad1b4c8 UIKit`-[UIApplication _handleNonLaunchSpecificActions:forScene:withTransitionContext:] + 2420
    frame #5: 0x000000018ad1f810 UIKit`-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:] + 2220
    frame #6: 0x000000018ad1ecec UIKit`__88-[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:]_block_invoke + 140
    frame #7: 0x000000018ad1ec44 UIKit`-[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:] + 380
    frame #8: 0x000000018ad12578 UIKit`-[UIApplication scene:didUpdateWithDiff:transitionContext:completion:] + 512
    frame #9: 0x000000018e54562c FrontBoardServices`__31-[FBSSerialQueue performAsync:]_block_invoke + 28
    frame #10: 0x000000018628ea28 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
    frame #11: 0x000000018628db30 CoreFoundation`__CFRunLoopDoBlocks + 312
    frame #12: 0x000000018628c154 CoreFoundation`__CFRunLoopRun + 1756
    frame #13: 0x00000001861b90a4 CoreFoundation`CFRunLoopRunSpecific + 396
    frame #14: 0x000000018f35b5a4 GraphicsServices`GSEventRunModal + 168
    frame #15: 0x000000018aaee3c0 UIKit`UIApplicationMain + 1488
  * frame #16: 0x0000000100307df4 myApp`top_level_code + 76 at AppDelegate.swift:15
    frame #17: 0x0000000100307f0c myApp`main + 48 at AppDelegate.swift:0
    frame #18: 0x000000019709aa08 libdyld.dylib`start + 4

如果我在Safari中打开myApp://,那么app会正常打开而不会崩溃。

1 个答案:

答案 0 :(得分:1)

我遇到了一个非常类似的问题。我使用此Swift代码实现了application:openURL:sourceApplication:annotation:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {

但事实证明sourceApplication实际上应该是可选的:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

不知道我是怎么做错的(我假设Xcode是自动完成的,但它现在似乎正在做正确的事情),但改变了这一点就为我解决了。