将RegexKitLite( https://github.com/wezm/RegexKitLite ) 拖入项目记得必须是拖入,该文件比较老属于mrc,且需要svn环境,所以别pod,拖入项目即可。
在target中选择build phases —compile source 找到 RegexKitLite.m文件双击添加-fno-objc-arc 支持arc混编 即可解决报错
用法:
1.///YYLabel/YYText 加载html标签获取a标签的内容链接
+(NSString *)getATagWithHtmlString:(NSString *)htmlString {
NSString *regex_http = @"(.*?)<\\/a>";
NSArray *array_http = [htmlString arrayOfCaptureComponentsMatchedByRegex:regex_http];
NSString *linkStr = @"";
if([array_http count]) {
for(NSArray *arrayinarray_http) {
linkStr = [array.firstObject componentsSeparatedByString:@"\""][1];
}
}
returnlinkStr;
}
需求场景:后端返回html,客户端使用YYLabel/YYtext加载html时候,需要拿到a标签的链接 作为跳转路由,或者参数等等。该方法即获取a标签内容
2.webView的需求场景常见于webView加载url/html后a标签点击原本可以继续进入H5的二级页面,但是如果原生也存在这个页面,为了体验,要求直接进入原生页面。
webView需要通过代理方法拦截点击事件的,获得a标签内容
问题:下方代码我们在使用中发现,很多跳转中navigationAction.navigationType == WKNavigationTypeOther,这时,我们的判断就不会生效。此时如果你的后端不配合你,直甩给你带a标签的html,将WKNavigationTypeOther中代码注释掉(正确优化详见:https://www.pudn.com/news/6300b6e6f97302478e7d5616.html)
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
switch(navigationAction.navigationType) {
caseWKNavigationTypeLinkActivated: {
[selfpushCurrentSnapshotViewWithRequest:navigationAction.request];
break;
}
caseWKNavigationTypeFormSubmitted: {
[selfpushCurrentSnapshotViewWithRequest:navigationAction.request];
break;
}
caseWKNavigationTypeBackForward: {
break;
}
caseWKNavigationTypeReload: {
break;
}
caseWKNavigationTypeFormResubmitted: {
break;
}
caseWKNavigationTypeOther: {
[selfpushCurrentSnapshotViewWithRequest:navigationAction.request];
break;
}
default: {
break;
}
}
[selfupdateNavigationItems];
decisionHandler(WKNavigationActionPolicyAllow);
}
//请求链接处理
-(void)pushCurrentSnapshotViewWithRequest:(NSURLRequest*)request{
// NSLog(@"push with request %@",request);
NSURLRequest* lastRequest = (NSURLRequest*)[[self.snapShotsArray lastObject] objectForKey:@"request"];
//如果url是很奇怪的就不push
if([request.URL.absoluteString isEqualToString:@"about:blank"]) {
// NSLog(@"about blank!! return");
return;
}
//如果url一样就不进行push
if([lastRequest.URL.absoluteString isEqualToString:request.URL.absoluteString]) {
return;
}
//拦截a标签链接内容,并判断是否本地/rn页面存在该路由
if([SGVCRouter canRouteURL:request.URL]) {
[SGVCRouter openURL:request.URL.absoluteString];
}else{
//传给RN 看RN是否具备该路由,都不具备则继续在webView里面浏览下一级页面
}
UIView* currentSnapShotView = [self.wkWebView snapshotViewAfterScreenUpdates:YES];
[self.snapShotsArray addObject:
@{@"request":request,@"snapShotView":currentSnapShotView}];
}