I want to open an url in safari, outisde the app and not in webview.
I implemented the UIWebViewDelegate but I am still not able to open the url. Basically I am not
I found this answer in google but I need to after opening the browser terminate my application and continue the browser.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"] options: @{} completionHandler: nil];
in Android can do It easy to calling finish()
method how can I do that
This answer was readily available via Google:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]];
Just put that in your button press or whatever event you're wanting to call it on, and then pass it a URL (replace the @"http:/www.apple.com").
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (![[NSString stringWithFormat:@"%@",[request URL]] containsString:@"file"] ) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
I used an html file in local. In this html there is some links.
If you set delegate UIWebViewDelegate and use this local html will open in your webView and the other links will open in safari
I wrote "file" because this link is "file:///Users/~/x.app/about.html" in local.
Swift 3 (iOS 10+):
if let url = URL(string: "http://www.seligmanventures.com") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
For iOS 10+
// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];
// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)
For more info refer THIS.
******************** Swift **********************
//MARK: Button Click on open with SafariViewController
private var urlString:String = "https://google.com"
@IBAction func openWithSafariVC(sender: AnyObject)
{
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
svc.delegate = self
self.presentViewController(svc, animated: true, completion: nil)
}
//MARK: SafatriViewConroller Dismiss
func safariViewControllerDidFinish(controller: SFSafariViewController)
{
controller.dismissViewControllerAnimated(true, completion: nil)
}