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
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];
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)
}
}
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) {
}];
}
});
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];
});
Swift 4.1 with OS version check.
DispatchQueue.main.async() {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
For Swift3
DispatchQueue.main.async {
UIApplication.shared.openURL(url)
}