|
// |
|
// AppDelegate.swift |
|
// NBSwiftTestApp |
|
// |
|
// Copyright (MIT) 2015-present NationBuilder |
|
// |
|
|
|
import UIKit |
|
import NBClient |
|
|
|
@UIApplicationMain |
|
class AppDelegate: UIResponder { |
|
|
|
// MARK: Properties of Interest |
|
|
|
lazy var accountButton: NBAccountButton = { |
|
return NBAccountButton(fromNibWithTarget: self, action: Selector("presentAccountsViewController:")) |
|
}() |
|
|
|
lazy var accountsManager: NBAccountsManager = { |
|
return NBAccountsManager(clientInfo: nil, delegate: self) |
|
}() |
|
|
|
lazy var accountsViewController: NBAccountsViewController = { |
|
let viewController = NBAccountsViewController(nibName: nil, bundle: nil) |
|
viewController.dataSource = self.accountsManager |
|
return viewController |
|
}() |
|
|
|
lazy var testClient: NBClient? = { |
|
if let info = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource(NBInfoFileName, ofType: "plist")!) as? [String: String] { |
|
return NBClient( |
|
nationSlug: info[NBInfoNationSlugKey]!, |
|
apiKey: info[NBInfoTestTokenKey]!, |
|
customBaseURL: NSURL(string: "\(info[NBInfoBaseURLFormatKey])\(info[NBInfoNationSlugKey])"), |
|
customURLSession: nil, customURLSessionConfiguration: nil |
|
) |
|
} |
|
return nil |
|
}() |
|
|
|
// MARK: Other Properties |
|
|
|
lazy var viewController: UIViewController = { |
|
let viewController = UIViewController(nibName: nil, bundle: nil) |
|
viewController.navigationItem.leftBarButtonItem = self.accountButton.barButtonItemWithCompactButtonType(.AvatarOnly) |
|
return viewController |
|
}() |
|
|
|
lazy var window: UIWindow? = { |
|
let window = UIWindow(frame: UIScreen.mainScreen().bounds) |
|
window.rootViewController = UINavigationController(rootViewController: self.viewController) |
|
window.rootViewController?.view.backgroundColor = UIColor.whiteColor() |
|
return window |
|
}() |
|
|
|
// MARK: Actions |
|
|
|
@IBAction func presentAccountsViewController(sender: AnyObject?) { |
|
if let rootViewController = self.window?.rootViewController { |
|
self.accountsViewController.showWithAccountButton(self.accountButton, presentingViewController: rootViewController) |
|
} |
|
} |
|
|
|
} |
|
|
|
// MARK: - NBAuthenticatorPresentationDelegate |
|
|
|
extension AppDelegate: NBAccountsManagerDelegate { |
|
|
|
func presentWebBrowserForAuthenticationWithRedirectPath(webBrowser: SFSafariViewController) { |
|
self.accountsViewController.presentViewController(webBrowser, animated: true, completion: nil) |
|
} |
|
|
|
} |
|
|
|
// MARK: - NBAccountsManagerDelegate |
|
|
|
extension AppDelegate: NBAccountsManagerDelegate { |
|
|
|
func accountsManager(accountsManager: NBAccountsManager, didFailToSwitchToAccount account: NBAccount, withError error: NSError?) { |
|
if let error = error, rootViewController = self.window?.rootViewController { |
|
rootViewController.presentViewController( |
|
UIAlertController.nb_genericAlertWithError(error defaultDismissal: true) |
|
animated: true, completion: nil |
|
) |
|
} |
|
} |
|
|
|
func accountsManager(accountsManager: NBAccountsManager, didSignOutOfInvalidAccount account: NBAccount, fromHTTPError error: NSError) { |
|
self.presentAccountsViewController(nil) |
|
} |
|
|
|
func accountsManager(accountsManager: NBAccountsManager, didSwitchToAccount account: NBAccount?) { |
|
self.accountButton.dataSource = account |
|
} |
|
|
|
} |
|
|
|
// MARK: - UIApplicationDelegate |
|
|
|
extension AppDelegate: UIApplicationDelegate { |
|
|
|
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { |
|
application.nb_loadBundleResources() |
|
self.window?.makeKeyAndVisible() |
|
return true |
|
} |
|
|
|
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool { |
|
NBAuthenticator.finishAuthenticatingInWebBrowserWithURL(url) |
|
return false |
|
} |
|
|
|
} |