iOS:如何重写 UIWebView 的按钮操作并调用我们自己的 Objective C 方法

Vip*_*dav 5 iphone xcode ios swift

我正在 iOS 中的 UIWebView 中加载 url。url 包含一个按钮。当我点击这个按钮时,我想调用一个 Objective C 方法。请帮忙。

Lu_*_*Lu_ 0

如果是您的页面,请通过 JavaScript 与应用程序通信,否则您可以重写 shouldStartLoadWithRequest 方法并检查此特定请求是否被调用某些代码:

- (BOOL)isItThatButtonEvent:(NSURLRequest *)req {
    if([req.URL.absoluteString isEqualString:@"that.specific.url.or.some.other.way.to.check.that"]) {
        return YES;
    }
    return NO;
}

#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if([self isItThatButtonEvent:request]) {
        [self methodThatOpensYourViewController];
        return NO;
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)