WKWebView init by code

This commit is contained in:
lealife
2020-04-23 16:00:13 +08:00
parent 62772e083b
commit d255fac619
3 changed files with 39 additions and 11 deletions

View File

@@ -18,7 +18,7 @@
<key>AutocorrectionType</key>
<string>No</string>
<key>DefaultValue</key>
<string>2.3 (3095)</string>
<string>2.3 (30A4)</string>
<key>IsSecure</key>
<false/>
<key>Key</key>

View File

@@ -17,7 +17,6 @@
<outlet property="refreshButton" destination="6" id="49"/>
<outlet property="toolbar" destination="3" id="8"/>
<outlet property="view" destination="1" id="14"/>
<outlet property="webView" destination="5" id="9"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
@@ -25,15 +24,6 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<wkWebView contentMode="scaleToFill" id="5">
<rect key="frame" x="0.0" y="0.0" width="320" height="436"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<wkWebViewConfiguration key="configuration" allowsInlineMediaPlayback="YES">
<audiovisualMediaTypes key="mediaTypesRequiringUserActionForPlayback" audio="YES" video="YES"/>
<wkPreferences key="preferences"/>
</wkWebViewConfiguration>
</wkWebView>
<toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="bottom" barStyle="black" id="3">
<rect key="frame" x="0.0" y="436" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>

View File

@@ -15,6 +15,9 @@
https://github.com/wordpress-mobile/WordPress-iOS/blob/8ff4b5b18c638f6b592018f277616db2734de2cb/WordPress/Classes/Utility/WPWebViewController.m
block: https://www.jianshu.com/p/d911cd16c100
Xcode11-退Could not instantiate class named WKWebView https://www.bilibili.com/read/cv3184836/
iOS--constraints https://blog.csdn.net/wang1514869032/article/details/52164199
contstrait https://blog.csdn.net/jason_chen13/article/details/52869540
*/
#import "Common.h"
@@ -61,6 +64,41 @@
{
[super viewDidLoad];
//
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
//
WKPreferences *preference = [[WKPreferences alloc]init];
// javaScriptEnabledNO
preference.minimumFontSize = 0;
// javaScript
preference.javaScriptEnabled = YES;
// iOSNOjavaScript
preference.javaScriptCanOpenWindowsAutomatically = YES;
config.preferences = preference;
// 使h5线, 使
config.allowsInlineMediaPlayback = YES;
// NO
config.requiresUserActionForMediaPlayback = YES;
//
config.allowsPictureInPictureMediaPlayback = YES;
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
// self.webView = [[WKWebView alloc] init];
// self.view.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_webView];
NSDictionary *dic = @{
@"webView":self.webView
};
NSString *vfl = @"V:|-0-[webView]-44-|"; // 0, 44
NSString *vfl1 = @"H:|-0-[webView]-0-|"; // 0
_webView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:0 metrics:nil views:dic]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl1 options:0 metrics:nil views:dic]];
// delegate didStartProvisionalNavigation !!!
self.webView.navigationDelegate = self;