Reputation: 1061
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 able to click the url.
Below is the code:
-(void)newView:(NSString *)title Description:(NSString *)desc URL:(NSString *)url{
webView =[[UIWebView alloc]initWithFrame:CGRectMake(15, 17, 190, 190)];
webView.backgroundColor=[UIColor clearColor];
webView.delegate=self;
webView.opaque = NO;
[webView loadHTMLString:[NSString stringWithFormat:@"<html><body p style='color:white' text=\"#FFFFFF\" face=\"Bookman Old Style, Book Antiqua, Garamond\" size=\"5\">%@ %@</body></html>", desc,url] baseURL:nil];
v = [[HUDView alloc] initWithFrame:CGRectMake(60, 70, 220, 220)];
cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = CGRectMake(0, 0, 30, 30);
[cancelButton setBackgroundImage:[UIImage imageNamed:@"closebox.png"] forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancelButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:cancelButton];
[v addSubview:webView];
[self.view addSubview:v];
}
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Upvotes: 22
Views: 55187
Reputation: 4604
Swift 3 (iOS 10+):
if let url = URL(string: "http://www.seligmanventures.com") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Upvotes: 0
Reputation: 4029
After reading the comments I think this is what you're looking for:
Implement this method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
from UIWebViewDelegate
and depending on that request argument you should return TRUE
or FALSE
. If you don't want the web view to open it, you should call:
[[UIApplication sharedApplication] openURL:request.URL];
as others mentioned and return FALSE
.
Hope this helps. Cheers!
EDIT: If the links are not recognized in your web view, try this:
[webView setDataDetectorTypes:UIDataDetectorTypeLink]
Upvotes: 43
Reputation: 801
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
Upvotes: 1
Reputation: 71852
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.
Upvotes: 6
Reputation: 149
Swift and no webview way
if let url = NSURL(string: "http://www.apple.com") {
UIApplication.sharedApplication().openURL(url)
}
Upvotes: 0
Reputation: 611
******************** 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)
}
Upvotes: 1
Reputation: 391
- (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.
Upvotes: 2
Reputation: 26507
This works for me:
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: @"http://www.apple.com"]];
Upvotes: 3
Reputation: 1579
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
Upvotes: 8
Reputation: 5780
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").
Upvotes: 44