|
| 1 | +// |
| 2 | +// AppDelegate.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by 王 原闯 on 14-7-29. |
| 6 | +// Copyright (c) 2014年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | + |
| 10 | +#define UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) |
| 11 | +#define _IPHONE80_ 80000 |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +#import "AppDelegate.h" |
| 16 | +#import "RootTabViewController.h" |
| 17 | +#import "LoginViewController.h" |
| 18 | +#import "AFNetworkActivityIndicatorManager.h" |
| 19 | +#import "Login.h" |
| 20 | +#import "UnReadManager.h" |
| 21 | +#import "UMessage.h" |
| 22 | +#import "XGPush.h" |
| 23 | + |
| 24 | +@implementation AppDelegate |
| 25 | + |
| 26 | +@synthesize managedObjectContext = _managedObjectContext; |
| 27 | +@synthesize managedObjectModel = _managedObjectModel; |
| 28 | +@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; |
| 29 | + |
| 30 | +#pragma mark XGPush |
| 31 | +- (void)registerPush{ |
| 32 | + float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue]; |
| 33 | + if(sysVer < 8){ |
| 34 | + [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; |
| 35 | + }else{ |
| 36 | +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ |
| 37 | + UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; |
| 38 | + UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert |
| 39 | + categories:[NSSet setWithObject:categorys]]; |
| 40 | + [[UIApplication sharedApplication] registerUserNotificationSettings:userSettings]; |
| 41 | + [[UIApplication sharedApplication] registerForRemoteNotifications]; |
| 42 | +#endif |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +#pragma lifeCycle |
| 48 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 49 | +{ |
| 50 | + self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| 51 | + // Override point for customization after application launch. |
| 52 | + self.window.backgroundColor = [UIColor whiteColor]; |
| 53 | + |
| 54 | + //网络指示标志 |
| 55 | + [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; |
| 56 | + |
| 57 | + //设置导航条样式 |
| 58 | + [self customizeInterface]; |
| 59 | + |
| 60 | + |
| 61 | + if ([Login isLogin]) { |
| 62 | + [self setupTabViewController]; |
| 63 | + NSDictionary *remoteNotification = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]; |
| 64 | + if (remoteNotification) { |
| 65 | + NSDictionary *apsInfo = [remoteNotification objectForKey:@"aps"]; |
| 66 | + [BaseViewController handleNotificationInfo:apsInfo]; |
| 67 | + } |
| 68 | + }else{ |
| 69 | + [UIApplication sharedApplication].applicationIconBadgeNumber = 0; |
| 70 | + [self setupLoginViewController]; |
| 71 | + } |
| 72 | + |
| 73 | + [self.window makeKeyAndVisible]; |
| 74 | +// UMENG 统计 |
| 75 | + [MobClick startWithAppkey:kUmeng_AppKey reportPolicy:BATCH channelId:nil]; |
| 76 | + |
| 77 | +// UMENG 推送 |
| 78 | + //set AppKey and LaunchOptions |
| 79 | + [UMessage startWithAppkey:kUmeng_AppKey_Push launchOptions:launchOptions]; |
| 80 | + [UMessage setBadgeClear:NO]; |
| 81 | + [UMessage setLogEnabled:NO]; |
| 82 | + |
| 83 | +// 信鸽推送 |
| 84 | + [XGPush startApp:kXGPush_Id appKey:kXGPush_Key]; |
| 85 | + [Login setXGAccountWithCurUser]; |
| 86 | + //注销之后需要再次注册前的准备 |
| 87 | + void (^successCallback)(void) = ^(void){ |
| 88 | + //如果变成需要注册状态 |
| 89 | + if(![XGPush isUnRegisterStatus] && [Login isLogin]){ |
| 90 | + [self registerPush]; |
| 91 | + } |
| 92 | + }; |
| 93 | + [XGPush initForReregister:successCallback]; |
| 94 | + |
| 95 | + //[XGPush registerPush]; //注册Push服务,注册后才能收到推送 |
| 96 | + |
| 97 | + //推送反馈(app不在前台运行时,点击推送激活时。统计而已) |
| 98 | + [XGPush handleLaunching:launchOptions]; |
| 99 | + |
| 100 | + return YES; |
| 101 | +} |
| 102 | + |
| 103 | +- (void)applicationWillResignActive:(UIApplication *)application |
| 104 | +{ |
| 105 | + // 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. |
| 106 | + // 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. |
| 107 | +} |
| 108 | + |
| 109 | +- (void)applicationDidEnterBackground:(UIApplication *)application |
| 110 | +{ |
| 111 | + // 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. |
| 112 | + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. |
| 113 | + [ImageSizeManager save]; |
| 114 | +} |
| 115 | + |
| 116 | +- (void)applicationWillEnterForeground:(UIApplication *)application |
| 117 | +{ |
| 118 | + // 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. |
| 119 | +} |
| 120 | + |
| 121 | +- (void)applicationDidBecomeActive:(UIApplication *)application |
| 122 | +{ |
| 123 | + // 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. |
| 124 | +} |
| 125 | + |
| 126 | +- (void)applicationWillTerminate:(UIApplication *)application |
| 127 | +{ |
| 128 | + // Saves changes in the application's managed object context before the application terminates. |
| 129 | + [self saveContext]; |
| 130 | + [ImageSizeManager save]; |
| 131 | +} |
| 132 | +#pragma mark - Umeng Message |
| 133 | +- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken |
| 134 | +{ |
| 135 | + [UMessage registerDeviceToken:deviceToken]; |
| 136 | + [Login addUmengAliasWithCurUser:[Login isLogin]]; |
| 137 | + |
| 138 | + NSString * deviceTokenStr = [XGPush registerDevice:deviceToken]; |
| 139 | + NSLog(@"deviceTokenStr : %@", deviceTokenStr); |
| 140 | +} |
| 141 | + |
| 142 | +- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo |
| 143 | +{ |
| 144 | + NSLog(@"didReceiveRemoteNotification-userInfo:-----\n%@", userInfo); |
| 145 | + |
| 146 | + if ([application applicationState] == UIApplicationStateInactive) { |
| 147 | + //If the application state was inactive, this means the user pressed an action button |
| 148 | + // from a notification. |
| 149 | + [XGPush handleReceiveNotification:userInfo]; |
| 150 | + |
| 151 | + [BaseViewController handleNotificationInfo:userInfo]; |
| 152 | + }else if ([application applicationState] == UIApplicationStateActive){ |
| 153 | +// kTipAlert(@"UIApplicationStateActive"); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#pragma mark - Methods Private |
| 158 | +- (void)setupLoginViewController{ |
| 159 | + LoginViewController *loginVC = [[LoginViewController alloc] init]; |
| 160 | + [self.window setRootViewController:[[UINavigationController alloc] initWithRootViewController:loginVC]]; |
| 161 | +} |
| 162 | + |
| 163 | +- (void)setupTabViewController{ |
| 164 | + RootTabViewController *rootVC = [[RootTabViewController alloc] init]; |
| 165 | + [self.window setRootViewController:rootVC]; |
| 166 | +} |
| 167 | + |
| 168 | +- (void)customizeInterface { |
| 169 | + //设置Nav的背景色和title色 |
| 170 | + UINavigationBar *navigationBarAppearance = [UINavigationBar appearance]; |
| 171 | + NSDictionary *textAttributes = nil; |
| 172 | + if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { |
| 173 | + [navigationBarAppearance setTintColor:[UIColor whiteColor]];//返回按钮的箭头颜色 |
| 174 | + [[UITextField appearance] setTintColor:[UIColor colorWithHexString:@"0x3bbc79"]];//设置UITextField的光标颜色 |
| 175 | + [[UITextView appearance] setTintColor:[UIColor colorWithHexString:@"0x3bbc79"]];//设置UITextView的光标颜色 |
| 176 | + [[UISearchBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0xe5e5e5"]] forBarPosition:0 barMetrics:UIBarMetricsDefault]; |
| 177 | + |
| 178 | + textAttributes = @{ |
| 179 | + NSFontAttributeName: [UIFont boldSystemFontOfSize:kNavTitleFontSize], |
| 180 | + NSForegroundColorAttributeName: [UIColor whiteColor], |
| 181 | + }; |
| 182 | + } else { |
| 183 | +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0 |
| 184 | + [[UISearchBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0xe5e5e5"]]]; |
| 185 | + |
| 186 | + textAttributes = @{ |
| 187 | + UITextAttributeFont: [UIFont boldSystemFontOfSize:kNavTitleFontSize], |
| 188 | + UITextAttributeTextColor: [UIColor whiteColor], |
| 189 | + UITextAttributeTextShadowColor: [UIColor clearColor], |
| 190 | + UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero], |
| 191 | + }; |
| 192 | +#endif |
| 193 | + } |
| 194 | + [navigationBarAppearance setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0x28303b"]] forBarMetrics:UIBarMetricsDefault]; |
| 195 | + [navigationBarAppearance setTitleTextAttributes:textAttributes]; |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +#pragma mark - Methods Core Data |
| 200 | + |
| 201 | +- (void)saveContext |
| 202 | +{ |
| 203 | + NSError *error = nil; |
| 204 | + NSManagedObjectContext *managedObjectContext = self.managedObjectContext; |
| 205 | + if (managedObjectContext != nil) { |
| 206 | + if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { |
| 207 | + // Replace this implementation with code to handle the error appropriately. |
| 208 | + // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. |
| 209 | + NSLog(@"Unresolved error %@, %@", error, [error userInfo]); |
| 210 | + abort(); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +#pragma mark - Core Data stack |
| 216 | + |
| 217 | +// Returns the managed object context for the application. |
| 218 | +// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. |
| 219 | +- (NSManagedObjectContext *)managedObjectContext |
| 220 | +{ |
| 221 | + if (_managedObjectContext != nil) { |
| 222 | + return _managedObjectContext; |
| 223 | + } |
| 224 | + |
| 225 | + NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; |
| 226 | + if (coordinator != nil) { |
| 227 | + _managedObjectContext = [[NSManagedObjectContext alloc] init]; |
| 228 | + [_managedObjectContext setPersistentStoreCoordinator:coordinator]; |
| 229 | + } |
| 230 | + return _managedObjectContext; |
| 231 | +} |
| 232 | + |
| 233 | +// Returns the managed object model for the application. |
| 234 | +// If the model doesn't already exist, it is created from the application's model. |
| 235 | +- (NSManagedObjectModel *)managedObjectModel |
| 236 | +{ |
| 237 | + if (_managedObjectModel != nil) { |
| 238 | + return _managedObjectModel; |
| 239 | + } |
| 240 | + NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Coding_iOS" withExtension:@"momd"]; |
| 241 | + _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; |
| 242 | + return _managedObjectModel; |
| 243 | +} |
| 244 | + |
| 245 | +// Returns the persistent store coordinator for the application. |
| 246 | +// If the coordinator doesn't already exist, it is created and the application's store added to it. |
| 247 | +- (NSPersistentStoreCoordinator *)persistentStoreCoordinator |
| 248 | +{ |
| 249 | + if (_persistentStoreCoordinator != nil) { |
| 250 | + return _persistentStoreCoordinator; |
| 251 | + } |
| 252 | + |
| 253 | + NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Coding_iOS.sqlite"]; |
| 254 | + |
| 255 | + NSError *error = nil; |
| 256 | + _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; |
| 257 | + if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { |
| 258 | + /* |
| 259 | + Replace this implementation with code to handle the error appropriately. |
| 260 | + |
| 261 | + abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. |
| 262 | + |
| 263 | + Typical reasons for an error here include: |
| 264 | + * The persistent store is not accessible; |
| 265 | + * The schema for the persistent store is incompatible with current managed object model. |
| 266 | + Check the error message to determine what the actual problem was. |
| 267 | + |
| 268 | + |
| 269 | + If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. |
| 270 | + |
| 271 | + If you encounter schema incompatibility errors during development, you can reduce their frequency by: |
| 272 | + * Simply deleting the existing store: |
| 273 | + [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] |
| 274 | + |
| 275 | + * Performing automatic lightweight migration by passing the following dictionary as the options parameter: |
| 276 | + @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} |
| 277 | + |
| 278 | + Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. |
| 279 | + |
| 280 | + */ |
| 281 | + NSLog(@"Unresolved error %@, %@", error, [error userInfo]); |
| 282 | + abort(); |
| 283 | + } |
| 284 | + |
| 285 | + return _persistentStoreCoordinator; |
| 286 | +} |
| 287 | + |
| 288 | +#pragma mark - Application's Documents directory |
| 289 | + |
| 290 | +// Returns the URL to the application's Documents directory. |
| 291 | +- (NSURL *)applicationDocumentsDirectory |
| 292 | +{ |
| 293 | + return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; |
| 294 | +} |
| 295 | + |
| 296 | +@end |
0 commit comments