ActionViewController.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // ActionViewController.swift
  3. // send-ios-action-extension
  4. //
  5. // Created by Donovan Preston on 7/26/18.
  6. //
  7. import UIKit
  8. import WebKit
  9. import MobileCoreServices
  10. var typesToLoad = [("com.adobe.pdf", "application/pdf"), ("public.png", "image/png"),
  11. ("public.jpeg", "image/jpeg"), ("public.jpeg-2000", "image/jp2"),
  12. ("com.compuserve.gif", "image/gif"), ("com.microsoft.bmp", "image/bmp"),
  13. ("public.plain-text", "text/plain")]
  14. class ActionViewController: UIViewController, WKScriptMessageHandler {
  15. @IBOutlet var webView: WKWebView!
  16. var typeToSend: String?
  17. var dataToSend: Data?
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. self.webView.frame = self.view.bounds
  21. self.webView?.configuration.userContentController.add(self, name: "loaded")
  22. self.webView?.configuration.userContentController.add(self, name: "copy")
  23. if let url = Bundle.main.url(
  24. forResource: "index",
  25. withExtension: "html",
  26. subdirectory: "assets") {
  27. self.webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
  28. }
  29. // Get the item[s] we're handling from the extension context.
  30. for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
  31. for provider in item.attachments! as! [NSItemProvider] {
  32. for (type, mimeType) in typesToLoad {
  33. if provider.hasItemConformingToTypeIdentifier(type) {
  34. provider.loadDataRepresentation(forTypeIdentifier: type, completionHandler: { (data, error) in
  35. OperationQueue.main.addOperation {
  36. self.typeToSend = mimeType
  37. self.dataToSend = data
  38. }
  39. })
  40. return
  41. }
  42. }
  43. }
  44. }
  45. }
  46. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  47. print("Message received: \(message.name) with body: \(message.body)")
  48. if (message.name == "loaded") {
  49. let stringToSend = "window.sendBase64EncodedFromSwift('data:\(self.typeToSend ?? "application/octet-stream");base64,\(self.dataToSend?.base64EncodedString() ?? "")')";
  50. self.webView.evaluateJavaScript(stringToSend) { (object: Any?, error: Error?) -> Void in
  51. print("completed")
  52. }
  53. } else if (message.name == "copy") {
  54. UIPasteboard.general.string = "\(message.body)"
  55. }
  56. }
  57. override func didReceiveMemoryWarning() {
  58. super.didReceiveMemoryWarning()
  59. // Dispose of any resources that can be recreated.
  60. }
  61. @IBAction func done() {
  62. // Return any edited content to the host app.
  63. // This template doesn't do anything, so we just echo the passed in items.
  64. self.extensionContext!.completeRequest(returningItems: self.extensionContext!.inputItems, completionHandler: nil)
  65. }
  66. }