Skip to content

Instantly share code, notes, and snippets.

@benjiwheeler
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benjiwheeler/a30027fc141622611b74 to your computer and use it in GitHub Desktop.
Save benjiwheeler/a30027fc141622611b74 to your computer and use it in GitHub Desktop.
AppDelegate boilerplate for Parse and FB with push
#import "AppDelegate.h"
#import <Parse/Parse.h>
#import <FacebookSDK/FacebookSDK.h>
#import "MyParseUser.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
NSForegroundColorAttributeName,nil]];
self.window.tintColor = [UIColor blueColor];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
// ****************************************************************************
// Parse config
[MyParseUser registerSubclass];
[Parse setApplicationId:@"insert_here" clientKey:@"insert_here"];
// ****************************************************************************
// Facebook
// ****************************************************************************
[PFFacebookUtils initializeFacebook];
// ****************************************************************************
// track app opens for parse analytics
// NOTE that we do not call [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo] because we don't use ios 7-specific functionality, but maybe we should
if (application.applicationState != UIApplicationStateBackground) {
// Track an app open here if we launch with a push, unless
// "content_available" was used to trigger a background push (introduced
// in iOS 7). In that case, we skip tracking here to avoid double
// counting the app-open.
BOOL preBackgroundPush = ![application respondsToSelector:@selector(backgroundRefreshStatus)];
BOOL oldPushHandlerOnly = ![self respondsToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)];
BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
}
}
[MyParseUser ensureHasFBInfoWithCompletion:^(BOOL success, id responseOrError) {
if (success) {
// can go right to normal app view here, user is already logged in
// go to push notification if there is one
if (launchOptions != nil) {
// Launched from push notification
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[self handlePush:notification forState:UIApplicationStateInactive];
}
} else {
// the user isn't logged in
}
}];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation addUniqueObject:@"global" forKey:@"channels"];
[currentInstallation saveEventually:nil];
DLog(@"Registered for push");
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
if (error.code == 3010) {
DLog(@"Push notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register.
DLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification {
DLog(@"didReceiveRemoteNotification got push with dictionary: %@", notification);
// record for parse analytics
if (application.applicationState == UIApplicationStateInactive) {
// The application was just brought from the background to the foreground,
// so we consider the app as having been "opened by a push notification."
[PFAnalytics trackAppOpenedWithRemoteNotificationPayload:notification];
}
[self handlePush:notification forState:application.applicationState];
}
- (UIViewController *)visibleViewController
{
UIViewController *rootViewController = self.window.rootViewController;
return [self getTopOrPresentedViewController:rootViewController];
}
// elegant but confusing: recursively goes DOWN the hierarchy of viewcontrollers to get the one that's in front
- (UIViewController*)getTopOrPresentedViewController:(UIViewController*)vc {
// if we have other top level view controller types, we should list them here
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self getTopOrPresentedViewController:[[(UINavigationController*)vc viewControllers] lastObject]];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return [self getTopOrPresentedViewController:[(UITabBarController*)vc selectedViewController]];
} else if ([vc presentedViewController]) {
return [self getTopOrPresentedViewController:[vc presentedViewController]];
}
return vc;
}
- (void)handlePush:(NSDictionary *)notification forState:(UIApplicationState)applicationState
{
UIViewController *foregroundVC = [self visibleViewController];
ViewControllerEnum foregroundVCType = [AppDelegate getViewControllerType:foregroundVC];
switch (applicationState) {
case UIApplicationStateActive:
// app was already in the foreground; refresh group if we're in that view
switch (foregroundVCType) {
// here we can list all of the different UIViewController subclasses that might be. eg:
/*
case ViewControllerEnumCongratulations:
[self showBannerForPush:notification];
[(CongratulationsViewController*)foregroundVC reloadMessagesFromServer];
break;
*/
case ViewControllerEnumUnknown:
default:
[self showBannerForPush:notification];
// do nothing else
break;
}
break;
case UIApplicationStateBackground:
// app was just brought from background to foreground
switch (foregroundVCType) {
// list cases here
case ViewControllerEnumUnknown:
default:
// do nothing, don't show a banner
break;
}
break;
case UIApplicationStateInactive:
// aka currently launching
// probably just do nothing
break;
default:
// do nothing
break;
}
}
+ (ViewControllerEnum)getViewControllerType:(UIViewController*)vc
{
// determine enum version of the vc, like:
/*
if ([vc isKindOfClass:[CongratulationsViewController class]]) {
DLog(@"Foreground: CongratulationsViewController");
return TYViewControllerEnumCongratulationsController;
} else if ...
*/
DLog(@"Foreground: some other vc");
return ViewControllerEnumUnknown;
}
- (void)showBannerForPush:(NSDictionary*)notification {
NSString *pushMessage = notification[@"aps"][@"alert"];
DLog(@"can show banner for push: %@", pushMessage);
// here is where you actually display the push notification
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication withSession:[PFFacebookUtils session]];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBAppCall handleDidBecomeActiveWithSession:[PFFacebookUtils session]];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment