简体   繁体   English

从今天扩展名打开时,iOS应用程序崩溃

[英]iOS app crash when opened from today extension

Possible duplicate of Crash when open containing app from today extension . 当打开包含今天扩展名的应用程序时,可能会崩溃

I'm trying to open the containing app from a today extension widget using a custom URL scheme. 我正在尝试使用自定义URL方案从Today扩展窗口小部件中打开包含的应用程序。 The code i'm using inside the extension is: 我在扩展程序中使用的代码是:

let urlStr = String(format: "myapp://?device_id=%@", deviceId!)
let url = NSURL(string: urlStr)
self.extensionContext!.openURL(url!, completionHandler: nil)

However, the containing app crashes when opened: 但是,包含的应用程序在打开时崩溃:

Thread : Crashed: com.apple.main-thread
0  CoreFoundation                 0x0000000103e572dc CFStringCreateCopy + 28
1  libswiftFoundation.dylib       0x0000000105f02a44 _TF10Foundation24_convertNSStringToStringFCSo8NSStringSS + 148
2  MyApp                          0x00000001017a9055 @objc MyApp.CLAppDelegate.application (MyApp.CLAppDelegate)(ObjectiveC.UIApplication, openURL : ObjectiveC.NSURL, sourceApplication : Swift.String, annotation : Swift.Optional<Swift.AnyObject>) -> Swift.Bool (CLAppDelegate.swift)
3  UIKit                          0x0000000104898685 -[UIApplication _applicationOpenURL:payload:] + 185
4  UIKit                          0x00000001048a0816 -[UIApplication _handleNonLaunchSpecificActions:forScene:withTransitionContext:] + 2380
5  UIKit                          0x00000001048a42e8 __88-[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:]_block_invoke + 196
6  UIKit                          0x00000001048a4215 -[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:] + 349
7  UIKit                          0x000000010488f31a -[UIApplication scene:didUpdateWithDiff:transitionContext:completion:] + 486
8  FrontBoardServices             0x000000010903a2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
9  CoreFoundation                 0x0000000103eb553c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
10 CoreFoundation                 0x0000000103eab285 __CFRunLoopDoBlocks + 341
11 CoreFoundation                 0x0000000103eaaa43 __CFRunLoopRun + 851
12 CoreFoundation                 0x0000000103eaa486 CFRunLoopRunSpecific + 470
13 GraphicsServices               0x0000000106e6e9f0 GSEventRunModal + 161
14 UIKit                          0x0000000104891420 UIApplicationMain + 1282
15 MyApp                       0x00000001017ac57e top_level_code (CLAppDelegate.swift:16)
16 MyApp                       0x00000001017ac5ba main (CLAppDelegate.swift)
17 libdyld.dylib                  0x0000000106155145 start + 1

The same custom URL scheme is working when opened from anywhere else. 从其他任何地方打开时,相同的自定义URL方案也有效。

Using Xcode 6.1.1, Deployment target 8.1. 使用Xcode 6.1.1,部署目标为8.1。

Managed to solve this myself and thought I'll post the solution. 设法自己解决了这个问题,并以为我会发布解决方案。

Turns out, the UIApplicationDelegate declaration as auto-completed by one of Xcode's previous versions was incorrect: 原来,由Xcode的先前版本之一自动完成的UIApplicationDelegate声明是错误的:

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

should really be: 确实应该是:

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

( sourceApplication needs to be String? and NOT String! ) sourceApplication需要为String?而不是String!

Hope this helps someone. 希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM