OpenUrl freezes app for over 10 seconds

前端 未结 11 880
孤街浪徒
孤街浪徒 2020-12-13 02:05

I\'m currently developing an App, that needs to open a browser to display a webpage. To do that i use the [UIApplication sharedApplication] openURL method with

相关标签:
11条回答
  • 2020-12-13 02:22

    I have seen the same issue in iOS 7. My solution is only slightly different from those already proposed. By using performSelector with just a 0.1 second delay, the app immediately opens the URL.

    [self performSelector:@selector(methodToRedirectToURL:) withObject:url afterDelay:0.1];
    
    0 讨论(0)
  • 2020-12-13 02:24

    Here is the answer in Swift 3.0 with a check to see if we can open the URL or not.

    guard let url = URL(string: myURLString) else {
        return
    }
    
    
    if UIApplication.shared.canOpenURL(url) {
       DispatchQueue.main.async {
         UIApplication.shared.openURL(url)
       }
    }
    
    0 讨论(0)
  • 2020-12-13 02:26

    I found it will get better to use this since iOS 10.

    dispatch_async(dispatch_get_main_queue(), ^{
        if ([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."].firstObject integerValue] < 10) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:..."]];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:..."] options:@{} completionHandler:^(BOOL success) {
    
            }];
        }
    });
    
    0 讨论(0)
  • 2020-12-13 02:29

    If you put the "openURL" action in the viewDidLoad method, then it sure will execute slowly. You can put it in the viewDidAppear method. Or, you can use the GCD in the viewDidLoad method like below:

    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] openURL:url];
    });
    
    0 讨论(0)
  • 2020-12-13 02:30

    Swift 4.1 with OS version check.

    DispatchQueue.main.async() {
      if #available(iOS 10.0, *) {
        UIApplication.shared.open(url)
      } else {
        UIApplication.shared.openURL(url)
      }
    }
    
    0 讨论(0)
  • 2020-12-13 02:35

    For Swift3

        DispatchQueue.main.async {
            UIApplication.shared.openURL(url)
        }
    
    0 讨论(0)
提交回复
热议问题