gpt4 book ai didi

ios - 在另一个 View Controller 中打开 webview 链接

转载 作者:行者123 更新时间:2023-11-29 11:43:20 26 4
gpt4 key购买 nike

我有一个 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];
}

有什么解决办法吗?谢谢!

最佳答案

从 UIWebview 委托(delegate)中,您应该使用“shouldStartLoadWith”并检查 linkClicked 的 navigationType:

    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 委托(delegate)的更多信息可在此处找到: https://developer.apple.com/documentation/uikit/uiwebviewdelegate/1617945-webview

关于ios - 在另一个 View Controller 中打开 webview 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45266386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com