ICRAppDelegate.m 10.3 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
    
陈俊俊's avatar
陈俊俊 committed
41 42
    [self enterpriseUpdate];

freecui's avatar
freecui committed
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 68 69
    // 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
70
    [self setupBMKMapMgr];
mei's avatar
mei committed
71
    _m_appViewControllerMgr = [ICRAppViewControllerManager getAppViewControllerManager];
freecui's avatar
freecui committed
72
#pragma 版本更新
mei's avatar
mei committed
73 74 75
    [self.window makeKeyAndVisible];
    return YES;
}
freecui's avatar
freecui committed
76 77
#pragma mark enterprise alertView 企业版提示
- (void)enterpriseUpdate{
zhu's avatar
zhu committed
78 79 80 81 82 83
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Info" ofType:@"plist"];
    NSDictionary* content =[NSDictionary dictionaryWithContentsOfFile:path];
    NSString *version = [content valueForKey:@"CFBundleVersion"] ;
    self.version=version;
    
zhu's avatar
zhu committed
84
    // 获取服务器更新信息
zhu's avatar
zhu committed
85
    [ self  getDataFromServer];
zhu's avatar
zhu committed
86
    
zhu's avatar
zhu committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}
//请求服务器更新信息
- (void)getDataFromServer{
    __weak typeof(self)weakSelf = self;
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        __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
102
                BOOL forceUpdate= [dictData objectForKey:@"forceUpdate"];
zhu's avatar
zhu committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
                self.severVersion=serverVeision;
                self.severUrl=serverUrl;
                self.severForceUpdate=forceUpdate;
                [strongSelf  comperVersion];
            }else{
                [IBTLoadingView showTips:message];
            }
        }else{
            [IBTLoadingView showTips:@"     无记录     "];
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
    [[ICRHTTPController sharedController] getVeisionIdWithType:@"ipa" success:succ failure:fail];
}
//比较版本号及是否下载
-(void)comperVersion
{
陈俊俊's avatar
陈俊俊 committed
124
    if (self.version && self.severVersion) {
zhu's avatar
zhu committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138
        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];
//        NSComparisonResult comResult = [self p_compareVersions: self.version betweenVersion2:self.severVersion];
freecui's avatar
freecui committed
139
        
zhu's avatar
zhu committed
140
        if (isNeedUpdate) {
陈俊俊's avatar
陈俊俊 committed
141 142
            if (self.severForceUpdate) {
                //强制更新
陈俊俊's avatar
陈俊俊 committed
143 144 145
//                UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"新版本告知" message:  @"有新版本发布请您下载最新版本" delegate:self cancelButtonTitle:nil otherButtonTitles: @"去下载", nil];
//                alertV.tag = 1001;
//                [alertV show];
陈俊俊's avatar
陈俊俊 committed
146 147 148 149 150 151 152
            }else
            {
                UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"新版本告知" message:  @"有新版本发布请您下载最新版本" delegate:self cancelButtonTitle:@"下次再说"otherButtonTitles: @"去下载", nil];//
                alertV.tag = 1002;
                [alertV show];
            }
        }
freecui's avatar
freecui committed
153 154
    }
}
zhu's avatar
zhu committed
155 156 157 158 159 160 161 162 163 164 165
-(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
166 167
- (void) alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
zhu's avatar
zhu committed
168
    if (alertView.tag==1001) {
zhu's avatar
zhu committed
169
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.severUrl]];
zhu's avatar
zhu committed
170 171 172 173 174
    }else if (alertView.tag==1002)
    {
        if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.severUrl]];
        }
freecui's avatar
freecui committed
175
    }
zhu's avatar
zhu committed
176
    
freecui's avatar
freecui committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
}
//比较版本号
- (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
204 205 206 207 208 209 210 211 212 213 214 215 216

- (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 {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
freecui's avatar
freecui committed
217 218 219 220
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
zhu's avatar
zhu committed
221
    
freecui's avatar
freecui committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235
    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
236 237 238 239 240 241 242 243 244 245
}

- (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
246 247 248 249 250 251 252 253 254
#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
255
@end