CCLoginWeb.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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: .custom, color: Constant.GlobalConstants.k_Color_NavigationBar, colorText: Constant.GlobalConstants.k_Color_NavigationBar_Text)
  20. webVC.delegateWeb = self
  21. vc.present(webVC, animated: false, 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 && (loginType == 0 || loginType == 1)) {
  31. let keyValue = url.path.components(separatedBy: "&")
  32. if (keyValue.count == 3) {
  33. if (keyValue[0].contains("server:") && keyValue[1].contains("user:") && keyValue[2].contains("password:")) {
  34. var serverUrl : String = keyValue[0].replacingOccurrences(of: "/server:", with: "")
  35. if (serverUrl.characters.last == "/") {
  36. serverUrl = String(serverUrl.characters.dropLast())
  37. }
  38. let username : String = keyValue[1].replacingOccurrences(of: "user:", with: "")
  39. let password : String = keyValue[2].replacingOccurrences(of: "password:", with: "")
  40. let account : String = "\(username) \(serverUrl)"
  41. CCCoreData.deleteAccount(account)
  42. CCCoreData.addAccount(account, url: serverUrl, user: username, password: password)
  43. let tableAccount : TableAccount = CCCoreData.setActiveAccount(account)
  44. if (tableAccount.account == account) {
  45. appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activePassword: password)
  46. self.delegate?.loginSuccess(loginType)
  47. self.viewController?.dismiss(animated: true, completion: nil)
  48. }
  49. }
  50. }
  51. }
  52. }
  53. public func didFinishLoading(success: Bool, url: URL) {
  54. print("Finished loading. Success: \(success).")
  55. }
  56. }