Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open the url in safari not in webview

Tags:

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; } 
like image 810
user578386 Avatar asked Jul 17 '12 14:07

user578386


People also ask

How do I open links in Safari not app?

There is no way to force it to open the url in safari and not open the app. Every app will have list of urls that it can open. So you have to go to that app settings and tell that it should open in browser for the urls and not in the app.

How do I open a link in Safari in SwiftUI?

SwiftUI gives us a dedicated Link view that looks like a button but opens a URL in Safari when pressed. It's easy enough to use – just give it a title for the button, plus a destination URL to show, like this: Link("Learn SwiftUI", destination: URL(string: "https://www.hackingwithswift.com/quick-start/swiftui")!)


2 Answers

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").

like image 137
JTApps Avatar answered Sep 28 '22 05:09

JTApps


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] 
like image 22
George Avatar answered Sep 28 '22 05:09

George