// // AppDelegate.m // YouHuiProject // // Created by 小花 on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "AppDelegate.h" #import "YHTabbarController.h" #import #import "UMMobClick/MobClick.h" #import #import "MiPushSDK.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self setDefauleSex]; [self setupAlibcSDK]; [self setUpSVProgressHUD]; [self setBugly]; [self setUMAnalisis]; [self setupKeyWindow]; [self setupMiPush]; [self cancelIconBadgeNumber]; return YES; } - (void)setupMiPush { [MiPushSDK registerMiPush:self]; } - (void)setBugly { [Bugly startWithAppId:BUGLY_ID]; } -(void)setUMAnalisis { UMConfigInstance.appKey = UMAPP_KEY; UMConfigInstance.channelId = @"App Store"; [MobClick startWithConfigure:UMConfigInstance]; [MobClick setLogEnabled:YES]; } - (void)cancelIconBadgeNumber { // 注册显示应用程序BadgeNumber的通知 UIApplication *application = [UIApplication sharedApplication]; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; [application registerUserNotificationSettings:settings]; if (application.applicationIconBadgeNumber > 0) { application.applicationIconBadgeNumber = 0; } } /** 设置默认性别 */ - (void)setDefauleSex { NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey]; if (sex.length == 0 || [sex isEqualToString:@""]) { //默认性别为女 [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:UserSexKey]; } } /** 配置阿里百川SDK */ - (void)setupAlibcSDK { // 百川平台基础SDK初始化,加载并初始化各个业务能力插件 [[AlibcTradeSDK sharedInstance] asyncInitWithSuccess:^{ } failure:^(NSError *error) { NSLog(@"Init failed: %@", error.description); }]; // 开发阶段打开日志开关,方便排查错误信息 //默认调试模式打开日志,release关闭,可以不调用下面的函数 // [[AlibcTradeSDK sharedInstance] setDebugLogOpen:YES]; // 配置全局的淘客参数 //如果没有阿里妈妈的淘客账号,setTaokeParams函数需要调用 AlibcTradeTaokeParams *taokeParams = [[AlibcTradeTaokeParams alloc] init]; taokeParams.pid = ALTK_PID; //mm_XXXXX为你自己申请的阿里妈妈淘客pid [[AlibcTradeSDK sharedInstance] setTaokeParams:taokeParams]; //设置全局的app标识,在电商模块里等同于isv_code //没有申请过isv_code的接入方,默认不需要调用该函数 [[AlibcTradeSDK sharedInstance] setISVCode:@"your_isv_code"]; // 设置全局配置,是否强制使用h5 [[AlibcTradeSDK sharedInstance] setIsForceH5:NO]; } - (void)setupKeyWindow { UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; YHTabbarController *tab = [[YHTabbarController alloc] init]; window.rootViewController = tab; window.backgroundColor = [UIColor whiteColor]; [window makeKeyAndVisible]; self.window = window; } - (void)setUpSVProgressHUD { [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark]; [SVProgressHUD setMinimumDismissTimeInterval:2]; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ // 新接口写法 if (![[AlibcTradeSDK sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]) { // 处理其他app跳转到自己的app } return YES; } // ios9 新api - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary *)options { // 新接口写法 if (@available(iOS 9.0, *)) { if (![[AlibcTradeSDK sharedInstance] application:application openURL:url options:options]) { //处理其他app跳转到自己的app,如果百川处理过会返回YES } } else { // Fallback on earlier versions } return YES; } #pragma mark 注册push服务. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"APNS token: %@", [deviceToken description]); // 注册APNS成功, 注册deviceToken [MiPushSDK bindDeviceToken:deviceToken]; } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSLog(@"APNS error: %@", err); // 注册APNS失败. // 自行处理. } #pragma mark MiPushSDKDelegate - (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data { // 请求成功 // 可在此获取regId if ([selector isEqualToString:@"bindDeviceToken:"]) { NSLog(@"regid = %@", data[@"regid"]); } } - (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data { // 请求失败 } #pragma mark Local And Push Notification - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"APNS notify: %@", userInfo); // 当同时启动APNs与内部长连接时, 把两处收到的消息合并. 通过miPushReceiveNotification返回 [MiPushSDK handleReceiveRemoteNotification:userInfo]; } // iOS10新加入的回调方法 // 应用在前台收到通知 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"APNS notify: %@", userInfo);; [MiPushSDK handleReceiveRemoteNotification:userInfo]; } [self cancelIconBadgeNumber]; // completionHandler(UNNotificationPresentationOptionAlert); } // 点击通知进入应用 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"APNS notify: %@", userInfo); [MiPushSDK handleReceiveRemoteNotification:userInfo]; } completionHandler(); } #pragma mark MiPushSDKDelegate - (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 invalidate graphics rendering callbacks. 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. [self cancelIconBadgeNumber]; } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } - (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:. } @end