WKWebViewGenerator.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // WKWebViewGenerator.swift
  3. // RichTextView
  4. //
  5. // Created by Ahmed Elkady on 2018-11-08.
  6. // Copyright © 2018 Top Hat. All rights reserved.
  7. //
  8. import WebKit
  9. class WKWebViewGenerator {
  10. // MARK: - Init
  11. private init() {}
  12. // MARK: - Utility Functions
  13. static func getWebView(from input: String) -> WKWebView? {
  14. guard let url = WKWebViewGenerator.getWebViewURL(from: input) else {
  15. return nil
  16. }
  17. let configuration = WKWebViewConfiguration()
  18. configuration.allowsInlineMediaPlayback = true
  19. let webView = WKWebView(frame: .zero, configuration: configuration)
  20. webView.load(URLRequest(url: url))
  21. return webView
  22. }
  23. // MARK: - Private Helpers
  24. private static func getWebViewURL(from input: String) -> URL? {
  25. if let youTubeID = input.getSubstring(inBetween: RichTextViewConstants.youtubeStartTag, and: RichTextViewConstants.videoEndTag) {
  26. return URL(string: "https://www.youtube.com/embed/" + youTubeID + "?playsinline=1&cc_load_policy=1")
  27. } else if let vimeoID = input.getSubstring(inBetween: RichTextViewConstants.vimeoStartTag, and: RichTextViewConstants.videoEndTag) {
  28. return URL(string: "https://player.vimeo.com/video/" + vimeoID + "?title=0&byline=0&portrait=0")
  29. }
  30. return nil
  31. }
  32. }