有时候通过UIWebView打开网页后,还会通过其子链接继续打开链接,这时候如何进行相关的操作呢?比如说,获取子标题,或获取子链接html。
我们可以通过如下代理方法进行操作,需要注意的是,执行完该代理方法后,另一代理方法"
- (void)webViewDidFinishLoad:(UIWebView *)webView”也会被再次招执行。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (!self.shouldChangeTitle)
{
return YES;
}
//判断是否是单击
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSURL *url = [request URL];
NSString *urlString = url.relativeString;
urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSRange range = [urlString rangeOfString:@"/" options:NSBackwardsSearch];
urlString = [urlString substringFromIndex:range.location + range.length];
range = [urlString rangeOfString:@"."];
urlString = [urlString substringToIndex:range.location];
self.subTitle = urlString;
}
return YES;
}