首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在safari而不是webview中打开url?

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

Stack Overflow用户
提问于 2012-07-17 22:32:43
回答 8查看 53K关注 0票数 22

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

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

回答 8

Stack Overflow用户

发布于 2012-07-17 22:36:52

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

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

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

票数 44
EN

Stack Overflow用户

发布于 2012-07-17 22:37:57

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

Stack Overflow用户

发布于 2016-12-10 14:54:33

对于iOS 10+

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

// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)

有关更多信息,请参阅THIS

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11524805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档