Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-7991 WKWebView integration
  3. CB-6884

[WKWebView][iOS 8] Support new Cordova bridge

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • None
    • cordova-ios, cordova-js
    • None
    • iOS 8 beta 2

    Description

      https://developer.apple.com/library/prerelease/ios/releasenotes/General/RN-iOSSDK-8.0/index.html

      WKUserContentController - add a script message handler with name "cordova":
      https://developer.apple.com/library/prerelease/ios/documentation/WebKit/Reference/WKUserContentController_Ref/index.html#//apple_ref/swift/cl/WKUserContentController

      The WKUserContentController is associated with a WKWebViewConfiguration which is initialized with a WKWebView.

      WKUserContentController* userContentController = [[WKUserContentController alloc] init];
      // scriptMessageHandler is the object that conforms to the WKScriptMessageHandler protocol
      // see https://developer.apple.com/library/prerelease/ios/documentation/WebKit/Reference/WKScriptMessageHandler_Ref/index.html#//apple_ref/swift/intf/WKScriptMessageHandler
      [userContentController addScriptMessageHandler:scriptMessageHandler name:@"cordova"];
      

      In JavaScript, you would call into Objective-C thus:

      var command = { callbackId: "blabla23", service:"Camera", action: "takePicture", actionArgs: [ 1, 2, 3 ] };
      window.webkit.messageHandlers.cordova.postMessage(command);
      

      In the object that conforms to the WKScriptMessageHandler protocol:

      - (void)userContentController:(WKUserContentController *)userContentController   didReceiveScriptMessage:(WKScriptMessage *)message
      {
          if (![message.name isEqualToString:@"cordova"]) {
              return;
          }
          NSDictionary* command = message.body;
          // TODO: get the callbackId, service, action, actionArgs keys and create a CDVInvokedUrlCommand, and dispatch it
      }
      

      Attachments

        Issue Links

          Activity

            People

              shazron Shazron Abdullah
              shazron Shazron Abdullah
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: