从今天扩展程序打开时 iOS 应用程序崩溃
<p><p><a href="https://stackoverflow.com/questions/27562409/crash-when-open-containing-app-from-today-extension" rel="noreferrer noopener nofollow">Crash when open containing app from today extension</a> 的可能重复项.</p>
<p>我正在尝试使用自定义 URL 方案从今天的扩展小部件打开包含应用程序。我在扩展中使用的代码是:</p>
<pre><code>let urlStr = String(format: "myapp://?device_id=%@", deviceId!)
let url = NSURL(string: urlStr)
self.extensionContext!.openURL(url!, completionHandler: nil)
</code></pre>
<p>但是,包含应用程序在打开时崩溃:</p>
<pre><code>Thread : Crashed: com.apple.main-thread
0CoreFoundation 0x0000000103e572dc CFStringCreateCopy + 28
1libswiftFoundation.dylib 0x0000000105f02a44 _TF10Foundation24_convertNSStringToStringFCSo8NSStringSS + 148
2MyApp 0x00000001017a9055 @objc MyApp.CLAppDelegate.application (MyApp.CLAppDelegate)(ObjectiveC.UIApplication, openURL : ObjectiveC.NSURL, sourceApplication : Swift.String, annotation : Swift.Optional<Swift.AnyObject>) -> Swift.Bool (CLAppDelegate.swift)
3UIKit 0x0000000104898685 - + 185
4UIKit 0x00000001048a0816 - + 2380
5UIKit 0x00000001048a42e8 __88-_block_invoke + 196
6UIKit 0x00000001048a4215 - + 349
7UIKit 0x000000010488f31a - + 486
8FrontBoardServices 0x000000010903a2a3 __31-_block_invoke + 16
9CoreFoundation 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
</code></pre>
<p>从其他任何地方打开时,相同的自定义 URL 方案都有效。</p>
<p>使用 Xcode 6.1.1,部署目标 8.1。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>设法自己解决了这个问题,并认为我会发布解决方案。</p>
<p>事实证明,由 Xcode 先前版本之一自动完成的 UIApplicationDelegate 声明是不正确的:</p>
<pre><code>func application(application: UIApplication,
openURL url: NSURL,
sourceApplication: String!, // Wrong
annotation: AnyObject?) -> Bool
</code></pre>
<p>应该是:</p>
<pre><code>func application(application: UIApplication,
openURL url: NSURL,
sourceApplication: String?, // OK
annotation: AnyObject?) -> Bool
</code></pre>
<p>(<code>sourceApplication</code> 需要是 <code>String?</code> 而不是 <code>String!</code>)</p>
<p>希望这对某人有所帮助。</p></p>
<p style="font-size: 20px;">关于从今天扩展程序打开时 iOS 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/27941626/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/27941626/
</a>
</p>
页:
[1]