瀏覽代碼

SwiftWebVC

Marino Faggiana 8 年之前
父節點
當前提交
8518e54e63

+ 47 - 8
iOSClient/Library/SwiftWebVC/SwiftModalWebVC.swift

@@ -8,10 +8,17 @@
 
 import UIKit
 
+public protocol SwiftModalWebVCDelegate: class {
+    func didStartLoading()
+    func didFinishLoading(success: Bool, url: URL)
+}
+
 public class SwiftModalWebVC: UINavigationController {
     
+    public weak var delegateWeb: SwiftModalWebVCDelegate?
+    
     public enum SwiftModalWebVCTheme {
-        case lightBlue, lightBlack, dark
+        case lightBlue, lightBlack, dark, customLoginWeb
     }
     
     weak var webViewDelegate: UIWebViewDelegate? = nil
@@ -24,6 +31,10 @@ public class SwiftModalWebVC: UINavigationController {
         self.init(pageURL: URL(string: urlString)!, theme: theme)
     }
     
+    public convenience init(urlString: String, theme: SwiftModalWebVCTheme, color : UIColor) {
+        self.init(pageURL: URL(string: urlString)!, theme: theme, color: color)
+    }
+    
     public convenience init(pageURL: URL) {
         self.init(request: URLRequest(url: pageURL))
     }
@@ -31,11 +42,16 @@ public class SwiftModalWebVC: UINavigationController {
     public convenience init(pageURL: URL, theme: SwiftModalWebVCTheme) {
         self.init(request: URLRequest(url: pageURL), theme: theme)
     }
+   
+    public convenience init(pageURL: URL, theme: SwiftModalWebVCTheme, color : UIColor) {
+        self.init(request: URLRequest(url: pageURL), theme: theme, color: color)
+    }
     
-    public init(request: URLRequest, theme: SwiftModalWebVCTheme = .dark) {
+    public init(request: URLRequest, theme: SwiftModalWebVCTheme = .dark, color : UIColor = UIColor.clear) {
+        
         let webViewController = SwiftWebVC(aRequest: request)
         webViewController.storedStatusColor = UINavigationBar.appearance().barStyle
-
+        
         let doneButton = UIBarButtonItem(image: SwiftWebVC.bundledImage(named: "SwiftWebVCDismiss"),
                                          style: UIBarButtonItemStyle.plain,
                                          target: webViewController,
@@ -57,15 +73,23 @@ public class SwiftModalWebVC: UINavigationController {
             webViewController.buttonColor = UIColor.white
             webViewController.titleColor = UIColor.groupTableViewBackground
             UINavigationBar.appearance().barStyle = UIBarStyle.black
+        case .customLoginWeb:
+            webViewController.buttonColor = UIColor.white
+            UINavigationBar.appearance().barStyle = UIBarStyle.default
+            UINavigationBar.appearance().tintColor = color
         }
         
-        if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
-            webViewController.navigationItem.leftBarButtonItem = doneButton
-        }
-        else {
-            webViewController.navigationItem.rightBarButtonItem = doneButton
+        if (theme != .customLoginWeb) {
+            if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
+                webViewController.navigationItem.leftBarButtonItem = doneButton
+            }
+            else {
+                webViewController.navigationItem.rightBarButtonItem = doneButton
+            }
         }
+        
         super.init(rootViewController: webViewController)
+        webViewController.delegate = self
     }
     
     override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
@@ -80,3 +104,18 @@ public class SwiftModalWebVC: UINavigationController {
         super.viewWillAppear(false)
     }
 }
+
+extension SwiftModalWebVC: SwiftWebVCDelegate {
+    
+    public func didStartLoading() {
+        self.delegateWeb?.didStartLoading()
+    }
+    
+    public func didFinishLoading(success: Bool) {
+        print("Finished loading. Success: \(success).")
+    }
+    
+    public func didFinishLoading(success: Bool, url: URL) {
+        self.delegateWeb?.didFinishLoading(success: success, url: url)
+    }
+}

+ 10 - 0
iOSClient/Library/SwiftWebVC/SwiftWebVC.swift

@@ -11,6 +11,7 @@ import WebKit
 public protocol SwiftWebVCDelegate: class {
     func didStartLoading()
     func didFinishLoading(success: Bool)
+    func didFinishLoading(success: Bool, url: URL)
 }
 
 public class SwiftWebVC: UIViewController {
@@ -100,6 +101,12 @@ public class SwiftWebVC: UIViewController {
     }
     
     func loadRequest(_ request: URLRequest) {
+        if #available(iOS 9.0, *) {
+            webView.customUserAgent = "Mozilla/5.0 (iOS) Nextcloud-iOS"
+        } else {
+            // Fallback on earlier versions
+            UserDefaults.standard.register(defaults: ["UserAgent": "Mozilla/5.0 (iOS) Nextcloud-iOS"])
+        }
         webView.load(request)
     }
     
@@ -290,8 +297,10 @@ extension SwiftWebVC: WKNavigationDelegate {
     
     public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
         self.delegate?.didFinishLoading(success: true)
+        self.delegate?.didFinishLoading(success: true, url: webView.url!)
         UIApplication.shared.isNetworkActivityIndicatorVisible = false
         
+        
         webView.evaluateJavaScript("document.title", completionHandler: {(response, error) in
             self.navBarTitle.text = response as! String?
             self.navBarTitle.sizeToFit()
@@ -302,6 +311,7 @@ extension SwiftWebVC: WKNavigationDelegate {
     
     public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
         self.delegate?.didFinishLoading(success: false)
+        self.delegate?.didFinishLoading(success: false, url: webView.url!)
         UIApplication.shared.isNetworkActivityIndicatorVisible = false
         updateToolbarItems()
     }

+ 18 - 17
iOSClient/Login/CCLoginWeb.swift

@@ -8,32 +8,33 @@
 
 import UIKit
 
-/*
- - (void)goToWebVC:(CCMenuItem *)sender
- {
- if (self.splitViewController.isCollapsed) {
- 
- SwiftWebVC *webVC = [[SwiftWebVC alloc] initWithUrlString:sender.argument];
- [self.navigationController pushViewController:webVC animated:YES];
- 
- } else {
- 
- SwiftModalWebVC *webVC = [[SwiftModalWebVC alloc] initWithUrlString:sender.argument];
- [self presentViewController:webVC animated:YES completion:nil];
- }
- }
-*/
-
 class CCLoginWeb: UIViewController {
 
     override func viewDidLoad() {
         super.viewDidLoad()
     }
     
+    override func didReceiveMemoryWarning() {
+        super.didReceiveMemoryWarning()
+    }
+    
     func presentModalWithDefaultTheme(_ vc: UIViewController) {
-        let webVC = SwiftModalWebVC(urlString: k_loginBaseUrl)
+                
+        let webVC = SwiftModalWebVC(urlString: k_loginBaseUrl, theme: .customLoginWeb, color: UIColor.red)
+        webVC.delegateWeb = self
         vc.present(webVC, animated: true, completion: nil)
     }
 }
 
+extension CCLoginWeb: SwiftModalWebVCDelegate {
+    
+    func didStartLoading() {
+        print("Started loading.")
+    }
+
+    func didFinishLoading(success: Bool, url: URL) {
+        print("Finished loading. Success: \(success).")
+    }
+}
+