ICRAppDelegate.m 9.91 KB
Newer Older
mei's avatar
mei committed
1 2
//
//  ICRAppDelegate.m
mei's avatar
mei committed
3
//  XFFruit
mei's avatar
mei committed
4 5 6 7 8 9 10
//
//  Created by Xummer on 3/23/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "ICRAppDelegate.h"
#import "ICRAppViewControllerManager.h"
mei's avatar
mei committed
11 12
#import "ICRURLProtocol.h"
#import "BMKMapManager.h"
zhu's avatar
zhu committed
13
#import "ICRHTTPController.h"
freecui's avatar
freecui committed
14
#import "APService.h"
mei's avatar
mei committed
15 16 17

@interface ICRAppDelegate ()
@property (strong, nonatomic) BMKMapManager *mapManager;
zhu's avatar
zhu committed
18 19
@property (strong, nonatomic) NSString *version;//本地版本号
@property (strong, nonatomic) NSString *severVersion;//服务器版本号
zhu's avatar
zhu committed
20 21 22
@property (strong, nonatomic) NSString *intVersion;//本地版本号
@property (strong, nonatomic) NSString *intSeverVersion;//服务器版本号

zhu's avatar
zhu committed
23 24
@property (strong, nonatomic) NSString *severUrl;//Ipa下载地址
@property (assign, nonatomic) BOOL severForceUpdate;//强制更新
mei's avatar
mei committed
25 26

@end
mei's avatar
mei committed
27 28 29 30 31 32

@implementation ICRAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
mei's avatar
mei committed
33
    
mei's avatar
mei committed
34 35 36
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyWindow];
    
mei's avatar
mei committed
37 38 39
    [NSURLProtocol registerClass:[ICRURLProtocol class]];
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
    CLog(@"%@",path);
zhu's avatar
zhu committed
40
    
freecui's avatar
freecui committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    // Required
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //categories
        [APService
         registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                             UIUserNotificationTypeSound |
                                             UIUserNotificationTypeAlert)
         categories:nil];
    } else {
        //categories nil
        [APService
         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                             UIRemoteNotificationTypeSound |
                                             UIRemoteNotificationTypeAlert)
#else
         //categories nil
         categories:nil];
        [APService
         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                             UIRemoteNotificationTypeSound |
                                             UIRemoteNotificationTypeAlert)
#endif
         // Required
         categories:nil];
    }
    [APService setupWithOption:launchOptions];
mei's avatar
mei committed
68
    [self setupBMKMapMgr];
mei's avatar
mei committed
69
    _m_appViewControllerMgr = [ICRAppViewControllerManager getAppViewControllerManager];
freecui's avatar
freecui committed
70
#pragma 版本更新
陈俊俊's avatar
陈俊俊 committed
71
    [self enterpriseUpdate];
mei's avatar
mei committed
72 73 74
    [self.window makeKeyAndVisible];
    return YES;
}
freecui's avatar
freecui committed
75 76
#pragma mark enterprise alertView 企业版提示
- (void)enterpriseUpdate{
Sandy's avatar
Sandy committed
77 78 79

    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

zhu's avatar
zhu committed
80 81
    self.version=version;
    
zhu's avatar
zhu committed
82
    // 获取服务器更新信息
zhu's avatar
zhu committed
83
    [ self  getDataFromServer];
zhu's avatar
zhu committed
84
    
zhu's avatar
zhu committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
}
//请求服务器更新信息
- (void)getDataFromServer{
    __weak typeof(self)weakSelf = self;
    void(^succ)(id) = ^(id data) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
            NSString *message  = data[@"message"] ;
            if (success == 1) {
                
                NSDictionary *dictData = data[@"data"];
                NSString*serverVeision=[dictData objectForKey:@"version"];
                NSString*serverUrl=[dictData objectForKey:@"url"];
陈俊俊's avatar
陈俊俊 committed
99
                BOOL forceUpdate= [dictData objectForKey:@"forceUpdate"];
zhu's avatar
zhu committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
                self.severVersion=serverVeision;
                self.severUrl=serverUrl;
                self.severForceUpdate=forceUpdate;
                [strongSelf  comperVersion];
            }else{
                [IBTLoadingView showTips:message];
            }
        }else{
            [IBTLoadingView showTips:@"     无记录     "];
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView showTips:data];
    };
    [[ICRHTTPController sharedController] getVeisionIdWithType:@"ipa" success:succ failure:fail];
}
//比较版本号及是否下载
-(void)comperVersion
{
陈俊俊's avatar
陈俊俊 committed
119
    if (self.version && self.severVersion) {
zhu's avatar
zhu committed
120 121 122 123 124 125 126 127 128 129 130 131 132
        self.intVersion=@"";
        self.intSeverVersion=@"";
        NSArray *array1 = [self.version componentsSeparatedByString:@"."];
         NSArray *array2 = [self.severVersion componentsSeparatedByString:@"."];
        for (int i = 0; i < [array1 count]; i++) {
            self.intVersion=[self.intVersion stringByAppendingFormat:@"%@",[array1 objectAtIndex:i]];
            NSLog(@"string:%@", [array1 objectAtIndex:i]);
        }
        for (int i = 0; i < [array2 count]; i++) {
            self.intSeverVersion=[self.intSeverVersion stringByAppendingFormat:@"%@",[array2 objectAtIndex:i]];
            NSLog(@"string:%@", [array2 objectAtIndex:i]);
        }
        BOOL isNeedUpdate=[self compareVersion];
freecui's avatar
freecui committed
133
        
zhu's avatar
zhu committed
134
        if (isNeedUpdate) {
陈俊俊's avatar
陈俊俊 committed
135 136
            if (self.severForceUpdate) {
                //强制更新
陈俊俊's avatar
陈俊俊 committed
137 138 139
                UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"新版本告知" message:  @"有新版本发布请您下载最新版本" delegate:self cancelButtonTitle:nil otherButtonTitles: @"去下载", nil];
                alertV.tag = 1001;
                [alertV show];
陈俊俊's avatar
陈俊俊 committed
140 141 142 143 144 145 146
            }else
            {
                UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"新版本告知" message:  @"有新版本发布请您下载最新版本" delegate:self cancelButtonTitle:@"下次再说"otherButtonTitles: @"去下载", nil];//
                alertV.tag = 1002;
                [alertV show];
            }
        }
freecui's avatar
freecui committed
147 148
    }
}
zhu's avatar
zhu committed
149 150 151 152 153 154 155 156 157 158 159
-(BOOL)compareVersion
{
    float  version=[self.intVersion floatValue];
    float severVersion=[self.intSeverVersion floatValue];
    if (severVersion >version) {
        return YES;
    }else
    {
        return NO;
    }
}
freecui's avatar
freecui committed
160 161
- (void) alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
zhu's avatar
zhu committed
162
    if (alertView.tag==1001) {
zhu's avatar
zhu committed
163
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.severUrl]];
zhu's avatar
zhu committed
164 165 166 167 168
    }else if (alertView.tag==1002)
    {
        if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.severUrl]];
        }
freecui's avatar
freecui committed
169
    }
zhu's avatar
zhu committed
170
    
freecui's avatar
freecui committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
}
//比较版本号
- (NSComparisonResult)p_compareVersions: (NSString *)version1 betweenVersion2: (NSString *)version2{
    NSComparisonResult result = NSOrderedSame;
    if ([version2 isEqualToString:@""]) {
        result = NSOrderedDescending;
        return result ;
    }
    
    NSMutableArray *a = (NSMutableArray *)[version1 componentsSeparatedByString:@"."];
    NSMutableArray *b = (NSMutableArray *)[version2 componentsSeparatedByString:@"."];
    
    while (a.count < b.count) {[ a addObject:@"0"];}
    while (b.count < a.count) {[ b addObject:@"0"];}
    
    for(int i = 0; i < a.count; ++i){
        if ([[a objectAtIndex:i] integerValue] < [[b objectAtIndex:i] integerValue]) {
            result = NSOrderedAscending;
            break;
        }else if ([[b objectAtIndex:i] integerValue] < [[a objectAtIndex:i] integerValue]){
            result = NSOrderedDescending;
            break;
        }
    }
    return  result;
    
}
mei's avatar
mei committed
198 199 200 201 202 203 204 205 206 207 208 209

- (void)applicationWillResignActive:(UIApplication *)application {
    // 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.
    // 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.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 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.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
陈俊俊's avatar
陈俊俊 committed
210 211
    //即将进入前台再次判断版本是否有需要更新
    [self enterpriseUpdate];
freecui's avatar
freecui committed
212 213
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];
陈俊俊's avatar
陈俊俊 committed
214 215
    
    
freecui's avatar
freecui committed
216 217
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
zhu's avatar
zhu committed
218
    
freecui's avatar
freecui committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232
    CLog (@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
    [APService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required
    [APService handleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
mei's avatar
mei committed
233 234 235 236 237 238 239 240 241 242
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 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.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

mei's avatar
mei committed
243 244 245 246 247 248 249 250 251
#pragma mark - Private
- (void)setupBMKMapMgr {
    self.mapManager = [[BMKMapManager alloc] init];
    BOOL ret = [_mapManager start:BMK_ID generalDelegate:nil];
    if (!ret) {
        CLog(@"mapManager start failed!");
    }
}

mei's avatar
mei committed
252
@end