美文网首页
iPad上WKWebView js交互失效

iPad上WKWebView js交互失效

作者: 大炮打小鸟 | 来源:发表于2020-10-26 16:50 被阅读0次

最近公司项目在iPad上所有js交互都失效了,是因为WKWebView 在iPad上加载手机端的网址时,会自动将该网址转为PC端的网址,所以只需改变wkwebview的userAgent浏览器标识就可以了,附上代码:

.h

#import <WebKit/WebKit.h>
@interface WhzxWebView : WKWebView

@end

.m

#import "WhzxWebView.h"

@implementation WhzxWebView

- (instancetype)initWithFrame:(CGRect)frame configuration:(nonnull WKWebViewConfiguration *)configuration
{
    self = [super initWithFrame:frame configuration:configuration];
    if (self) {
        [self evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id __nullable userAgent, NSError * __nullable error) {
            self.customUserAgent = [userAgent stringByReplacingOccurrencesOfString:@"iPad" withString:@"iPhone"];
        }];
    }
    return self;
}
@end

创建WKWebview继承此webview即可

相关文章

网友评论

      本文标题:iPad上WKWebView js交互失效

      本文链接:https://www.haomeiwen.com/subject/thzamktx.html