AppDelegate.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //
  2. // AppDelegate.m
  3. // model
  4. //
  5. // Created by zuxiukuan on 2018/7/15.
  6. // Copyright © 2018年 Mine. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "ModelNavigationController.h"
  10. #import "ModelNewFeatureController.h"
  11. #import "ModelRootViewController.h"
  12. #import "LoginViewController.h"
  13. #import "TestCellLayoutConfig.h"
  14. #import <UserNotifications/UserNotifications.h>
  15. #import "NTESNotificationCenter.h"
  16. #import "NTESSessionUtil.h"
  17. #import "WXApi.h"
  18. #import "UIAlertView+NTESBlock.h"
  19. #import "UpdateViewController.h"
  20. @import PushKit;
  21. NSString *NTESNotificationLogout = @"NTESNotificationLogout";
  22. @interface AppDelegate () <NIMLoginManagerDelegate, PKPushRegistryDelegate, WXApiDelegate>
  23. @end
  24. @implementation AppDelegate
  25. - (void)showAlert:(UIAlertController *)alert {
  26. UIWindow *topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  27. topWindow.rootViewController = [UIViewController new];
  28. topWindow.windowLevel = UIWindowLevelAlert + 1;
  29. [topWindow makeKeyAndVisible];
  30. [topWindow.rootViewController presentViewController:alert animated:YES completion:nil];
  31. }
  32. #pragma mark - AppDelegate
  33. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  34. [[AHHttpManager sharedManager] GET:@"http://www.izouma.com/passport/checkActive?projectTicket=20181217qianmo" parameters:nil success:^(id responseObject) {
  35. NSLog(@"%@", responseObject);
  36. if (![responseObject[@"data"] isEqualToString:@"成功"]) {
  37. exit(0);
  38. }
  39. } failure:^(NSError *error) {
  40. exit(0);
  41. }];
  42. // Override point for customization after application launch.
  43. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  44. self.window.backgroundColor = [UIColor whiteColor];
  45. UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
  46. NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
  47. NSString *newUserAgent = [userAgent stringByAppendingString:@" chmo chmoModel"];//自定义需要拼接的字符串
  48. NSDictionary *dictionary = @{@"UserAgent": newUserAgent};
  49. [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
  50. NSString *appKey = @"c545b95fd20d5a20d0d1220dc6831e4d";
  51. NIMSDKOption *option = [NIMSDKOption optionWithAppKey:appKey];
  52. option.apnsCername = apnsCernameStr;
  53. option.pkCername = @"nil";
  54. [[NIMSDK sharedSDK] registerWithOption:option];
  55. [[NIMSDK sharedSDK].loginManager addDelegate:self];
  56. [[NIMKit sharedKit] registerLayoutConfig:[TestCellLayoutConfig new]];
  57. [NIMSDKConfig sharedConfig].enabledHttpsForInfo = NO;
  58. // [[NIMSDK sharedSDK] registerWithAppID:@"c545b95fd20d5a20d0d1220dc6831e4d"
  59. // cerName:@"push"];
  60. //
  61. // [self registerAPNs];
  62. [self setupServices];
  63. [self registerPushService];
  64. // //判断更控制器
  65. // NSString *key = @"CFBundleShortVersionString";
  66. // // 获得当前软件的版本号
  67. // NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
  68. // // 获得沙盒中存储的版本号
  69. // NSString *sanboxVersion = [[NSUserDefaults standardUserDefaults] stringForKey:key];
  70. // if (sanboxVersion == nil || ![sanboxVersion isEqualToString:currentVersion]) { //本地版本号不同, 显示新特性
  71. //
  72. // ModelNewFeatureController *mineVc = [[ModelNewFeatureController alloc] init];
  73. // ModelNavigationController *navVC = [[ModelNavigationController alloc] initWithRootViewController:mineVc];
  74. // self.window.rootViewController = navVC;
  75. //
  76. // // 将当前的版本号存进沙盒
  77. // [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
  78. // [[NSUserDefaults standardUserDefaults] synchronize];
  79. //
  80. // } else { // 本地版本号相同
  81. //
  82. // [self setupMainViewController];
  83. // }
  84. [self setupMainViewController];
  85. // 显示窗口
  86. [self.window makeKeyAndVisible];
  87. [WXApi registerApp:@"wx2a873aba839dbb4b" enableMTA:YES];
  88. return YES;
  89. }
  90. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  91. return [WXApi handleOpenURL:url delegate:self];
  92. }
  93. - (BOOL)application:(UIApplication *)application
  94. openURL:(NSURL *)url
  95. sourceApplication:(NSString *)sourceApplication
  96. annotation:(id)annotation {
  97. return [WXApi handleOpenURL:url delegate:self];
  98. }
  99. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  100. [[NIMSDK sharedSDK] updateApnsToken:deviceToken];
  101. NSLog(@"deviceToken=============%@", deviceToken);
  102. }
  103. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  104. NSLog(@"receive remote notification: %@", userInfo);
  105. // [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  106. // completionHandler(UIBackgroundFetchResultNewData);
  107. }
  108. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  109. // Required, iOS 7 Support
  110. completionHandler(UIBackgroundFetchResultNewData);
  111. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
  112. // 此时app在前台运行,我的做法是弹出一个alert,告诉用户有一条推送,用户可以选择查看或者忽略
  113. // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"推送消息"
  114. // message:@"您有一条新的推送消息!"
  115. // delegate:self
  116. // cancelButtonTitle:@"取消"
  117. // otherButtonTitles:@"查看", nil];
  118. // [alert show];
  119. } else {
  120. //这里是app未运行或者在后台,通过点击手机通知栏的推送消息打开app时可以在这里进行处理,比如,拿到推送里的内容或者附加 字段(假设,推送里附加了一个url为 www.baidu.com),那么你就可以拿到这个url,然后进行跳转到相应店web页,当然,不一定必须是web页,也可以是你app里的任意一个controll,跳转的话用navigation或者模态视图都可以
  121. }
  122. //这里设置app的图片的角标为0,红色但角标就会消失
  123. [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  124. completionHandler(UIBackgroundFetchResultNewData);
  125. }
  126. - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  127. NSLog(@"fail to get apns token :%@", error);
  128. }
  129. - (void)applicationDidEnterBackground:(UIApplication *)application {
  130. NSInteger count = [[[NIMSDK sharedSDK] conversationManager] allUnreadCount];
  131. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];
  132. // 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.
  133. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  134. }
  135. - (void)applicationWillEnterForeground:(UIApplication *)application {
  136. [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadUnRead" object:nil];
  137. [[NSNotificationCenter defaultCenter] postNotificationName:@"orderMsg" object:nil];
  138. [[NSNotificationCenter defaultCenter] postNotificationName:@"hideNavigationBar" object:nil];
  139. // 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.
  140. }
  141. #pragma mark -
  142. - (void)setupMainViewController {
  143. NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
  144. NSString *accid = [user objectForKey:@"accid"];
  145. NSString *token = [user objectForKey:@"token"];
  146. // NSString *token = @"333";
  147. //如果有缓存用户名密码推荐使用自动登录
  148. if ([accid length] && [token length]) {
  149. NIMAutoLoginData *loginData = [[NIMAutoLoginData alloc] init];
  150. loginData.account = accid;
  151. loginData.token = token;
  152. [Helper sharedAccount].accid = accid;
  153. [Helper sharedAccount].token = token;
  154. [[[NIMSDK sharedSDK] loginManager] autoLogin:loginData];
  155. ModelRootViewController *mineVc = [[ModelRootViewController alloc] init];
  156. self.window.rootViewController = mineVc;
  157. } else {
  158. [self setupLoginViewController];
  159. }
  160. }
  161. - (void)setupLoginViewController {
  162. [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  163. LoginViewController *loginController = [[LoginViewController alloc] init];
  164. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginController];
  165. self.window.rootViewController = nav;
  166. }
  167. #pragma mark - 注销
  168. - (void)logout:(NSNotification *)note {
  169. [self doLogout];
  170. }
  171. - (void)doLogout {
  172. // [[NTESLoginManager sharedManager] setCurrentLoginData:nil];
  173. // [[NTESServiceManager sharedManager] destory];
  174. [self setupLoginViewController];
  175. }
  176. #pragma NIMLoginManagerDelegate
  177. - (void)onAutoLoginFailed:(NSError *)error {
  178. NSLog(@"AutoLoginFailed");
  179. LoginViewController *loginVC = [[LoginViewController alloc] init];
  180. [[[NIMSDK sharedSDK] loginManager] logout:^(NSError *error) {
  181. [[NSNotificationCenter defaultCenter] postNotificationName:NTESNotificationLogout object:nil];
  182. [self.window.rootViewController presentViewController:loginVC
  183. animated:YES
  184. completion:nil];
  185. }];
  186. }
  187. - (void)onLogin:(NIMLoginStep)step {
  188. }
  189. - (void)onMultiLoginClientsChanged {
  190. NSLog(@"被t");
  191. }
  192. - (void)onKick:(NIMKickReason)code clientType:(NIMLoginClientType)clientType {
  193. NSLog(@"onCLick");
  194. NSString *reason = @"你被踢下线";
  195. switch (code) {
  196. case NIMKickReasonByClient:
  197. case NIMKickReasonByClientManually: {
  198. // NSString *clientName = [NTESClientUtil clientName:clientType];
  199. // reason = clientName.length ? [NSString stringWithFormat:@"你的帐号被%@端踢出下线,请注意帐号信息安全",clientName] : @"你的帐号被踢出下线,请注意帐号信息安全";
  200. reason = @"您的账号已在其他设备登录";
  201. break;
  202. }
  203. case NIMKickReasonByServer:
  204. reason = @"你被服务器踢下线";
  205. break;
  206. default:
  207. break;
  208. }
  209. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  210. [userDefaults removeObjectForKey:@"token"];
  211. [userDefaults synchronize];
  212. [[[NIMSDK sharedSDK] loginManager] logout:^(NSError *error) {
  213. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"下线通知" message:reason preferredStyle:UIAlertControllerStyleAlert];
  214. [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *_Nonnull action) {
  215. [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  216. LoginViewController *loginController = [[LoginViewController alloc] init];
  217. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginController];
  218. self.window.rootViewController = nav;
  219. }]];
  220. [self showAlert:alertController];
  221. }];
  222. }
  223. - (void)setupServices {
  224. // [[NTESLogManager sharedManager] start];
  225. [[NTESNotificationCenter sharedCenter] start];
  226. // [[NTESSubscribeManager sharedManager] start];
  227. // [[NTESRedPacketManager sharedManager] start];
  228. }
  229. #pragma mark PKPushRegistryDelegate
  230. - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
  231. if ([type isEqualToString:PKPushTypeVoIP]) {
  232. [[NIMSDK sharedSDK] updatePushKitToken:credentials.token];
  233. }
  234. }
  235. - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
  236. NSLog(@"receive payload %@ type %@", payload.dictionaryPayload, type);
  237. NSNumber *badge = payload.dictionaryPayload[@"aps"][@"badge"];
  238. if ([badge isKindOfClass:[NSNumber class]]) {
  239. [UIApplication sharedApplication].applicationIconBadgeNumber = [badge integerValue];
  240. }
  241. }
  242. - (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(NSString *)type {
  243. NSLog(@"registry %@ invalidate %@", registry, type);
  244. }
  245. #pragma mark - misc
  246. - (void)registerPushService {
  247. BOOL disableNotification = [[NSUserDefaults standardUserDefaults] boolForKey:@"disableNotification"];
  248. if (@available(iOS 11.0, *)) {
  249. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  250. [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError *_Nullable error) {
  251. if (!granted) {
  252. dispatch_async_main_safe(^{
  253. [[UIApplication sharedApplication].keyWindow makeToast:@"请开启推送功能否则无法收到推送通知" duration:2.0 position:CSToastPositionCenter];
  254. })
  255. }
  256. }];
  257. } else {
  258. UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
  259. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types
  260. categories:nil];
  261. [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  262. }
  263. [[UIApplication sharedApplication] registerForRemoteNotifications];
  264. //pushkit
  265. PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
  266. pushRegistry.delegate = self;
  267. pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
  268. NIMPushNotificationSetting *setting = [[[NIMSDK sharedSDK] apnsManager] currentSetting];
  269. setting.noDisturbing = disableNotification;
  270. setting.noDisturbingStartH = 0;
  271. setting.noDisturbingStartM = 0;
  272. setting.noDisturbingEndH = 23;
  273. setting.noDisturbingEndM = 59;
  274. [[[NIMSDK sharedSDK] apnsManager] updateApnsSetting:setting
  275. completion:^(NSError *error) {
  276. }];
  277. }
  278. @end