dkahgld

AppDelegate.m 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // AppDelegate.m
  3. // ZBProject
  4. //
  5. // Created by 学丽 on 2019/3/26.
  6. // Copyright © 2019 ZB. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "ZBTabController.h"
  10. @interface AppDelegate ()
  11. @end
  12. @implementation AppDelegate
  13. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  14. [self initWindows];
  15. [self getUserInfo];
  16. [self setUM];
  17. [self setupMiPush];
  18. [self getRemoteNotificationDictWithDict:launchOptions];
  19. [self cancelIconBadgeNumber];
  20. return YES;
  21. }
  22. #pragma mark---获取用户信息
  23. -(void)getUserInfo
  24. {
  25. if ([AccountTool isLogin]) {
  26. [ZBHTTP post:getUserInfoURL params:nil success:^(id _Nonnull json) {
  27. NSDictionary *dics =(NSDictionary *)json[@"data"];
  28. AccountModel *oldM =[AccountTool account];
  29. oldM.name = dics[@"name"];
  30. oldM.img=dics[@"img"];
  31. [AccountTool saveAccount:oldM];
  32. } failure:^(NSError * _Nonnull error) {
  33. }];
  34. }
  35. }
  36. - (void)getRemoteNotificationDictWithDict:(NSDictionary *)tfdic {
  37. NSDictionary *remoteNotification = [tfdic objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  38. if(remoteNotification){
  39. NSString *messageId;
  40. if ([remoteNotification containsObjectForKey:@"_id_"]) {
  41. messageId = [remoteNotification objectForKey:@"_id_"];
  42. }
  43. if (messageId!=nil) {
  44. [MiPushSDK openAppNotify:messageId];
  45. }
  46. [self jumpViewController:remoteNotification];
  47. }
  48. }
  49. #pragma mark---小米推送
  50. - (void)setupMiPush {
  51. [MiPushSDK registerMiPush:self];
  52. }
  53. #pragma mark--友盟统计
  54. -(void)setUM
  55. {
  56. UMConfigInstance.appKey=UMENG_KEY;
  57. UMConfigInstance.channelId=@"App Store";
  58. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  59. NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
  60. [MobClick setAppVersion:appVersion];
  61. [MobClick startWithConfigure:UMConfigInstance];
  62. [MobClick setLogEnabled:YES];
  63. }
  64. #pragma mark---初始化
  65. -(void)initWindows
  66. {
  67. self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  68. ZBTabController *tabv= [[ZBTabController alloc]init];
  69. ZBSliderViewController *sliv=[[ZBSliderViewController alloc]init];
  70. WBSliderViewController *slider=[[WBSliderViewController alloc]initWithMainViewController:tabv leftViewController:sliv rightViewController:nil];
  71. self.window.rootViewController=slider;
  72. [self.window makeKeyAndVisible];
  73. AccountModel *userModel =[AccountTool account];
  74. if (![AccountTool isLogin] || userModel.has_kwai.integerValue !=1 ) {
  75. [self.window.rootViewController presentViewController:[[UINavigationController alloc]initWithRootViewController:[[ZBLoginTipeViewController alloc]init]] animated:YES completion:nil];
  76. }
  77. }
  78. #pragma mark 注册push服务.
  79. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  80. {
  81. NSLog(@"APNS token: %@", [deviceToken description]);
  82. // 注册APNS成功, 注册deviceToken
  83. [MiPushSDK bindDeviceToken:deviceToken];
  84. }
  85. - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
  86. {
  87. NSLog(@"APNS error: %@", err);
  88. // 注册APNS失败.
  89. // 自行处理.
  90. }
  91. #pragma mark MiPushSDKDelegate
  92. - (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data
  93. {
  94. // 请求成功
  95. // 可在此获取regId
  96. if ([selector isEqualToString:@"bindDeviceToken:"]) {
  97. NSLog(@"regid = %@", data[@"regid"]);
  98. }
  99. }
  100. - (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data
  101. {
  102. // 请求失败
  103. }
  104. #pragma mark Local And Push Notification
  105. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  106. {
  107. NSLog(@"APNS notify: %@", userInfo);
  108. // 当同时启动APNs与内部长连接时, 把两处收到的消息合并. 通过miPushReceiveNotification返回
  109. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  110. }
  111. // iOS10新加入的回调方法
  112. // 应用在前台收到通知
  113. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) {
  114. NSDictionary * userInfo = notification.request.content.userInfo;
  115. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  116. NSLog(@"APNS notify: %@", userInfo);;
  117. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  118. NSString *messageId = [userInfo objectForKey:@"_id_"];
  119. [MiPushSDK openAppNotify:messageId];
  120. }
  121. [self cancelIconBadgeNumber];
  122. }
  123. - (void)cancelIconBadgeNumber {
  124. // 注册显示应用程序BadgeNumber的通知
  125. UIApplication *application = [UIApplication sharedApplication];
  126. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
  127. [application registerUserNotificationSettings:settings];
  128. if (application.applicationIconBadgeNumber > 0) {
  129. application.applicationIconBadgeNumber = 0;
  130. }
  131. }
  132. // 点击通知进入应用
  133. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) {
  134. NSDictionary * userInfo = response.notification.request.content.userInfo;
  135. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  136. NSLog(@"APNS notify: %@", userInfo);
  137. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  138. NSString *messageId = [userInfo objectForKey:@"_id_"];
  139. [MiPushSDK openAppNotify:messageId];
  140. }
  141. [self jumpViewController:userInfo];
  142. completionHandler();
  143. }
  144. - (void)jumpViewController:(NSDictionary *)remoteNotification {
  145. [self cancelIconBadgeNumber];
  146. NSInteger pushType = [remoteNotification[@"pushType"] integerValue];
  147. switch (pushType) {
  148. case 1005://跳转到消息列表
  149. [self pushMessage];
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. #pragma mark---消息列表
  156. -(void)pushMessage
  157. {
  158. ZBMessageViewController *message=[[ZBMessageViewController alloc]init];
  159. [[self currentViewController]presentViewController:[[UINavigationController alloc]initWithRootViewController:message] animated:NO completion:nil];
  160. }
  161. - (UIViewController *)currentViewController{
  162. UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
  163. NSAssert(window, @"The window is empty");
  164. return window.rootViewController;
  165. }
  166. - (void)applicationWillResignActive:(UIApplication *)application {
  167. // 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.
  168. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  169. }
  170. - (void)applicationDidEnterBackground:(UIApplication *)application {
  171. // 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.
  172. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  173. }
  174. - (void)applicationWillEnterForeground:(UIApplication *)application {
  175. // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  176. }
  177. - (void)applicationDidBecomeActive:(UIApplication *)application {
  178. // 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.
  179. }
  180. - (void)applicationWillTerminate:(UIApplication *)application {
  181. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  182. }
  183. @end