2

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

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

因此,我还将 URL Scheme 添加到包含应用程序 plist 中:

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

小部件工作正常,但是当我尝试按下小部件时,我的应用程序在包含应用程序 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

如果我myApp://在 Safari 中打开,应用程序可以正确打开而不会崩溃。

4

1 回答 1

1

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

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 自动完成,但它现在似乎在做正确的事情),但改变它为我修复了它。

于 2015-03-02T17:34:23.203 回答