最近用webView打开本地PDF、doc和excel文件,发现默认打开文件时直接全屏加载,在jianshu上看到一篇文章发现直接注入屏幕适应代码就可以了。jianshu地址:www.jianshu.com/p/34b351cc5… 代码如下:
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *script = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *useCtro = [[WKUserContentController alloc] init];
[useCtro addUserScript:script];
WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
webConfig.userContentController = useCtro;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:webConfig];
webView.navigationDelegate = self;