CCLoginWeb.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. public enum enumLoginType : Int {
  17. case loginAdd = 0, loginAddForced = 1, loginModifyPasswordUser = 2
  18. }
  19. func presentModalWithDefaultTheme(_ vc: UIViewController) {
  20. self.viewController = vc
  21. let webVC = SwiftModalWebVC(urlString: k_loginBaseUrl, theme: .loginWeb)
  22. webVC.delegateWeb = self
  23. vc.present(webVC, animated: true, completion: nil)
  24. }
  25. }
  26. extension CCLoginWeb: SwiftModalWebVCDelegate {
  27. public func didStartLoading() {
  28. print("Started loading.")
  29. }
  30. public func didReceiveServerRedirectForProvisionalNavigation(url: URL) {
  31. let urlString: String = url.absoluteString
  32. if (urlString.contains(k_webLoginAutenticationProtocol) == true) {
  33. let keyValue = url.path.components(separatedBy: "&")
  34. if (keyValue.count == 3) {
  35. let serverUrl : String = String(keyValue[0].replacingOccurrences(of: "/server:", with: "").characters.dropLast())
  36. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "")
  37. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  38. let account : String = "\(username) \(serverUrl)"
  39. CCCoreData.deleteAccount(account)
  40. CCCoreData.addAccount(account, url: serverUrl, user: username, password: password)
  41. let tableAccount : TableAccount = CCCoreData.setActiveAccount(account)
  42. if (tableAccount.account == account) {
  43. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activePassword: password)
  44. self.delegate?.loginSuccess(0)
  45. self.viewController?.dismiss(animated: true, completion: nil)
  46. }
  47. }
  48. }
  49. }
  50. public func didFinishLoading(success: Bool, url: URL) {
  51. print("Finished loading. Success: \(success).")
  52. }
  53. }