ios 判断wkwebview 判断调用ajax里面的memberlogout接口 object-c
要判断WKWebView是否调用了/ajax/member/logout接口,可以通过WKNavigationDelegate的代理方法来实现。
首先,在你的ViewController中设置WKWebView的代理:
@interface ViewController () <WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建WKWebView
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
self.webView.navigationDelegate = self;
[self.view addSubview:self.webView];
// 加载网页
NSURL *url = [NSURL URLWithString:@"https://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
...
@end
然后,在WKNavigationDelegate中的代理方法webView:decidePolicyForNavigationAction:decisionHandler:中判断请求的URL是否为/ajax/member/logout:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *url = navigationAction.request.URL;
if ([url.absoluteString isEqualToString:@"https://example.com/ajax/member/logout"]) {
// 调用了/ajax/member/logout接口
NSLog(@"调用了/ajax/member/logout接口");
}
// 其他处理
decisionHandler(WKNavigationActionPolicyAllow);
}
在这个代理方法中,你可以根据需要进行其他处理,比如拦截URL并进行特定操作。最后,记得调用decisionHandler来决定是否允许加载页面
原文地址: http://www.cveoy.top/t/topic/iTlk 著作权归作者所有。请勿转载和采集!