如何在Safari中打开网址而不是在webview中

use*_*386 19 xcode objective-c ios

我想在safari中打开一个url,超出应用程序,而不是在webview中.

我实现了UIWebViewDelegate,但我仍然无法打开网址.基本上我无法点击网址.

以下是代码:

-(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;
}
Run Code Online (Sandbox Code Playgroud)

JTA*_*pps 43

这个答案很容易通过谷歌获得:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]];
Run Code Online (Sandbox Code Playgroud)

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


Geo*_*rge 39

阅读评论后,我认为这是您正在寻找的:

实现此方法:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
Run Code Online (Sandbox Code Playgroud)

来自UIWebViewDelegate并取决于您应该返回的请求参数TRUEFALSE.如果您不希望Web视图打开它,您应该致电:

[[UIApplication sharedApplication] openURL:request.URL];
Run Code Online (Sandbox Code Playgroud)

正如其他人提到的那样FALSE.

希望这可以帮助.干杯!

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

[webView setDataDetectorTypes:UIDataDetectorTypeLink]
Run Code Online (Sandbox Code Playgroud)


Oba*_*oof 8

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
Run Code Online (Sandbox Code Playgroud)


Dha*_*esh 5

对于 iOS 10+

// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];

// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

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