Skip to content

Instantly share code, notes, and snippets.

@hlfcoding
Last active March 17, 2016 03:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlfcoding/fa2052a3c822792a6d20 to your computer and use it in GitHub Desktop.
Save hlfcoding/fa2052a3c822792a6d20 to your computer and use it in GitHub Desktop.
Using NationBuilder iOS SDK with Swift

Using NationBuilder iOS SDK with Swift

Additional Steps

  1. Put use_frameworks! in your Podfile. Use pod 'NBClient', '~> 1.3.0'. Re-run Cocoapods.
  2. Add bridging header to your project. See guide.
  3. Refer to the same AppDelegate source to continue.

Caveats

  • Xcode autocomplete for NBClient symbols may be unreliable.
//
// 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
}
}
//
// NBClient-Bridging-Header.h
// NBSwiftTestApp
//
// Copyright (MIT) 2015-present NationBuilder
//
#ifndef NBSwiftTestApp_NBClient_Bridging_Header_h
#define NBSwiftTestApp_NBClient_Bridging_Header_h
#import <NBClient/Main.h>
#import <NBClient/UI.h>
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment