Reputation: 153
I have a webview and I want to open the urls that I can click in that webview in another ViewController for example to change the navigation bar and add a back button.
This is my current code in 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];
}
Any solutions? Thank you!
Upvotes: 1
Views: 584
Reputation: 181
from UIWebview delegate you should use "shouldStartLoadWith" and check 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
}
More info about uiwebview delegates can be found here: https://developer.apple.com/documentation/uikit/uiwebviewdelegate/1617945-webview
Upvotes: 1