我有一个webview,我想在另一个ViewController中打开我可以在该webview中单击的URL,例如更改导航栏并添加后退按钮。
这是我目前在objective-c中的代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = @"https://www.apple.com/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.webView.delegate = (id)self;
[self.webView loadRequest:requestObj];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
refreshControl.tintColor = [UIColor clearColor];
[self.webView.scrollView addSubview:refreshControl];
}
-(void)handleRefresh:(UIRefreshControl *)refresh {
NSString *fullURL = @"https://www.apple.com/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[refresh endRefreshing];
}
任何解决方案? 谢谢!
答案 0 :(得分:1)
你应该使用“shouldStartLoadWith”并检查navigationType for linkClicked:
func webView(_ webView: UIWebView,
shouldStartLoadWith request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked{
if let url = URL(string:"YourURLString") {
if UIApplication.shared.canOpenURL(url){
UIApplication.shared.openURL(url)
return false
}
}
}
return true
}
有关uiwebview代表的更多信息,请访问: https://developer.apple.com/documentation/uikit/uiwebviewdelegate/1617945-webview