NCLoginWeb.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // NCLoginWeb.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/08/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. @objc protocol NCLoginDelegateWeb: class {
  25. func loginSuccess(_: NSInteger)
  26. @objc optional func webDismiss()
  27. }
  28. class NCLoginWeb: UIViewController {
  29. var webView: WKWebView?
  30. @objc var urlBase = ""
  31. @objc var loginType: Int = 0
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. webView = WKWebView(frame: CGRect.zero)
  35. webView!.navigationDelegate = self
  36. view.addSubview(webView!)
  37. webView!.translatesAutoresizingMaskIntoConstraints = false
  38. webView!.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  39. webView!.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
  40. webView!.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
  41. webView!.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  42. // ADD k_flowEndpoint for Web Flow
  43. if NCBrandOptions.sharedInstance.use_login_web_personalized == false && urlBase != NCBrandOptions.sharedInstance.linkloginPreferredProviders {
  44. urlBase = urlBase + k_flowEndpoint
  45. }
  46. loadWebPage(webView: webView!, url: URL(string: urlBase)!)
  47. }
  48. func loadWebPage(webView: WKWebView, url: URL) {
  49. let language = NSLocale.preferredLanguages[0] as String
  50. var request = URLRequest(url: url)
  51. request.setValue(CCUtility.getUserAgent(), forHTTPHeaderField: "User-Agent")
  52. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  53. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  54. webView.load(request)
  55. }
  56. }
  57. extension NCLoginWeb: WKNavigationDelegate {
  58. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  59. if let serverTrust = challenge.protectionSpace.serverTrust {
  60. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  61. } else {
  62. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  63. }
  64. }
  65. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  66. print("didStartProvisionalNavigation");
  67. }
  68. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  69. print("didReceiveServerRedirectForProvisionalNavigation");
  70. }
  71. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  72. NCUtility.sharedInstance.stopActivityIndicator()
  73. }
  74. }