AppDelegate.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //
  2. // AppDelegate.m
  3. // IBOSS
  4. //
  5. // Created by iHope on 14-6-5.
  6. // Copyright (c) 2017年 沈阳东科云信软件有限公司. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "LoginViewController.h"
  10. #import "MainTabVC.h"
  11. #import "ExceptionHandler.h"
  12. #import "DateFormat.h"
  13. #import "ServiceNoticeModel.h"
  14. #import "SQLiteManager.h"
  15. @interface AppDelegate(){
  16. // app store 升级url
  17. NSString *trackViewURL;
  18. }
  19. /**
  20. 请求数据对象
  21. */
  22. @property (strong,nonatomic) ASIDownManager *mDownManager ;
  23. @end
  24. @implementation AppDelegate
  25. /**
  26. 登陆主页
  27. */
  28. - (void)changeViewDidLogin {
  29. MainTabVC *mainVC = [[MainTabVC alloc] init];
  30. self.window.rootViewController = mainVC;//mainVC.tabBarController;
  31. }
  32. /**
  33. 登陆页
  34. */
  35. - (void)changeViewDidLogout {
  36. LoginViewController *loginVC = [[LoginViewController alloc] init];
  37. self.window.rootViewController = loginVC;
  38. }
  39. /**
  40. 自动登录
  41. */
  42. - (void)autoLogin
  43. {
  44. LoginViewController *loginVC = [[LoginViewController alloc] init];
  45. loginVC.isAutoLogin = YES;
  46. self.window.rootViewController = loginVC;
  47. }
  48. /**
  49. 程序启动
  50. @param application <#application description#>
  51. @param launchOptions <#launchOptions description#>
  52. @return <#return value description#>
  53. */
  54. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  55. {
  56. // [self requestxxxx
  57. // IOS判断app在appstore是否有可用的更新
  58. [self checkVersion];
  59. [self initAPNS];
  60. [self initJPush:launchOptions];
  61. //[self jumpToViewController:launchOptions];
  62. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  63. [self ShowLoadView:nil];
  64. // Override point for customization after application launch.
  65. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){
  66. [[UINavigationBar appearance] setBarTintColor:NavColor];
  67. }
  68. [self.window makeKeyAndVisible];
  69. InstallUncaughtExceptionHandler();
  70. return YES;
  71. }
  72. /**
  73. 请求下数据,弹出允许网络权限
  74. */
  75. - (void)request {
  76. if(!kkFirstLogin){
  77. self.mDownManager = [[ASIDownManager alloc] init];
  78. self.mDownManager.delegate = self;
  79. [self.mDownManager postHttpRequest:@"www.baidu.com" dic:nil path:nil fileName:nil];
  80. kkFirstLogin = @"ok";
  81. }
  82. }
  83. /**
  84. IOS判断app在appstore是否有可用的更新
  85. */
  86. - (void)checkVersion{
  87. self.mDownManager = [[ASIDownManager alloc] init];
  88. self.mDownManager.delegate = self;
  89. self.mDownManager.onRequestSuccess = @selector(onRequestSuccess:) ;
  90. [self.mDownManager postHttpRequest:@"http://itunes.apple.com/lookup?id=1273991989" dic:nil path:nil fileName:nil];
  91. }
  92. /**
  93. 请求数据成功
  94. @param d <#d description#>
  95. */
  96. - (void)onRequestSuccess:(ASIDownManager *)sender{
  97. NSDictionary *resultDic = [sender.mWebStr JSONValue];
  98. NSArray *infoArray = [resultDic objectForKey:@"results"];
  99. if (infoArray.count > 0) {
  100. NSDictionary* releaseInfo = [infoArray objectAtIndex:0];
  101. NSString* appStoreVersion = [releaseInfo objectForKey:@"version"];
  102. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  103. NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
  104. NSArray *curVerArr = [currentVersion componentsSeparatedByString:@"."];
  105. NSArray *appstoreVerArr = [appStoreVersion componentsSeparatedByString:@"."];
  106. BOOL needUpdate = NO;
  107. //比较版本号大小
  108. int maxv=(int)MAX(curVerArr.count, appstoreVerArr.count);
  109. for (int i = 0; i < maxv; i++) {
  110. int appstoreVer=[ appstoreVerArr[i] intValue];
  111. int curVer=[curVerArr[i] intValue];
  112. if(appstoreVer>curVer){
  113. needUpdate = YES;
  114. break;
  115. }
  116. if(appstoreVer<curVer){
  117. needUpdate = NO;
  118. break;
  119. }
  120. }
  121. //如果有可用的更新
  122. if (needUpdate){
  123. trackViewURL = [[NSString alloc] initWithString:[releaseInfo objectForKey:@"trackViewUrl"]];//trackViewURL临时变量存储app下载地址,可以让app跳转到appstore
  124. UIAlertView* alertview = [[UIAlertView alloc] initWithTitle:@"版本升级" message:[NSString stringWithFormat:@"发现有新版本,是否升级?"] delegate:self cancelButtonTitle:@"暂不升级" otherButtonTitles:@"马上升级", nil];
  125. [alertview show];
  126. }
  127. }
  128. }
  129. /**
  130. app下载地址,可以让app跳转到appstore
  131. @param alertView <#alertView description#>
  132. @param buttonIndex <#buttonIndex description#>
  133. */
  134. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  135. {
  136. if (buttonIndex == 1){
  137. UIApplication *application = [UIApplication sharedApplication];
  138. [application openURL:[NSURL URLWithString:trackViewURL]];
  139. }
  140. }
  141. /**
  142. view
  143. @param view <#view description#>
  144. */
  145. - (void)ShowLoadView:(UIView *)view {
  146. //没有自动登录
  147. if(kkAutoLogin == nil||[[NSString stringWithFormat:@"%@",kkAutoLogin ]isEqualToString:@"0"]){
  148. [self changeViewDidLogout];
  149. }
  150. else {
  151. //自动登录了
  152. if(kkSessionKey != nil && ![kkSessionKey isEqualToString:@""]){
  153. [self autoLogin];
  154. }
  155. else{
  156. [self changeViewDidLogout];
  157. }
  158. }
  159. }
  160. - (void)applicationWillResignActive:(UIApplication *)application
  161. {
  162. }
  163. - (void)applicationDidEnterBackground:(UIApplication *)application
  164. {
  165. }
  166. - (void)applicationWillEnterForeground:(UIApplication *)application
  167. {
  168. [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  169. }
  170. - (void)applicationDidBecomeActive:(UIApplication *)application
  171. {
  172. }
  173. - (void)applicationWillTerminate:(UIApplication *)application
  174. {
  175. NSLog(@"terminate");
  176. }
  177. #pragma mark- JPUSHRegisterDelegate
  178. /**
  179. 注册APNs成功并上报DeviceToken
  180. @param application <#application description#>
  181. @param deviceToken <#deviceToken description#>
  182. */
  183. - (void)application:(UIApplication *)application
  184. didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  185. /// Required - 注册 DeviceToken
  186. [JPUSHService registerDeviceToken:deviceToken];
  187. }
  188. /**
  189. 初始化APNs代码
  190. */
  191. - (void)initAPNS{
  192. //Required
  193. //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
  194. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  195. entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
  196. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  197. // 可以添加自定义categories
  198. // NSSet<UNNotificationCategory *> *categories for iOS10 or later
  199. // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
  200. }
  201. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  202. }
  203. /**
  204. 初始化JPush代码
  205. */
  206. - (void)initJPush:(NSDictionary *)launchOptions{
  207. // Optional
  208. // 获取IDFA
  209. // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
  210. // NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  211. // Required
  212. // init Push
  213. // notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
  214. // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
  215. //319528e242422691853d8a3e , dd149d29cad1f6dc980584e0
  216. //75b1858eab0ed446cf3ecbc3 , 7ca94b9206fed7df14637f77
  217. [JPUSHService setupWithOption:launchOptions appKey:@"319528e242422691853d8a3e"
  218. channel:@""
  219. apsForProduction:YES
  220. advertisingIdentifier:nil];
  221. }
  222. /**
  223. iOS 10 以上显示通知栏
  224. @param center <#center description#>
  225. @param notification <#notification description#>
  226. @param completionHandler <#completionHandler description#>
  227. */
  228. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  229. // Required
  230. NSDictionary * userInfo = notification.request.content.userInfo;
  231. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  232. [JPUSHService handleRemoteNotification:userInfo];
  233. }
  234. completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
  235. }
  236. /**
  237. iOS 10 以上
  238. @param center <#center description#>
  239. @param response <#response description#>
  240. @param completionHandler <#completionHandler description#>
  241. */
  242. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  243. // Required
  244. NSDictionary * userInfo = response.notification.request.content.userInfo;
  245. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  246. [JPUSHService handleRemoteNotification:userInfo];
  247. self.pushDic = userInfo;
  248. [self saveNotificationToDb:userInfo];
  249. }
  250. completionHandler(); // 系统要求执行这个方法
  251. }
  252. /**
  253. ios7以上 10以下
  254. @param application <#application description#>
  255. @param userInfo <#userInfo description#>
  256. @param completionHandler <#completionHandler description#>
  257. */
  258. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  259. self.pushDic = userInfo;//这里是声明了一个字典,因为要在下面的presentViewControllerWithPushInfo方法中,需要用到这个字典来进行判断
  260. //收到推送时程序在后台运行,点击通知栏中的推送消息,跳转到指定页面
  261. //Required, iOS 7 Support
  262. [JPUSHService handleRemoteNotification:userInfo];
  263. [self saveNotificationToDb:userInfo];
  264. completionHandler(UIBackgroundFetchResultNewData);
  265. }
  266. /**
  267. ios 3-7 支持
  268. @param application <#application description#>
  269. @param userInfo <#userInfo description#>
  270. */
  271. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  272. [JPUSHService handleRemoteNotification:userInfo];
  273. [self saveNotificationToDb:userInfo];
  274. }
  275. -(void) saveNotificationToDb:(NSDictionary*)userInfo{
  276. NSDictionary *messageDic=[userInfo objectForKey:@"aps"];
  277. NSString *content= [messageDic objectForKey:@"alert"];
  278. NSString *systemTime= [DateFormat getCurrentDateTime];
  279. NSString *readFlag=@"0";
  280. bool open= [SQLiteManager shareInstance].openDB;
  281. if(open){
  282. ServiceNoticeModel *serviceNotice = [[ServiceNoticeModel alloc] init];
  283. serviceNotice.content=content;
  284. serviceNotice.time=systemTime;
  285. serviceNotice.readFlag=readFlag;
  286. if ([serviceNotice insertSelfToDB]) {
  287. NSNotification* notification = [NSNotification notificationWithName:@"sendBroadCast" object:nil];
  288. [[NSNotificationCenter defaultCenter] postNotification:notification];
  289. }else{
  290. NSLog(@"对象插入失败!");
  291. }
  292. }
  293. [ [SQLiteManager shareInstance] closeDB];
  294. }
  295. @end