added iOS source code
[wl-app.git] / iOS / Pods / OAuthSwift / Sources / OAuthWebViewController.swift
1 //
2 //  OAuthWebViewController.swift
3 //  OAuthSwift
4 //
5 //  Created by Dongri Jin on 2/11/15.
6 //  Copyright (c) 2015 Dongri Jin. All rights reserved.
7 //
8
9 import Foundation
10
11 #if os(iOS)  || os(tvOS)
12     import UIKit
13     public typealias OAuthViewController = UIViewController
14 #elseif os(watchOS)
15     import WatchKit
16     public typealias OAuthViewController = WKInterfaceController
17 #elseif os(OSX)
18     import AppKit
19     public typealias OAuthViewController = NSViewController
20 #endif
21
22 /// Delegate for OAuthWebViewController
23 public protocol OAuthWebViewControllerDelegate: class {
24
25     #if os(iOS) || os(tvOS)
26     /// Did web view presented (work only without navigation controller)
27     func oauthWebViewControllerDidPresent()
28     /// Did web view dismiss (work only without navigation controller)
29     func oauthWebViewControllerDidDismiss()
30     #endif
31
32     func oauthWebViewControllerWillAppear()
33     func oauthWebViewControllerDidAppear()
34     func oauthWebViewControllerWillDisappear()
35     func oauthWebViewControllerDidDisappear()
36 }
37
38 /// A web view controller, which handler OAuthSwift authentification.
39 open class OAuthWebViewController: OAuthViewController, OAuthSwiftURLHandlerType {
40
41     #if os(iOS) || os(tvOS) || os(OSX)
42     /// Delegate for this view
43     public weak var delegate: OAuthWebViewControllerDelegate?
44     #endif
45
46     #if os(iOS) || os(tvOS)
47     /// If controller have an navigation controller, application top view controller could be used if true
48     public var useTopViewControlerInsteadOfNavigation = false
49
50     /// Set false to disable present animation.
51     public var presentViewControllerAnimated = true
52     /// Set false to disable dismiss animation.
53     public var dismissViewControllerAnimated = true
54
55     public var topViewController: UIViewController? {
56         #if !OAUTH_APP_EXTENSIONS
57             return UIApplication.topViewController
58         #else
59             return nil
60         #endif
61     }
62     #elseif os(OSX)
63     /// How to present this view controller if parent view controller set
64     public enum Present {
65         case asModalWindow
66         case asSheet
67         case asPopover(relativeToRect: NSRect, ofView : NSView, preferredEdge: NSRectEdge, behavior: NSPopover.Behavior)
68         case transitionFrom(fromViewController: NSViewController, options: NSViewController.TransitionOptions)
69         case animator(animator: NSViewControllerPresentationAnimator)
70         case segue(segueIdentifier: String)
71     }
72     public var present: Present = .asModalWindow
73     #endif
74
75     open func handle(_ url: URL) {
76         // do UI in main thread
77         OAuthSwift.main { [unowned self] in
78              self.doHandle(url)
79         }
80     }
81
82     #if os(watchOS)
83     public static var userActivityType: String = "org.github.dongri.oauthswift.connect"
84     #endif
85
86     open func doHandle(_ url: URL) {
87         #if os(iOS) || os(tvOS)
88             let completion: () -> Void = { [unowned self] in
89                 self.delegate?.oauthWebViewControllerDidPresent()
90             }
91             if let navigationController = self.navigationController, (!useTopViewControlerInsteadOfNavigation || self.topViewController == nil) {
92                 navigationController.pushViewController(self, animated: presentViewControllerAnimated)
93             } else if let p = self.parent {
94                 p.present(self, animated: presentViewControllerAnimated, completion: completion)
95             } else if let topViewController = topViewController {
96                 topViewController.present(self, animated: presentViewControllerAnimated, completion: completion)
97             } else {
98                 // assert no presentation
99                 assertionFailure("Failed to present. Maybe add a parent")
100             }
101         #elseif os(watchOS)
102             if url.scheme == "http" || url.scheme == "https" {
103                 self.updateUserActivity(OAuthWebViewController.userActivityType, userInfo: nil, webpageURL: url)
104             }
105         #elseif os(OSX)
106             if let p = self.parent { // default behaviour if this controller affected as child controller
107                 switch self.present {
108                 case .asSheet:
109                     p.presentViewControllerAsSheet(self)
110                 case .asModalWindow:
111                     p.presentViewControllerAsModalWindow(self)
112                     // FIXME: if we present as window, window close must detected and oauthswift.cancel() must be called...
113                 case .asPopover(let positioningRect, let positioningView, let preferredEdge, let behavior):
114                     p.presentViewController(self, asPopoverRelativeTo: positioningRect, of: positioningView, preferredEdge: preferredEdge, behavior: behavior)
115                 case .transitionFrom(let fromViewController, let options):
116                     let completion: () -> Void = { /*[unowned self] in*/
117                         //self.delegate?.oauthWebViewControllerDidPresent()
118                     }
119                     p.transition(from: fromViewController, to: self, options: options, completionHandler: completion)
120                 case .animator(let animator):
121                     p.presentViewController(self, animator: animator)
122                 case .segue(let segueIdentifier):
123                     p.performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: segueIdentifier), sender: self) // The segue must display self.view
124                 }
125             } else if let window = self.view.window {
126                 window.makeKeyAndOrderFront(nil)
127             } else {
128                 assertionFailure("Failed to present. Add controller into a window or add a parent")
129             }
130             // or create an NSWindow or NSWindowController (/!\ keep a strong reference on it)
131         #endif
132     }
133
134     open func dismissWebViewController() {
135         #if os(iOS) || os(tvOS)
136             let completion: () -> Void = { [unowned self] in
137                 self.delegate?.oauthWebViewControllerDidDismiss()
138             }
139             if let navigationController = self.navigationController, (!useTopViewControlerInsteadOfNavigation || self.topViewController == nil) {
140                 navigationController.popViewController(animated: dismissViewControllerAnimated)
141             } else if let parentViewController = self.parent {
142                 // The presenting view controller is responsible for dismissing the view controller it presented
143                 parentViewController.dismiss(animated: dismissViewControllerAnimated, completion: completion)
144             } else if let topViewController = topViewController {
145                 topViewController.dismiss(animated: dismissViewControllerAnimated, completion: completion)
146             } else {
147                 // keep old code...
148                 self.dismiss(animated: dismissViewControllerAnimated, completion: completion)
149             }
150         #elseif os(watchOS)
151             self.dismiss()
152         #elseif os(OSX)
153             if self.presenting != nil {
154                 self.dismiss(nil)
155                 if self.parent != nil {
156                     self.removeFromParentViewController()
157                 }
158             } else if let window = self.view.window {
159                 window.performClose(nil)
160             }
161         #endif
162     }
163
164     // MARK: overrides
165     #if os(iOS) || os(tvOS)
166     open override func viewWillAppear(_ animated: Bool) {
167         self.delegate?.oauthWebViewControllerWillAppear()
168     }
169     open override func viewDidAppear(_ animated: Bool) {
170         self.delegate?.oauthWebViewControllerDidAppear()
171     }
172     open override func viewWillDisappear(_ animated: Bool) {
173         self.delegate?.oauthWebViewControllerWillDisappear()
174     }
175     open override func viewDidDisappear(_ animated: Bool) {
176         self.delegate?.oauthWebViewControllerDidDisappear()
177     }
178     #elseif os(OSX)
179     open override func viewWillAppear() {
180         self.delegate?.oauthWebViewControllerWillAppear()
181     }
182     open override func viewDidAppear() {
183         self.delegate?.oauthWebViewControllerDidAppear()
184     }
185     open override func viewWillDisappear() {
186         self.delegate?.oauthWebViewControllerWillDisappear()
187     }
188     open override func viewDidDisappear() {
189         self.delegate?.oauthWebViewControllerDidDisappear()
190     }
191
192     #endif
193 }