我正在构建一个 iOS 应用程序。
我的 UIWebView
中包含带有超链接的 HTML 内容,但我无法打开指向另一个 UIWebView
的链接。
我使用 UIWebView 作为 ViewController 的 subview 。这是代码:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
switch (navigationType) {
case UIWebViewNavigationTypeLinkClicked: { //this is when a user tap is detected
//write the handling code here.
isWebViewLoaded = false; //set this to false only if you open another view controller.
return NO; //prevent tapped URL from loading inside the UIWebView.
}
// some other typical parameters within a UIWebView. Use what is needed
case UIWebViewNavigationTypeFormResubmitted: return YES;
case UIWebViewNavigationTypeReload: return YES;
//for all other cases, including UIWebViewNavigationTypeOther called when UIWebView is loading for the first time
default: {
if (!isWebViewLoaded) {
isWebViewLoaded = true;
return YES;
}
else
return NO;
}
}
}
最佳答案
您只需使用 Web View 连接到一个新 Controller ,并将请求传递给它即可。所以在你的第一个例子中是这样的,
case UIWebViewNavigationTypeLinkClicked: { //this is when a user tap is detected
[self performSegueWithIdentifier:@"Next" sender:request];
isWebViewLoaded = false; //set this to false only if you open another view controller.
return NO; //prevent tapped URL from loading inside the UIWebView.
}
请注意,performSegueWithIdentifier:sender: 中的 sender 参数是委托(delegate)方法返回的请求。将此请求传递给您要访问的 View Controller 中的属性,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSURLRequest *)sender {
NextViewController *next = segue.destinationViewController;
next.request = sender;
}
最后,使用该请求在 NextViewController 中加载 Web View 。
关于ios - 如何打开从一个 UIWebView 到另一个 UIWebView 的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24635107/