| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- //
- // AppDelegate.m
- // IBOSS
- //
- // Created by iHope on 14-6-5.
- // Copyright (c) 2017年 沈阳东科云信软件有限公司. All rights reserved.
- //
- #import "AppDelegate.h"
- #import "LoginViewController.h"
- #import "MainTabVC.h"
- #import "ExceptionHandler.h"
- #import "DateFormat.h"
- #import "ServiceNoticeModel.h"
- #import "SQLiteManager.h"
- @interface AppDelegate(){
- // app store 升级url
- NSString *trackViewURL;
- }
- /**
- 请求数据对象
- */
- @property (strong,nonatomic) ASIDownManager *mDownManager ;
- @end
- @implementation AppDelegate
- /**
- 登陆主页
- */
- - (void)changeViewDidLogin {
- MainTabVC *mainVC = [[MainTabVC alloc] init];
- self.window.rootViewController = mainVC;//mainVC.tabBarController;
- }
- /**
- 登陆页
- */
- - (void)changeViewDidLogout {
- LoginViewController *loginVC = [[LoginViewController alloc] init];
- self.window.rootViewController = loginVC;
- }
- /**
- 自动登录
- */
- - (void)autoLogin
- {
- LoginViewController *loginVC = [[LoginViewController alloc] init];
- loginVC.isAutoLogin = YES;
- self.window.rootViewController = loginVC;
- }
- /**
- 程序启动
-
- @param application <#application description#>
- @param launchOptions <#launchOptions description#>
- @return <#return value description#>
- */
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
-
- // [self requestxxxx
- // IOS判断app在appstore是否有可用的更新
- [self checkVersion];
- [self initAPNS];
- [self initJPush:launchOptions];
- //[self jumpToViewController:launchOptions];
- self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
- [self ShowLoadView:nil];
- // Override point for customization after application launch.
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){
- [[UINavigationBar appearance] setBarTintColor:NavColor];
- }
- [self.window makeKeyAndVisible];
- InstallUncaughtExceptionHandler();
- return YES;
- }
- /**
- 请求下数据,弹出允许网络权限
- */
- - (void)request {
- if(!kkFirstLogin){
- self.mDownManager = [[ASIDownManager alloc] init];
- self.mDownManager.delegate = self;
- [self.mDownManager postHttpRequest:@"www.baidu.com" dic:nil path:nil fileName:nil];
- kkFirstLogin = @"ok";
- }
- }
- /**
- IOS判断app在appstore是否有可用的更新
- */
- - (void)checkVersion{
- self.mDownManager = [[ASIDownManager alloc] init];
- self.mDownManager.delegate = self;
- self.mDownManager.onRequestSuccess = @selector(onRequestSuccess:) ;
- [self.mDownManager postHttpRequest:@"http://itunes.apple.com/lookup?id=1273991989" dic:nil path:nil fileName:nil];
- }
- /**
- 请求数据成功
-
- @param d <#d description#>
- */
- - (void)onRequestSuccess:(ASIDownManager *)sender{
- NSDictionary *resultDic = [sender.mWebStr JSONValue];
- NSArray *infoArray = [resultDic objectForKey:@"results"];
- if (infoArray.count > 0) {
- NSDictionary* releaseInfo = [infoArray objectAtIndex:0];
- NSString* appStoreVersion = [releaseInfo objectForKey:@"version"];
- NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
- NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
-
- NSArray *curVerArr = [currentVersion componentsSeparatedByString:@"."];
-
- NSArray *appstoreVerArr = [appStoreVersion componentsSeparatedByString:@"."];
- BOOL needUpdate = NO;
- //比较版本号大小
- int maxv=(int)MAX(curVerArr.count, appstoreVerArr.count);
-
- for (int i = 0; i < maxv; i++) {
- int appstoreVer=[ appstoreVerArr[i] intValue];
- int curVer=[curVerArr[i] intValue];
-
- if(appstoreVer>curVer){
- needUpdate = YES;
- break;
- }
- if(appstoreVer<curVer){
- needUpdate = NO;
- break;
- }
-
-
- }
-
- //如果有可用的更新
- if (needUpdate){
- trackViewURL = [[NSString alloc] initWithString:[releaseInfo objectForKey:@"trackViewUrl"]];//trackViewURL临时变量存储app下载地址,可以让app跳转到appstore
- UIAlertView* alertview = [[UIAlertView alloc] initWithTitle:@"版本升级" message:[NSString stringWithFormat:@"发现有新版本,是否升级?"] delegate:self cancelButtonTitle:@"暂不升级" otherButtonTitles:@"马上升级", nil];
- [alertview show];
- }
- }
- }
- /**
- app下载地址,可以让app跳转到appstore
- @param alertView <#alertView description#>
- @param buttonIndex <#buttonIndex description#>
- */
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (buttonIndex == 1){
- UIApplication *application = [UIApplication sharedApplication];
- [application openURL:[NSURL URLWithString:trackViewURL]];
- }
- }
- /**
- view
-
- @param view <#view description#>
- */
- - (void)ShowLoadView:(UIView *)view {
- //没有自动登录
- if(kkAutoLogin == nil||[[NSString stringWithFormat:@"%@",kkAutoLogin ]isEqualToString:@"0"]){
- [self changeViewDidLogout];
-
- }
- else {
- //自动登录了
- if(kkSessionKey != nil && ![kkSessionKey isEqualToString:@""]){
- [self autoLogin];
- }
- else{
- [self changeViewDidLogout];
- }
- }
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
-
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- }
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- NSLog(@"terminate");
- }
- #pragma mark- JPUSHRegisterDelegate
- /**
- 注册APNs成功并上报DeviceToken
-
- @param application <#application description#>
- @param deviceToken <#deviceToken description#>
- */
- - (void)application:(UIApplication *)application
- didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
-
- /// Required - 注册 DeviceToken
- [JPUSHService registerDeviceToken:deviceToken];
- }
- /**
- 初始化APNs代码
- */
- - (void)initAPNS{
- //Required
- //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
- JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
- entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
- if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
- // 可以添加自定义categories
- // NSSet<UNNotificationCategory *> *categories for iOS10 or later
- // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
- }
- [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
- }
- /**
- 初始化JPush代码
- */
- - (void)initJPush:(NSDictionary *)launchOptions{
- // Optional
- // 获取IDFA
- // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
- // NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
-
- // Required
- // init Push
- // notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
- // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
- //319528e242422691853d8a3e , dd149d29cad1f6dc980584e0
- //75b1858eab0ed446cf3ecbc3 , 7ca94b9206fed7df14637f77
- [JPUSHService setupWithOption:launchOptions appKey:@"319528e242422691853d8a3e"
- channel:@""
- apsForProduction:YES
- advertisingIdentifier:nil];
- }
- /**
- iOS 10 以上显示通知栏
-
- @param center <#center description#>
- @param notification <#notification description#>
- @param completionHandler <#completionHandler description#>
- */
- - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
- // Required
- NSDictionary * userInfo = notification.request.content.userInfo;
- if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
- [JPUSHService handleRemoteNotification:userInfo];
-
- }
- completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
- }
- /**
- iOS 10 以上
- @param center <#center description#>
- @param response <#response description#>
- @param completionHandler <#completionHandler description#>
- */
- - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
- // Required
- NSDictionary * userInfo = response.notification.request.content.userInfo;
- if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
- [JPUSHService handleRemoteNotification:userInfo];
- self.pushDic = userInfo;
- [self saveNotificationToDb:userInfo];
- }
- completionHandler(); // 系统要求执行这个方法
- }
- /**
- ios7以上 10以下
- @param application <#application description#>
- @param userInfo <#userInfo description#>
- @param completionHandler <#completionHandler description#>
- */
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- self.pushDic = userInfo;//这里是声明了一个字典,因为要在下面的presentViewControllerWithPushInfo方法中,需要用到这个字典来进行判断
- //收到推送时程序在后台运行,点击通知栏中的推送消息,跳转到指定页面
- //Required, iOS 7 Support
- [JPUSHService handleRemoteNotification:userInfo];
- [self saveNotificationToDb:userInfo];
- completionHandler(UIBackgroundFetchResultNewData);
- }
- /**
- ios 3-7 支持
- @param application <#application description#>
- @param userInfo <#userInfo description#>
- */
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
-
- [JPUSHService handleRemoteNotification:userInfo];
- [self saveNotificationToDb:userInfo];
- }
- -(void) saveNotificationToDb:(NSDictionary*)userInfo{
- NSDictionary *messageDic=[userInfo objectForKey:@"aps"];
- NSString *content= [messageDic objectForKey:@"alert"];
- NSString *systemTime= [DateFormat getCurrentDateTime];
- NSString *readFlag=@"0";
- bool open= [SQLiteManager shareInstance].openDB;
- if(open){
- ServiceNoticeModel *serviceNotice = [[ServiceNoticeModel alloc] init];
- serviceNotice.content=content;
- serviceNotice.time=systemTime;
- serviceNotice.readFlag=readFlag;
- if ([serviceNotice insertSelfToDB]) {
- NSNotification* notification = [NSNotification notificationWithName:@"sendBroadCast" object:nil];
- [[NSNotificationCenter defaultCenter] postNotification:notification];
-
- }else{
- NSLog(@"对象插入失败!");
- }
- }
- [ [SQLiteManager shareInstance] closeDB];
- }
- @end
|