如何在Safari浏览器中打开url,而不是在webview中

我想在safari中打开一个url,outisde的应用程序,而不是在web视图。

我实现了UIWebViewDelegate,但我仍然无法打开url。 基本上我无法点击url。

以下是代码:

-(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; } 

看完评论后,我认为这是你要找的东西:

实现这个方法:

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; 

UIWebViewDelegate并根据该请求参数,你应该返回TRUEFALSE 。 如果你不想让Web视图打开它,你应该打电话给:

 [[UIApplication sharedApplication] openURL:request.URL]; 

正如其他人提到的,并返回FALSE

希望这可以帮助。 干杯!

编辑:如果链接无法在您的网页视图中识别,请试试这个:

 [webView setDataDetectorTypes:UIDataDetectorTypeLink] 

这个答案很容易通过谷歌提供:

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]]; 

只要把它放在你的button按下或任何你想打电话的事件,然后传递一个URL(replace@“http:/www.apple.com”)。

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]]; 

这适用于我:

 [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: @"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; } 

我在本地使用了一个html文件。 在这个html中有一些链接。 如果你设置委托UIWebViewDelegate并使用这个本地HTML将在您的webView中打开,其他链接将在Safari浏览器中打开
我写了“文件”,因为这个链接在本地是“file:///Users/~/x.app/about.html”。

对于iOS 10+

 // Objective-C UIApplication *application = [UIApplication sharedApplication]; [application openURL:URL options:@{} completionHandler:nil]; // Swift UIApplication.shared.open(url, options: [:], completionHandler: nil) 

欲了解更多信息,请参阅本文。

******************** Swift ******************

// MARK:Button点击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:SafatriViewConrollerclosures

 func safariViewControllerDidFinish(controller: SFSafariViewController) { controller.dismissViewControllerAnimated(true, completion: nil) } 

Swift和没有webview的方式

  if let url = NSURL(string: "http://www.apple.com") { UIApplication.sharedApplication().openURL(url) } 

Swift 3 (iOS 10+):

 if let url = URL(string: "http://www.seligmanventures.com") { UIApplication.shared.open(url, options: [:], completionHandler: nil) } 

我在谷歌find了这个答案,但我需要打开浏览器终止我的应用程序,并继续浏览器。

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"] options: @{} completionHandler: nil]; 

在Android中可以做到这一点很容易调用finish()方法我怎么能做到这一点