ViewController.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //
  2. // ViewController.swift
  3. // send-ios
  4. //
  5. // Created by Donovan Preston on 7/19/18.
  6. //
  7. import UIKit
  8. import WebKit
  9. class ViewController: UIViewController, WKScriptMessageHandler {
  10. @IBOutlet var webView: WKWebView!
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. self.webView.frame = self.view.bounds
  14. self.webView?.configuration.userContentController.add(self, name: "loaded")
  15. self.webView?.configuration.userContentController.add(self, name: "copy")
  16. if let url = Bundle.main.url(
  17. forResource: "index",
  18. withExtension: "html",
  19. subdirectory: "assets") {
  20. webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
  21. }
  22. }
  23. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  24. print("Message received: \(message.name) with body: \(message.body)")
  25. UIPasteboard.general.string = "\(message.body)"
  26. }
  27. override func didReceiveMemoryWarning() {
  28. super.didReceiveMemoryWarning()
  29. // Dispose of any resources that can be recreated.
  30. }
  31. }