作者:再见傻瓜傻瓜_299 | 来源:互联网 | 2023-09-09 12:36
显示效果如下
oc代码如下
/**
* 跳转判断
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *urlString = request.URL.absoluteString;
NSLog(@"urlString:%@",urlString);
NSString *preString = @"fzw://";
if([urlString hasPrefix:preString])
{
NSString *methodString = [urlString substringFromIndex:preString.length];
NSLog(@"methodString:%@",methodString);
[self performSelector:NSSelectorFromString(methodString)];
return NO;
}
return YES;
-(void)send
{
NSLog(@"%s",__func__);
}
点击demo.html的按钮 js调用oc ,网页跳转到fzw://send。UIWebView的代理方法- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType拦截页面并获取网页链接fzw://send,经过处理,最后调用oc指定方法-(void)send,并返回NO取消跳转,从而达到js调用oc方法。
但xcode提示警告: PerformSelector may cause a leak because its selector is unknown
二、问题分析
编译器的警告对开发者来说是很有用的信息,但有时由于编译器的智商实在太低,会提示一些无谓的警告。当有些警告不想看到时,可以用如下代码消除警告。
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-W警告名称"
需要关闭警告的代码
#pragma clang diagnostic pop
三、问题解决
该警告的名称为-Warc-performSelector-leaks
/**
* 跳转判断
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *urlString = request.URL.absoluteString;
NSLog(@"urlString:%@",urlString);
NSString *preString = @"fzw://";
if([urlString hasPrefix:preString])
{
NSString *methodString = [urlString substringFromIndex:preString.length];
NSLog(@"methodString:%@",methodString);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:NSSelectorFromString(methodString)];
#pragma clang diagnostic pop
return NO;
}
return YES;
-(void)send
{
NSLog(@"%s",__func__);
}