[英]How to open a link from one UIWebView to another UIWebView?
I'm building an iOS app. 我正在构建一个iOS应用。
I have HTML content in a UIWebView
with hyperlinks, and I'm unable to open a link to another UIWebView
. 我在带有超链接的
UIWebView
具有HTML内容,但是无法打开到另一个UIWebView
的链接。
I used UIWebView
as a subview of ViewController
. 我使用
UIWebView
作为ViewController
的子视图。 Here is the code: 这是代码:
- (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;
}
}
}
You only need to segue to a new controller with a web view, and pass the request to it. 您只需要通过Web视图选择到新的控制器,然后将请求传递给它。 So something like this in your first case,
所以在您的第一种情况下,
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.
}
Notice that the sender argument in performSegueWithIdentifier:sender: is the request returned by the delegate method. 请注意,performSegueWithIdentifier:sender:中的sender参数是委托方法返回的请求。 Pass this request to a property in the view controller you're segueing to,
将此请求传递到您要选择的视图控制器中的属性,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSURLRequest *)sender {
NextViewController *next = segue.destinationViewController;
next.request = sender;
}
Finally, use that request to load the web view in NextViewController. 最后,使用该请求将Web视图加载到NextViewController中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.