SwiftWebVCActivityChrome.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // SwiftWebVCActivityChrome.swift
  3. //
  4. // Created by Myles Ringle on 24/06/2015.
  5. // Transcribed from code used in SVWebViewController.
  6. // Copyright (c) 2015 Myles Ringle & Sam Vermette. All rights reserved.
  7. //
  8. import UIKit
  9. class SwiftWebVCActivityChrome : SwiftWebVCActivity {
  10. override var activityTitle : String {
  11. return NSLocalizedString("Open in Chrome", tableName: "SwiftWebVC", comment: "")
  12. }
  13. override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
  14. for activityItem in activityItems {
  15. if activityItem is URL, UIApplication.shared.canOpenURL(URL(string: "googlechrome://")!) {
  16. return true;
  17. }
  18. }
  19. return false;
  20. }
  21. override func perform() {
  22. let inputURL: URL! = URLToOpen! as URL
  23. let scheme: String! = inputURL.scheme
  24. // Replace the URL Scheme with the Chrome equivalent.
  25. var chromeScheme: String? = nil;
  26. if scheme == "http" {
  27. chromeScheme = "googlechrome"
  28. }
  29. else if scheme == "https" {
  30. chromeScheme = "googlechromes"
  31. }
  32. // Proceed only if a valid Google Chrome URI Scheme is available.
  33. if chromeScheme != nil {
  34. let absoluteString: NSString! = inputURL!.absoluteString as NSString
  35. let rangeForScheme: NSRange! = absoluteString.range(of: ":")
  36. let urlNoScheme: String! = absoluteString.substring(from: rangeForScheme.location)
  37. let chromeURLString: String! = chromeScheme!+urlNoScheme
  38. let chromeURL: URL! = URL(string: chromeURLString)
  39. // Open the URL with Chrome.
  40. UIApplication.shared.open(chromeURL, options: [:], completionHandler: nil)
  41. }
  42. }
  43. }