Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.08 KB | None | 0 0
  1. //
  2. //  WebViewController.swift
  3. //  WebviewDemo
  4. //
  5. //  Created by marco.rossi on 22/09/2019.
  6. //  Copyright © 2019 marco.rossi. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import WebKit
  11.  
  12. class WebViewController: UIViewController {
  13.    
  14.     private var webView: WKWebView!
  15.  
  16.     override func viewDidLoad() {
  17.         super.viewDidLoad()
  18.        
  19.         let config = WKWebViewConfiguration()
  20.         let userContentController = WKUserContentController()
  21.  
  22.         userContentController.add(self, name: "camera")
  23.         userContentController.add(self, name: "data")
  24.         config.userContentController = userContentController
  25.  
  26.         webView = WKWebView(frame: .zero, configuration: config)
  27.  
  28.         view.addSubview(webView)
  29.  
  30.         webView.translatesAutoresizingMaskIntoConstraints = false
  31.         webView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
  32.         webView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
  33.         webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
  34.         webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  35.        
  36.         if let url = URL(string: "https://demo.morphcast.com/native-app-webview/webview_index.html") {
  37.             webView.load(URLRequest(url: url))
  38.         }
  39.     }
  40.    
  41. }
  42.  
  43. extension WebViewController: WKScriptMessageHandler {
  44.     func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  45.         if message.name == "camera", let dict = message.body as? NSDictionary {
  46.             if let maxSize = dict["value"] as? Int {
  47.                 onCameraFrame(maxSize: maxSize)
  48.             }
  49.         }
  50.         if message.name == "data", let dict = message.body as? NSDictionary {
  51.             if let type = dict["type"] as? String, let value = dict["value"] as? String {
  52.                 onData(type: type, value: value)
  53.             }
  54.         }
  55.     }
  56.    
  57.     // JS Callbacks
  58.     private func onCameraFrame(maxSize: Int) {
  59.         /*
  60.         App developer shall implement the following behaviour:
  61.        
  62.         1) Retrieve a frame from camera
  63.        
  64.         * We suggest to resize frames before passing them to the WebView and to encode them in base64 format
  65.         * Strings are processed faster than binary data through the JavaScript Interface
  66.  
  67.         2) resize it to maxSize x maxSize (maintaining the aspect ratio)
  68.         3) Convert the frame to Base64
  69.         4) return the Base64 String
  70.         */
  71.        
  72.         let base64Image = "..."
  73.         let js = "resolveFrame(\(base64Image))"
  74.         webView.evaluateJavaScript(js, completionHandler: nil)
  75.     }
  76.    
  77.     private func onData(type: String, value: String) {
  78.         /*
  79.          type: String
  80.          ["AGE","GENDER","EMOTION","FEATURES","POSE","AROUSAL_VALENCE","ATTENTION"]
  81.          value: Json-stringified of the result
  82.          
  83.          App developer can use the values returned from the webview to implement the desired behavior (e.g update the App view, send data to a custom db ...)
  84.         */
  85.     }
  86.    
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement