ios 如何在 safari 中而不是在 webview 中打开 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11524805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:17:11  来源:igfitidea点击:

how to open the url in safari not in webview

iosobjective-cxcode

提问by user578386

I want to open an url in safari, outisde the app and not in webview.

我想在 safari 中打开一个 url,在应用程序之外而不是在 webview 中。

I implemented the UIWebViewDelegatebut I am still not able to open the url. Basically I am not able to click the url.

我实现了UIWebViewDelegate但我仍然无法打开 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;
}

回答by JTApps

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

只需将它放在您的按钮按下或您想要调用它的任何事件中,然后向它传递一个 URL(替换 @"http://www.apple.com")。

回答by George

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 UIWebViewDelegateand depending on that request argument you should return TRUEor FALSE. If you don't want the web view to open it, you should call:

fromUIWebViewDelegate并根据该请求参数,您应该返回TRUEor FALSE。如果你不想让 web 视图打开它,你应该调用:

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

as others mentioned and return FALSE.

正如其他人提到并返回FALSE

Hope this helps. Cheers!

希望这可以帮助。干杯!

EDIT:If the links are not recognized in your web view, try this:

编辑:如果在您的网络视图中无法识别链接,请尝试以下操作:

[webView setDataDetectorTypes:UIDataDetectorTypeLink]

回答by Obaid Maroof

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

回答by Dharmesh Kheni

For iOS 10+

对于 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.

有关更多信息,请参阅这个

回答by Bijan

This works for me:

这对我有用:

[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: @"http://www.apple.com"]];

回答by kordiseps

- (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.

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

回答by Bhavin Chauhan

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

******************** 斯威夫特************************

//MARK: Button Click on open with SafariViewController

//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

//MARK: SafatriViewConroller 关闭

func safariViewControllerDidFinish(controller: SFSafariViewController)
{

    controller.dismissViewControllerAnimated(true, completion: nil)
}

回答by I don't know

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

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

回答by dirtsniffer

Swift and no webview way

Swift 和没有 webview 的方式

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

回答by Charlie Seligman

Swift 3(iOS 10+):

斯威夫特 3(iOS 10+):

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