CCLoginWeb.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // CCLoginWeb.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/04/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. import UIKit
  9. @objc protocol CCLoginDelegate: class {
  10. func loginSuccess(_: NSInteger)
  11. }
  12. public class CCLoginWeb: UIViewController {
  13. weak var delegate: CCLoginDelegate?
  14. var viewController : UIViewController?
  15. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  16. var loginType : NSInteger = 0
  17. func presentModalWithDefaultTheme(_ vc: UIViewController) {
  18. self.viewController = vc
  19. let webVC = SwiftModalWebVC(urlString: k_loginBaseUrl, theme: .loginWeb)
  20. webVC.delegateWeb = self
  21. vc.present(webVC, animated: true, completion: nil)
  22. }
  23. }
  24. extension CCLoginWeb: SwiftModalWebVCDelegate {
  25. public func didStartLoading() {
  26. print("Started loading.")
  27. }
  28. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  29. let urlString: String = url.absoluteString
  30. if (urlString.contains(k_webLoginAutenticationProtocol) == true) {
  31. let keyValue = url.path.components(separatedBy: "&")
  32. if (keyValue.count == 3) {
  33. let serverUrl : String = String(keyValue[0].replacingOccurrences(of: "/server:", with: "").characters.dropLast())
  34. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "")
  35. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  36. let account : String = "\(username) \(serverUrl)"
  37. CCCoreData.deleteAccount(account)
  38. CCCoreData.addAccount(account, url: serverUrl, user: username, password: password)
  39. let tableAccount : TableAccount = CCCoreData.setActiveAccount(account)
  40. if (tableAccount.account == account) {
  41. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activePassword: password)
  42. self.delegate?.loginSuccess(loginType)
  43. self.viewController?.dismiss(animated: true, completion: nil)
  44. }
  45. }
  46. }
  47. }
  48. public func didFinishLoading(success: Bool, url: URL) {
  49. print("Finished loading. Success: \(success).")
  50. }
  51. }