Skip to content

Instantly share code, notes, and snippets.

@hlfcoding
Last active March 17, 2016 03:13
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/0cf43f0dc72cc99e2ca6 to your computer and use it in GitHub Desktop.
Save hlfcoding/0cf43f0dc72cc99e2ca6 to your computer and use it in GitHub Desktop.
Compact Example Using NationBuilder iOS SDK
//
// NBAppDelegate.m
// NBTestApp
//
// Copyright (MIT) 2014-present NationBuilder
//
#import "NBAppDelegate.h"
#import <NBClient/Main.h>
#import <NBClient/UI.h>
@interface NBAppDelegate () <NBAccountsManagerDelegate, NBAuthenticatorPresentationDelegate>
@property (nonatomic) NBClient *testClient;
@property (nonatomic) NBAccountButton *accountButton;
@property (nonatomic) NBAccountsManager *accountsManager;
@property (nonatomic) NBAccountsViewController *accountsViewController;
@property (nonatomic) UIViewController *viewController;
- (IBAction)presentAccountsViewController:(id)sender;
@end
@implementation NBAppDelegate
#pragma mark Properties of Interest
- (NBAccountButton *)accountButton
{
if (!_accountButton) {
self.accountButton = [NBAccountButton accountButtonFromNibWithTarget:self action:@selector(presentAccountsViewController:)];
}
return _accountButton;
}
- (NBAccountsManager *)accountsManager
{
if (!_accountsManager) {
self.accountsManager = [[NBAccountsManager alloc] initWithClientInfo:nil delegate:self];
}
return _accountsManager;
}
- (NBAccountsViewController *)accountsViewController
{
if (!_accountsViewController) {
self.accountsViewController = [[NBAccountsViewController alloc] initWithNibName:nil bundle:nil];
self.accountsViewController.dataSource = self.accountsManager;
}
return _accountsViewController;
}
- (NBClient *)testClient
{
if (!_testClient) {
NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:NBInfoFileName ofType:@"plist"]];
self.testClient = [[NBClient alloc] initWithNationSlug:info[NBInfoNationSlugKey]
apiKey:info[NBInfoTestTokenKey]
customBaseURL:[NSURL URLWithString:[NSString stringWithFormat:
info[NBInfoBaseURLFormatKey],
info[NBInfoNationSlugKey]]]
customURLSession:nil customURLSessionConfiguration:nil];
}
return _testClient;
}
#pragma mark Other Properties
- (UIViewController *)viewController
{
if (!_viewController) {
self.viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
self.viewController.navigationItem.leftBarButtonItem = [self.accountButton barButtonItemWithCompactButtonType:NBAccountButtonTypeAvatarOnly];
}
return _viewController;
}
- (UIWindow *)window
{
if (!_window) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController.view.backgroundColor = [UIColor whiteColor];
}
return _window;
}
#pragma mark - Actions
- (IBAction)presentAccountsViewController:(id)sender
{
[self.accountsViewController showWithAccountButton:self.accountButton
presentingViewController:self.window.rootViewController];
}
#pragma mark - NBAuthenticatorPresentationDelegate
- (void)presentWebBrowserForAuthenticationWithRedirectPath:(SFSafariViewController *)webBrowser
{
[self.accountsViewController presentViewController:(UIViewController *)webBrowser animated:YES completion:nil];
}
#pragma mark - NBAccountsManagerDelegate
- (void)accountsManager:(NBAccountsManager *)accountsManager didFailToSwitchToAccount:(NBAccount *)account withError:(NSError *)error
{
[self.window.rootViewController presentViewController:[UIAlertController nb_genericAlertWithError:error defaultDismissal:YES]
animated:YES completion:nil];
}
- (void)accountsManager:(NBAccountsManager *)accountsManager didSignOutOfInvalidAccount:(NBAccount *)account fromHTTPError:(NSError *)error
{
[self presentAccountsViewController:nil];
}
- (void)accountsManager:(NBAccountsManager *)accountsManager didSwitchToAccount:(NBAccount *)account
{
self.accountButton.dataSource = account;
}
#pragma mark - UIAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application nb_loadBundleResources];
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[NBAuthenticator finishAuthenticatingInWebBrowserWithURL:url];
return NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment