Crash when open containing app from today extension

非 Y 不嫁゛ 提交于 2020-01-25 04:40:10

问题


I created today extension and add a button to open containing app. Inside extension view controller:

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

So, I also add URL Scheme to containing app plist:

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

Widget works fine, but when I try to press widget my app crashes inside containing app AppDelegate.swift. Breakpoint inside application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) can't be reached. Backtrace:

* 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

If I open myApp:// in Safari, app open correctly w/o crash.


回答1:


I ran into a very similar issue. I'd implemented application:openURL:sourceApplication:annotation: with this Swift code:

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

But it turns out that sourceApplication should actually be optional:

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

No idea how I got that wrong (I'm assuming Xcode autocomplete, but it seems to be doing the right thing now), but changing that fixed it for me.



来源:https://stackoverflow.com/questions/27562409/crash-when-open-containing-app-from-today-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!