AppDelegate.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // AppDelegate.m
  3. // kneet2
  4. //
  5. // Created by Created by Jason Lee on 10/1/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "AppDelegate.h"
  10. @interface AppDelegate ()
  11. @end
  12. // Constants
  13. // static NSString * const kSomeLocalConstant = @"SomeValue";
  14. @implementation AppDelegate
  15. @synthesize window = _window;
  16. #pragma mark -
  17. #pragma mark Static methods
  18. #pragma mark -
  19. #pragma mark Init and dealloc
  20. #pragma mark -
  21. #pragma mark Properties
  22. #pragma mark -
  23. #pragma mark Public methods
  24. #pragma mark -
  25. #pragma mark Private methods
  26. #pragma mark -
  27. #pragma mark Delegate methods
  28. #pragma mark UIApplicationDelegate
  29. - (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWithOptions:(__unused NSDictionary *)launchOptions {
  30. #ifndef PRODUCT_MODE //운영모드가 아닐 경우,
  31. [self checkLaunchOptions:launchOptions];
  32. #endif
  33. application.applicationIconBadgeNumber = 0;
  34. [self initializeApp];
  35. [self initRemoteNotification:launchOptions];
  36. // UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"IntroViewController" storyboardName:@"Main"];
  37. //
  38. //
  39. // self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  40. // self.window.rootViewController = vc;
  41. //
  42. // [self.window makeKeyAndVisible];
  43. return YES;
  44. }
  45. #pragma mark Handlers
  46. - (void)initializeApp {
  47. //set logger
  48. [DDLog addLogger:[DDASLLogger sharedInstance]];
  49. [DDLog addLogger:[DDTTYLogger sharedInstance]];
  50. [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
  51. }
  52. #pragma mark - Initialize Application
  53. - (void)checkLaunchOptions:(id)launchOptions {
  54. if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
  55. NSLog(@"UIApplicationLaunch %@", [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]);
  56. [[JDFacade facade] fireLocalNotification:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
  57. }
  58. if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]) {
  59. NSLog(@"UIApplicationLaunch %@", [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]);
  60. [[JDFacade facade] fireLocalNotification:@"UIApplicationLaunchOptionsLocalNotificationKey"];
  61. }
  62. }
  63. #pragma mark - APNS Setting
  64. /**
  65. APNS 초기화
  66. */
  67. - (void)initRemoteNotification:(NSDictionary *)launchOptions {
  68. // APNS에 디바이스를 등록한다.
  69. #if TARGET_IPHONE_SIMULATOR
  70. [JDFacade facade].APNSToken = @"IPHONE SIMULATOR";
  71. #else
  72. UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
  73. UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
  74. [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
  75. #endif
  76. if (launchOptions) {
  77. NSDictionary *pushInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  78. if (pushInfo) {
  79. [self handlePushNotification:pushInfo];
  80. }
  81. }
  82. }
  83. // 디바이스 토큰 받는곳
  84. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  85. NSMutableString *deviceId = [NSMutableString string];
  86. const unsigned char *ptr = (const unsigned char*) [deviceToken bytes];
  87. for(int i = 0 ; i < 32 ; i++) {
  88. [deviceId appendFormat:@"%02x", ptr[i]];
  89. }
  90. //메모리에 저장.
  91. [JDFacade facade].APNSToken = deviceId;
  92. NSLog(@"APNS Device Token: %@", deviceId);
  93. }
  94. // 서버에 등록 실패했을 경우.
  95. - (void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
  96. {
  97. NSLog(@"didFailToRegisterForRemoteNotifications: %@", error);
  98. }
  99. // 푸시 처리
  100. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
  101. [self handlePushNotification:userInfo];
  102. }
  103. - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
  104. [application registerForRemoteNotifications];
  105. }
  106. - (void)handlePushNotification:(NSDictionary *)pushInfo {
  107. NSLog(@"%s %@", __PRETTY_FUNCTION__, pushInfo);
  108. [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  109. NSString *message = nil;
  110. // NSString *pushTypeCode = [pushInfo objectForKey:@"push_type_code"];
  111. // if (pushTypeCode) {
  112. //
  113. // message = [[[pushInfo objectForKey:@"aps"] objectForKey:@"alert"] objectForKey:@"body"];
  114. // NSString *homegrpId = [pushInfo objectForKey:@"homegrp_id"];
  115. // [self handleWithPushTypeCode:pushTypeCode dummyCode:homegrpId message:message];
  116. //
  117. // return;
  118. // } else {
  119. // }
  120. message = [[pushInfo objectForKey:@"aps"] objectForKey:@"alert"];
  121. if (message && ![message isEmptyString]) {
  122. [[JDFacade facade] alert:message];
  123. }
  124. }
  125. #pragma mark - Application Events
  126. - (void)applicationWillResignActive:(UIApplication *)application {
  127. // 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.
  128. // 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.
  129. }
  130. - (void)applicationDidEnterBackground:(UIApplication *)application {
  131. // 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.
  132. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  133. }
  134. - (void)applicationWillEnterForeground:(UIApplication *)application {
  135. // 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.
  136. application.applicationIconBadgeNumber = 0;
  137. }
  138. - (void)applicationDidBecomeActive:(UIApplication *)application {
  139. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  140. application.applicationIconBadgeNumber = 0;
  141. NSLog(@"%s\n %zd : 0=active, 1=inactive, 2=background", __PRETTY_FUNCTION__, [UIApplication sharedApplication].applicationState);
  142. if ([JDFacade facade].loginUser) {//현재 로그인 되있는 경우,
  143. //초대 내역 갱신.
  144. NSInteger invitationCount = [JDFacade facade].loginUser.invitationList.count;
  145. [[JDFacade facade] updateLoginInfo:^{//로그인 정보 갱신,
  146. if ([JDFacade facade].loginUser.invitationList.count > invitationCount) {
  147. [[JDFacade facade] loadInvitationView];
  148. } else {
  149. [[JDFacade facade] checkDefaultHome];
  150. }
  151. }];
  152. }
  153. }
  154. - (void)applicationWillTerminate:(UIApplication *)application {
  155. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  156. }
  157. @end