我想在safari中打开一个网址,超出应用程序,而不是在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;
}
答案 0 :(得分:42)
这个答案很容易通过谷歌获得:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]];
只需将其放入按钮或任何您想要调用的事件中,然后将其传递给URL(替换@“http:/www.apple.com”)。
答案 1 :(得分:38)
阅读评论后,我认为这就是您要找的内容:
实施此方法:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
来自UIWebViewDelegate
,根据请求参数,您应该返回TRUE
或FALSE
。如果您不希望Web视图打开它,请致电:
[[UIApplication sharedApplication] openURL:request.URL];
正如其他人提到并返回FALSE
。
希望这会有所帮助。干杯!
编辑:如果您的网络视图中无法识别链接,请尝试以下操作:
[webView setDataDetectorTypes:UIDataDetectorTypeLink]
答案 2 :(得分:8)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
答案 3 :(得分:4)
适用于iOS 10 +
// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];
// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)
有关详细信息,请参阅THIS。
答案 4 :(得分:3)
这对我有用:
[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: @"http://www.apple.com"]];
答案 5 :(得分:2)
- (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”。
答案 6 :(得分:1)
********************斯威夫特**********************
// MARK:按钮单击使用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
func safariViewControllerDidFinish(controller: SFSafariViewController)
{
controller.dismissViewControllerAnimated(true, completion: nil)
}
答案 7 :(得分:1)
我在谷歌找到了这个答案,但我需要在打开浏览器后终止我的应用程序并继续浏览器。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"] options: @{} completionHandler: nil];
Android中的可以轻松调用finish()
方法,我该怎么做
答案 8 :(得分:0)
Swift,没有webview方式
if let url = NSURL(string: "http://www.apple.com") {
UIApplication.sharedApplication().openURL(url)
}
答案 9 :(得分:0)
Swift 3 (iOS 10 +):
{{1}}