Problem description
The HTML code is as follows
1 <HTML>2 <Head>3 <MetaCharSet= "Utf-8"/>4 <title>My Web page</title>5 <Scripttype= "Text/javascript">6 functionjs2oc ()7 {8 window.location.href="Fzw://send";9 }Ten </Script> One </Head> A <Body> - <inputvalue= "JS call oc"type= "button"onclick= ' js2oc (); '></input> - </Body> the </HTML>
The display effect is as follows
The OC code is as follows
1 /**2 * Jump to Judge3 */4-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (uiwebviewnavigationtype) Navigationtype5 {6NSString *urlstring =request. url.absolutestring;7NSLog (@"urlstring:%@", urlstring);8NSString *prestring =@"fzw://";9 if([urlstring hasprefix:prestring])Ten { OneNSString *methodstring =[URLString substringFromIndex:preString.length]; ANSLog (@"methodstring:%@", methodstring); - [Self performselector:nsselectorfromstring (methodstring)]; - returnNO; the } - returnYES; - } - +-(void) Send - { +NSLog (@"%s", __func__); A}
Click on the demo.html button "JS call oc", the webpage jumps to fzw://send. UIWebView Proxy Method-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) request Navigationtype: (uiwebviewnavigationtype) Navigationtype intercept page and get web link fzw://send, after processing, finally call OC Specify Method-(void) Send, and return no cancel jump, so as to achieve JS call oc method.
But Xcode hint warning: performselector may cause a leak because it selector is unknown
Problem analysis
Compiler warnings are useful information for developers, but sometimes the compiler's IQ is too low to hint at unnecessary warnings. When some warnings do not want to be seen, you can use the following code to eliminate the warning.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-W warning name"
Code to close the warning
#pragma clang diagnostic pop
Problem solving
The name of the warning is-warc-performselector-leaks
1 /**2 * Jump to Judge3 */4-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (uiwebviewnavigationtype) Navigationtype5 {6NSString *urlstring =request. url.absolutestring;7NSLog (@"urlstring:%@", urlstring);8NSString *prestring =@"fzw://";9 if([urlstring hasprefix:prestring])Ten { OneNSString *methodstring =[URLString substringFromIndex:preString.length]; ANSLog (@"methodstring:%@", methodstring); - #pragmaClang Diagnostic push - #pragmaClang diagnostic ignored "-warc-performselector-leaks" the [Self performselector:nsselectorfromstring (methodstring)]; - #pragmaClang Diagnostic Pop - returnNO; - } + returnYES; - } + A-(void) Send at { -NSLog (@"%s", __func__); -}
IOS 12 Learning Series: Warnings for Xcode ignore elimination processing