將RegexKitLite(?https://github.com/wezm/RegexKitLite?) 拖入項(xiàng)目記得必須是拖入解取,該文件比較老屬于mrc嫌套,且需要svn環(huán)境滓窍,所以別pod炕淮,拖入項(xiàng)目即可逢勾。
在target中選擇build phases —compile source 找到 RegexKitLite.m文件雙擊添加-fno-objc-arc 支持arc混編 即可解決報(bào)錯(cuò)
用法:
1.///YYLabel/YYText 加載html標(biāo)簽獲取a標(biāo)簽的內(nèi)容鏈接
+(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時(shí)候族铆,需要拿到a標(biāo)簽的鏈接 作為跳轉(zhuǎn)路由汉规,或者參數(shù)等等导匣。該方法即獲取a標(biāo)簽內(nèi)容
2.webView的需求場景常見于webView加載url/html后a標(biāo)簽點(diǎn)擊原本可以繼續(xù)進(jìn)入H5的二級頁面才菠,但是如果原生也存在這個(gè)頁面,為了體驗(yàn)贡定,要求直接進(jìn)入原生頁面赋访。
webView需要通過代理方法攔截點(diǎn)擊事件的,獲得a標(biāo)簽內(nèi)容
問題:下方代碼我們在使用中發(fā)現(xiàn)缓待,很多跳轉(zhuǎn)中navigationAction.navigationType == WKNavigationTypeOther蚓耽,這時(shí),我們的判斷就不會(huì)生效旋炒。此時(shí)如果你的后端不配合你步悠,直甩給你帶a標(biāo)簽的html,將WKNavigationTypeOther中代碼注釋掉(正確優(yōu)化詳見: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一樣就不進(jìn)行push
? ? if([lastRequest.URL.absoluteString isEqualToString:request.URL.absoluteString]) {
? ? ? ? return;
? ? }
? ? //攔截a標(biāo)簽鏈接內(nèi)容瘫镇,并判斷是否本地/rn頁面存在該路由
? if([SGVCRouter canRouteURL:request.URL]) {
? ? [SGVCRouter openURL:request.URL.absoluteString];
? }else{
?? //傳給RN 看RN是否具備該路由鼎兽,都不具備則繼續(xù)在webView里面瀏覽下一級頁面
?? }
? ? UIView* currentSnapShotView = [self.wkWebView snapshotViewAfterScreenUpdates:YES];
? ? [self.snapShotsArray addObject:
?? ? @{@"request":request,@"snapShotView":currentSnapShotView}];
}