iOS 9.3 在调用 openURL 后出现冻结

6

在iOS 9.3 builds 13E233 & 13E234上调用openURL后,我的应用程序会冻结。

我尝试使用dispatch_after,但这并没有解决问题。

以下是代码,没有什么特别之处。

+ (void)someMethod:(UIView *)senderView {

    [Utility showLoadingHUDWithText:nil inView:senderView];

    [[SomeClient sharedClient] someNetworkAPI:^(id result) {
        [Utility hideAllHUDsForView:senderView];

        NSDictionary *dict = (NSDictionary *)result;
        NSString *someString = dict[@"someKey"];
        NSURL *url = [NSURL URLWithString:someString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    } fail:^(NSError *error) {
        [Utility hideAllHUDsForView:senderView];
        [Utility showMessageHUD:error.localizedDescription];
    }];
}

这似乎是iOS的一个bug,很多其他应用程序也受到了影响。


你能展示一下你的代码吗? - Fonix
请问您能否发布整个函数的代码? - Luca D'Alberti
3
我敢打赌这与http://thenextweb.com/apple/2016/03/27/theres-ios-9-3-bug-freezing-crashing-devices-links/#gref有关。 - Alladinian
我有一个非常相似的代码,也遇到了同样的问题。在我的所有设备上都可以正常运行,但在一些随机客户端上会冻结... @nix-wang HUD是否一直停留在屏幕上而不是消失?因为这就是客户报告的问题... - sabadow
@sabadow 没错。但我在其他没有任何 HUD 的应用程序中也发现了这个问题。 - Nix Wang
只是评论一下,苹果发布了iOS 9.3.1似乎修复了这个问题。 - sabadow
2个回答

7

当在iOS 9.3的主线程上调用时,这确实会导致死锁(在iPhone 6和iPhone 6 Plus上测试)。

semaphore

回溯:

* thread #1: tid = 0x41840, 0x0000000180ac1014 libsystem_kernel.dylib`semaphore_wait_trap + 8, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
  * frame #0: 0x0000000180ac1014 libsystem_kernel.dylib`semaphore_wait_trap + 8
    frame #1: 0x000000010023fa20 libdispatch.dylib`_dispatch_semaphore_wait_slow + 244
    frame #2: 0x0000000180bda934 libxpc.dylib`xpc_connection_send_message_with_reply_sync + 204
    frame #3: 0x000000018276a238 MobileCoreServices`_LSStartOpenOperation + 232
    frame #4: 0x0000000182791efc MobileCoreServices`-[LSOpenOperation main] + 1160
    frame #5: 0x0000000182771bc0 MobileCoreServices`-[LSApplicationWorkspace openURL:withOptions:error:] + 472
    frame #6: 0x000000018633cdd8 UIKit`-[UIApplication _openURL:] + 356
    frame #7: 0x0000000100096c04 MyApp`__29-[ViewController someMethod]_block_invoke(.block_descriptor=0x0000000125d37e20) + 216 at ViewController.m:57
    frame #8: 0x000000010022da7c libdispatch.dylib`_dispatch_call_block_and_release + 24
    frame #9: 0x000000010022da3c libdispatch.dylib`_dispatch_client_callout + 16
    frame #10: 0x00000001002334e4 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 2096
    frame #11: 0x0000000180ef8dd8 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
    frame #12: 0x0000000180ef6c40 CoreFoundation`__CFRunLoopRun + 1628
    frame #13: 0x0000000180e20d10 CoreFoundation`CFRunLoopRunSpecific + 384
    frame #14: 0x0000000182708088 GraphicsServices`GSEventRunModal + 180
    frame #15: 0x00000001860f5f70 UIKit`UIApplicationMain + 204
    frame #16: 0x00000001000970d0 MyApp`main(argc=1, argv=0x000000016fd6fae8) + 124 at main.m:14
    frame #17: 0x00000001809be8b8 libdyld.dylib`start + 4

我发现将整个调用包装在一个dispatch_async块中可以解决这个问题:
dispatch_async(dispatch_get_main_queue(), ^{
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
});

我会向苹果提出一份雷达报告,并在Open Radar上发布它。看起来这个问题已经在iOS 9.3.1中得到了解决:

修复了一个问题,导致在Safari和其他应用程序中点击链接后应用程序无响应。


我在我的iOS 9.3的iPhone6上发现了相同的错误。实际上,我无法使用Safari(我无法打开链接:S),所有使用openURL的应用程序都会崩溃。例如:Slack。奇怪的是,我的同事拥有相同的配置(iPhone6与iOS9.3),但错误并未发生。 - Luca D'Alberti
实际上,我在控制台中收到了以下错误信息: <Warning>: Error getting NanoAppRegistry workspace info: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.nanoappregistry.workspace was invalidated." UserInfo={NSDebugDescription=The connection to service named com.apple.nanoappregistry.workspace was invalidated.} - Luca D'Alberti

2

我在iPad Air 2 / 3上运行iOS 9.3.5时遇到了同样的问题,JAL的答案对我没有解决问题。

下面的代码完美解决了这个问题。

fileprivate func letApplicationHandleUrl(_ url: URL) {

    guard UIApplication.shared.canOpenURL(url) else {

        return
    }

    if #available(iOS 10.0, *) {

        UIApplication.shared.open(url, options: [:], completionHandler: nil)

    } else {

        DispatchQueue.global(qos: .userInteractive).async {

            UIApplication.shared.openURL(url)
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接