Javascript in UIWebView callback to C/Objective-C

后端 未结 2 1258
既然无缘
既然无缘 2020-11-27 03:44

Is there a way to get a callback to objective-c when a certain event has been detected in a UIWebView? Can Javascript send a callback to Objective-C?

2条回答
  •  被撕碎了的回忆
    2020-11-27 04:01

    Just to illustrate the solution by "bpapa" with actual code:

    WARNING: untested code

    Implement this method in the UIWebView's delegate...

    -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
        if ( [[[inRequest URL] scheme] isEqualToString:@"callback"] ) {
    
            // Do something interesting...
    
            return NO;
        }
    
        return YES;
    }
    

    ...then put a link in the webwieb like this:

    Click me
    

    And it should activate your callback-code. Obviously, you could trigger it with a javascript instead of a plain link.

提交回复
热议问题