Search code examples
iosobjective-ccordovawkwebviewcordova-ios

Cordova-ios is creating both UIWebView and WKWebView Components. How to switch to only WKWebView?


I am using cordova-ios 6.2.0. I am creating a webview browser in my project. I recently debugged that my project is creating UIWebView instance instead of WKWebView. I scanned the whole project but i did not find any reference to UIWebView.

In the header file also my property is set to WKWebView: @property (nonatomic, strong) IBOutlet WKWebView* webView;

UIWebView at runtime

I tried to configure WKWebView. so that the instance will set WKWebView at runtime but end up creating both the views.

- (void)createViews
{
    // We create the views in code for primarily for ease of upgrades and not requiring an external .xib to be included
    
    CGRect webViewBounds = self.view.bounds;
    WKUserContentController* userContentController = [[WKUserContentController alloc] init];
    
    WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
    
    NSString *userAgent = configuration.applicationNameForUserAgent;

    configuration.applicationNameForUserAgent = userAgent;
    configuration.userContentController = userContentController;
    configuration.processPool = [[CDVWebViewProcessPoolFactory sharedFactory] sharedProcessPool];

    self.webView = [[WKWebView alloc] initWithFrame:webViewBounds configuration:configuration];
    [self.view addSubview:self.webView];
    [self.view sendSubviewToBack:self.webView];
    
    self.webView.navigationDelegate = self;
    self.webView.backgroundColor = [UIColor whiteColor];

}

WKWebView

Debug View Hierarchy

Also when i tried to set webView simply like the below code the UIWebView was set at runtime:

- (void)setupWebView {
    self.webView = [[WKWebView alloc] initWithFrame: CGRectZero];
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    [self.view sendSubviewToBack:self.webView];
}

Here is the config.xml I am using.

    <content src="index.html" />
    <plugin name="cordova-sqlite-storage" spec="git+https://github.com/litehelpers/Cordova-sqlite-storage.git" />
    <plugin name="cordova-plugin-splashscreen" spec="^6.0.0" />
    <plugin name="cordova-plugin-statusbar" spec="^3.0.0" />
    <plugin name="cordova-plugin-keyboard" spec="^1.2.0" />
    <plugin name="cordova-plugin-inappbrowser" spec="^5.0.0" />
    <plugin name="cordova-plugin-ionic-webview" />
    
    <platform name="ios">
        <preference name="WKWebViewOnly" value="true" />

        <feature name="CDVWKWebViewEngine">
            <param name="ios-package" value="CDVWKWebViewEngine" />
        </feature>

        <preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine" />
    </platform>
    
    <feature name="CDVWKWebViewEngine">
        <param name="ios-package" value="CDVWKWebViewEngine" />
    </feature>
    <feature name="NetworkStatus">
        <param name="ios-package" value="CDVConnection" />
    </feature>
    <feature name="LocalStorage">
        <param name="ios-package" value="CDVLocalStorage" />
    </feature>
    <feature name="SplashScreen">
        <param name="ios-package" value="CDVSplashScreen" />
        <param name="onload" value="true" />
    </feature>
    <feature name="InAppBrowser">
        <param name="ios-package" value="CDVInAppBrowser" />
    </feature>
    <feature name="AppAvailability">
        <param name="ios-package" value="AppAvailability" />
    </feature>
    <feature name="Battery">
        <param name="ios-package" value="CDVBattery" />
    </feature>
    <feature name="Camera">
        <param name="ios-package" value="CDVCamera" />
    </feature>
    <preference name="CameraUsesGeolocation" value="false" />
    <feature name="Console">
        <param name="ios-package" value="CDVLogger" />
    </feature>
    <feature name="Device">
        <param name="ios-package" value="CDVDevice" />
    </feature>
    <feature name="Accelerometer">
        <param name="ios-package" value="CDVAccelerometer" />
    </feature>
    <feature name="Compass">
        <param name="ios-package" value="CDVCompass" />
    </feature>
    <feature name="Notification">
        <param name="ios-package" value="CDVNotification" />
    </feature>
    <feature name="File">
        <param name="ios-package" value="CDVFile" />
        <param name="onload" value="true" />
    </feature>
    <feature name="FileTransfer">
        <param name="ios-package" value="CDVFileTransfer" />
    </feature>
    <feature name="Geolocation">
        <param name="ios-package" value="CDVLocation" />
    </feature>
    <feature name="Media">
        <param name="ios-package" value="CDVSound" />
    </feature>
    <feature name="Capture">
        <param name="ios-package" value="CDVCapture" />
    </feature>
    <preference name="ScrollEnabled" value="true" />
    <preference name="show-splash-screen-spinner" value="true" />
    <preference name="AutoHideSplashScreen" value="true" />
    <preference name="Splashscreen" value="screen" />
    <preference name="SplashScreenDelay" value="4000" />
    <preference name="AutoHideSplashScreen" value="true" />
    <preference name="ShowSplashScreenSpinner" value="true" />
    <preference name="fullscreen" value="false" />
    <preference name="WebViewBounce" value="false" />
    <preference name="Orientation" value="portrait" />
    <preference name="AllowInlineMediaPlayback" value="false" />
    <preference name="BackupWebStorage" value="cloud" />
    <preference name="DisallowOverscroll" value="true" />
    <preference name="EnableViewportScale" value="false" />
    <preference name="KeyboardDisplayRequiresUserAction" value="true" />
    <preference name="MediaTypesRequiringUserActionForPlayback" value="false" />
    <preference name="SuppressesIncrementalRendering" value="false" />
    <preference name="TopActivityIndicator" value="gray" />
    <preference name="GapBetweenPages" value="0" />
    <preference name="PageLength" value="0" />
    <preference name="PaginationBreakingMode" value="page" />
    <preference name="PaginationMode" value="unpaginated" />
    <preference name="StatusBarOverlaysWebView" value="false" />
    <preference name="StatusBarBackgroundColor" value="black" />
    <preference name="android-minSdkVersion" value="23" />
    <preference name="android-targetSdkVersion" value="31" />

    <preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine" />

    //IOS CORS
    <preference name="NativeXHRLogging" value="none" />
    <preference name="AllowUntrustedCerts"  value="true" />
    <preference name="InterceptRemoteRequests" value="secureOnly" />
    <preference name="allowFileAccessFromFileURLs" value="true" />
    <preference name="allowUniversalAccessFromFileURLs" value="true" />
    //Android CORS
    <preference name="AndroidInsecureFileModeEnabled" value="true" />

Solution

  • I found out that in my custom plugin XIB file. I was using deprecated UIWebView component. I deleted it and added WKWebView instead and it solved the problem.