我想在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;
}
发布于 2012-07-17 22:36:52
这个答案很容易通过谷歌获得:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]];
只需将其放入您的按钮按下或您想要调用它的任何事件中,然后传递一个URL (替换@“http://www.apple.com”)。
发布于 2012-07-17 22:37:57
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
发布于 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。
https://stackoverflow.com/questions/11524805
复制相似问题