xcode 如何使用 UIWebView 打开 Web 浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5779652/
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-09-14 20:54:30  来源:igfitidea点击:

How to open web browser with UIWebView

objective-cxcodesafariuiwebview

提问by PatrickGamboa

Hey guys, On my Xcode project, I am trying to open a web browser at the click of one of my buttons, but here's what I'm really trying to do. First of all, I am opening a web browser using this example code:

嘿伙计们,在我的 Xcode 项目中,我试图通过单击其中一个按钮来打开 Web 浏览器,但这就是我真正想做的事情。首先,我使用以下示例代码打开 Web 浏览器:

NSURL *fanPageURL = [NSURL URLWithString:@"fb://profile/210227459693"];

if (![[UIApplication sharedApplication] openURL: fanPageURL]) {
    NSURL *webURL = [NSURL URLWithString:@"http://www.facebook.com/pages/Blackout-Labs/210227459693"];
    [[UIApplication sharedApplication] openURL: webURL];
}
[super viewDidLoad];

Now this code works properly, but the problem is that it's closing my application and opens the link in the Safari application, which is not what I want. I went the other way around by creating another view (after click the button), then inserted a UIWebView using Interface Builder, but it still didn't work. So I was hoping someone can help me out how to open this link within my app instead of closing my app and opening the link in the Safari app, thanks

现在这段代码可以正常工作,但问题是它正在关闭我的应用程序并在 Safari 应用程序中打开链接,这不是我想要的。我反过来创建另一个视图(单击按钮后),然后使用 Interface Builder 插入一个 UIWebView,但它仍然不起作用。所以我希望有人能帮助我如何在我的应用程序中打开这个链接,而不是关闭我的应用程序并在 Safari 应用程序中打开链接,谢谢

回答by fabian789

Look at SVWebViewController. It's a ready made UIViewControllersubclass with a UIWebViewin it and all you need to do to show it is

看看SVWebViewController。它是一个现成的UIViewController子类,里面有一个UIWebView,你需要做的就是展示它

SVWebViewController *webViewController = [[SVWebViewController alloc] initWithAddress:@"http://google.com"];
[self.navigationController pushViewController:webViewController animated:YES];

There is also a modal version, see the Usage Section.

还有一个模态版本,请参阅用法部分

回答by conmulligan

You'll need to create a UIViewControllersubclass that has UIWebViewas a subview, and show that when the button in tapped.

您需要创建一个UIViewController具有UIWebView子视图的子类,并在点击按钮时显示。