[WebView] Inject
2021. 9. 29. 18:04ㆍ개발자료/iOS
반응형
## 뷰포트(Viewport) 추가
NSString *strJs = @"var meta = document.createElement('meta');";
strJs = [strJs stringByAppendingString:@"meta.name = 'viewport';"];
strJs = [strJs stringByAppendingString:@"meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';"];
strJs = [strJs stringByAppendingString:@"var head = document.getElementsByTagName('head')[0];"];
strJs = [strJs stringByAppendingString:@"head.appendChild(meta);"];
WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:strJs
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES];
[webview.configuration.userContentController addUserScript:wkUserScript];
## 스타일(Style) 추가
NSString *strJs = @"var style = document.createElement('style');";
strJs = [strJs stringByAppendingString:@"style.innerHTML = 'img { max-width: 100%; height: auto;}';"];
strJs = [strJs stringByAppendingString:@"document.head.appendChild(style);"];
WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:strJs
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES];
[webview.configuration.userContentController addUserScript:wkUserScript];
반응형