// // AppDelegate.m // YouHuiProject // // Created by 小花 on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "AppDelegate.h" #import "LZMTabbarController.h" #import #import "UMMobClick/MobClick.h" #import #import "MiPushSDK.h" #import "RouteManager.h" #import "LZMGuideView.h" #import #import #import "WXApiManager.h" #import "LZMSearchPopView.h" #import "CCAlertShowView.h" #import "LZMUpdatePopView.h" #import "LZMGoodDetailViewController.h" #import "LZMGoodListViewController.h" #import "KBAdWebViewController.h" //#import #import "HYUMShareManager.h" #import "LZMFindSearchResultViewController.h" @interface AppDelegate () < MiPushSDKDelegate, UNUserNotificationCenterDelegate, UIApplicationDelegate, WXApiDelegate > @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self setUpWeChat]; //微信sdk [self setDefauleSex]; //设置默认性别 [self setupAlibcSDK]; //阿里百川电商sdk [self setUpSVProgressHUD]; //设置loading指示器 [self setBugly]; //设置bugly [self setUMAnalisis]; //设置友盟统计 [self setUMShareAndWeChartInfo]; //设置友盟分享和微信appkey和秘钥 [self setupKeyWindow]; // 初始化window [self firstLaunchingGuide]; // 首次安装性别引导 [self setupMiPush]; //小米推送 [self cancelIconBadgeNumber]; //处理角标 [self addRouteManager]; //初始化路由跳转 [self getRemoteNotificationDictWithDict:launchOptions]; //设置点击推送跳转 return YES; } -(void)setUMWeChat{ [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WX_APPID appSecret:WX_APP_SECRET redirectURL:nil]; } - (void)setUpWeChat { [WXApi registerApp:WX_APPID]; } - (void)setupMiPush { [MiPushSDK registerMiPush:self]; } - (void)setBugly { [Bugly startWithAppId:BUGLY_ID]; } -(void)setUMAnalisis { UMConfigInstance.appKey = UMAPP_KEY; UMConfigInstance.channelId = @"App Store"; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; [MobClick setAppVersion:app_Version]; [MobClick startWithConfigure:UMConfigInstance]; [MobClick setLogEnabled:YES]; } -(void)setUMShareAndWeChartInfo{ [[HYUMShareManager shareInstance] setUMShare]; } - (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]; } } /** 首次安装性别引导 */ - (void)firstLaunchingGuide { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString *launched = [userDefaults objectForKey:@"launched"]; if (!launched) { [self creatGuideView]; launched = @"YES"; [userDefaults setObject:launched forKey:@"launched"]; [userDefaults synchronize]; } } - (void)creatGuideView { LZMGuideView *guideView = [[LZMGuideView alloc] initWithFrame:self.window.bounds]; [self.window addSubview:guideView]; } /** 配置阿里百川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 { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; LZMTabbarController *tab = [[LZMTabbarController alloc] init]; self.window.rootViewController = tab; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; } - (void)setUpSVProgressHUD { [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark]; [SVProgressHUD setMinimumDismissTimeInterval:2]; } - (void)addRouteManager { [RouteManager registerRouteWithScheme:SchemeKey]; } /** 点击通知跳转到指定页面 */ - (void)getRemoteNotificationDictWithDict:(NSDictionary *)tfdic { NSDictionary *remoteNotification = [tfdic objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; [self jumpViewController:remoteNotification]; } - (void)jumpViewController:(NSDictionary *)remoteNotification { NSString *pushType = remoteNotification[@"pushType"]; if ([pushType isEqualToString:@"1000"]) { //商品详情 LZMEventModel *evevtModel = [[LZMEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:pushAction]; LZMGoodDetailViewController *detail = [[LZMGoodDetailViewController alloc] init]; if ([remoteNotification.allKeys containsObject:@"goods_id"]) { DetailRequestModel *requestModel = [[DetailRequestModel alloc] init]; [DetailRequestModel setValuesForKeysWithDictionary:remoteNotification]; detail.requestModel = requestModel; } detail.eventModel = evevtModel; [[self currentViewController].navigationController pushViewController:detail animated:YES]; }else if ([pushType isEqualToString:@"1001"]){ //列表 LZMGoodListViewController *list = [[LZMGoodListViewController alloc] init]; if ([remoteNotification.allKeys containsObject:@"gid"]) { list.cate_id = remoteNotification[@"gid"]; }else { list.cate_id = @""; } if ([remoteNotification.allKeys containsObject:@"name"]) { list.name = remoteNotification[@"name"]; }else { list.name = @""; } list.topRequest = 2; [[self currentViewController].navigationController pushViewController:list animated:YES]; }else if ([pushType isEqualToString:@"1002"]){ KBAdWebViewController *web = [[KBAdWebViewController alloc] init]; if ([remoteNotification.allKeys containsObject:@"url"]) { web.url = remoteNotification[@"url"]; }else { web.url = @""; } //h5 [[self currentViewController].navigationController pushViewController:web animated:YES]; } } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation]; NSString *urlStr = url.absoluteString; if ([urlStr hasPrefix:@"YHProject"]) { return [[JLRoutes routesForScheme:SchemeKey] routeURL:url]; } // if ([urlStr hasPrefix:WX_APPID]) { // return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]]; // } // 新接口写法 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 { //微信分享 [[UMSocialManager defaultManager] handleOpenURL:url options:options]; NSString *urlStr = url.absoluteString; if ([urlStr hasPrefix:@"YHProject"]) { return [[JLRoutes routesForScheme:SchemeKey] routeURL:url]; } // if ([urlStr hasPrefix:WX_APPID]) { // return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]]; // } // 新接口写法 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 options))completionHandler __IOS_AVAILABLE(10.0) { 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(^)(void))completionHandler __IOS_AVAILABLE(10.0) { NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"APNS notify: %@", userInfo); [MiPushSDK handleReceiveRemoteNotification:userInfo]; } [self jumpViewController: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 { [self checkPasteboardText]; //识别剪贴板 } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } #pragma mark ---- - (void)checkPasteboardText { UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; NSString *pasteText = pasteBoard.string; if (pasteText.length > 10) { LZMSearchPopView *popView = [[LZMSearchPopView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(310), Fitsize(246)) searchText:pasteText]; CCAlertShowView *showView = [CCAlertShowView showAlertViewWithView:popView backgroundDismissEnable:YES]; popView.cancelClick = ^{ [showView dismiss]; }; popView.searchlClick = ^{ LZMFindSearchResultViewController *result = [[LZMFindSearchResultViewController alloc] init]; result.searchName = pasteText; result.hiddenNavigationWhenPop = YES; [[self currentViewController].navigationController pushViewController:result animated:YES]; [showView dismiss]; }; pasteBoard.string = @""; } } - (UIViewController *)currentViewController{ UIViewController * currVC = nil; UIWindow *window = [UIApplication sharedApplication].delegate.window; UIViewController * Rootvc = window.rootViewController; do { if ([Rootvc isKindOfClass:[UINavigationController class]]) { UINavigationController * nav = (UINavigationController *)Rootvc; UIViewController * v = [nav.viewControllers lastObject]; currVC = v; Rootvc = v.presentedViewController; continue; }else if([Rootvc isKindOfClass:[UITabBarController class]]){ UITabBarController * tabVC = (UITabBarController *)Rootvc; currVC = tabVC; Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex]; continue; } } while (Rootvc!=nil); return currVC; } @end