口袋优选

AppDelegate.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. //
  2. // AppDelegate.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "KBTabbarController.h"
  10. #import <AlibcTradeSDK/AlibcTradeSDK.h>
  11. #import "UMMobClick/MobClick.h"
  12. #import <Bugly/Bugly.h>
  13. #import "MiPushSDK.h"
  14. #import "RouteManager.h"
  15. #import "KBGuideView.h"
  16. #import <JLRoutes.h>
  17. #import <WXApi.h>
  18. #import "WXApiManager.h"
  19. #import "KBSearchPopView.h"
  20. #import "CCAlertShowView.h"
  21. #import "KBUpdatePopView.h"
  22. #import "KBGoodDetailViewController.h"
  23. #import "KBGoodListViewController.h"
  24. #import "KBAdWebViewController.h"
  25. //#import <UMSocialCore/UMSocialCore.h>
  26. #import "HYUMShareManager.h"
  27. #import "KBFindSearchResultViewController.h"
  28. @interface AppDelegate ()
  29. <
  30. MiPushSDKDelegate,
  31. UNUserNotificationCenterDelegate,
  32. UIApplicationDelegate,
  33. WXApiDelegate
  34. >
  35. @end
  36. @implementation AppDelegate
  37. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  38. // Override point for customization after application launch.
  39. [self setUpWeChat]; //微信sdk
  40. [self setDefauleSex]; //设置默认性别
  41. [self setupAlibcSDK]; //阿里百川电商sdk
  42. [self setUpSVProgressHUD]; //设置loading指示器
  43. [self setBugly]; //设置bugly
  44. [self setUMAnalisis]; //设置友盟统计
  45. [self setUMShareAndWeChartInfo]; //设置友盟分享和微信appkey和秘钥
  46. [self setupKeyWindow]; // 初始化window
  47. [self firstLaunchingGuide]; // 首次安装性别引导
  48. [self setupMiPush]; //小米推送
  49. [self cancelIconBadgeNumber]; //处理角标
  50. [self addRouteManager]; //初始化路由跳转
  51. [self getRemoteNotificationDictWithDict:launchOptions]; //设置点击推送跳转
  52. return YES;
  53. }
  54. -(void)setUMWeChat{
  55. [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WX_APPID appSecret:WX_APP_SECRET redirectURL:nil];
  56. }
  57. - (void)setUpWeChat {
  58. [WXApi registerApp:WX_APPID];
  59. }
  60. - (void)setupMiPush {
  61. [MiPushSDK registerMiPush:self];
  62. }
  63. - (void)setBugly {
  64. [Bugly startWithAppId:BUGLY_ID];
  65. }
  66. -(void)setUMAnalisis
  67. {
  68. UMConfigInstance.appKey = UMAPP_KEY;
  69. UMConfigInstance.channelId = @"App Store";
  70. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  71. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  72. [MobClick setAppVersion:app_Version];
  73. [MobClick startWithConfigure:UMConfigInstance];
  74. [MobClick setLogEnabled:YES];
  75. }
  76. -(void)setUMShareAndWeChartInfo{
  77. [[HYUMShareManager shareInstance] setUMShare];
  78. }
  79. - (void)cancelIconBadgeNumber {
  80. // 注册显示应用程序BadgeNumber的通知
  81. UIApplication *application = [UIApplication sharedApplication];
  82. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
  83. [application registerUserNotificationSettings:settings];
  84. if (application.applicationIconBadgeNumber > 0) {
  85. application.applicationIconBadgeNumber = 0;
  86. }
  87. }
  88. /**
  89. 设置默认性别
  90. */
  91. - (void)setDefauleSex {
  92. NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  93. if (sex.length == 0 || [sex isEqualToString:@""]) {
  94. //默认性别为女
  95. [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:UserSexKey];
  96. }
  97. }
  98. /**
  99. 首次安装性别引导
  100. */
  101. - (void)firstLaunchingGuide {
  102. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  103. NSString *launched = [userDefaults objectForKey:@"launched"];
  104. if (!launched)
  105. {
  106. [self creatGuideView];
  107. launched = @"YES";
  108. [userDefaults setObject:launched forKey:@"launched"];
  109. [userDefaults synchronize];
  110. }
  111. }
  112. - (void)creatGuideView {
  113. KBGuideView *guideView = [[KBGuideView alloc] initWithFrame:self.window.bounds];
  114. [self.window addSubview:guideView];
  115. }
  116. /**
  117. 配置阿里百川SDK
  118. */
  119. - (void)setupAlibcSDK {
  120. // 百川平台基础SDK初始化,加载并初始化各个业务能力插件
  121. [[AlibcTradeSDK sharedInstance] asyncInitWithSuccess:^{
  122. } failure:^(NSError *error) {
  123. NSLog(@"Init failed: %@", error.description);
  124. }];
  125. // 开发阶段打开日志开关,方便排查错误信息
  126. //默认调试模式打开日志,release关闭,可以不调用下面的函数
  127. // [[AlibcTradeSDK sharedInstance] setDebugLogOpen:YES];
  128. // 配置全局的淘客参数
  129. //如果没有阿里妈妈的淘客账号,setTaokeParams函数需要调用
  130. AlibcTradeTaokeParams *taokeParams = [[AlibcTradeTaokeParams alloc] init];
  131. taokeParams.pid = ALTK_PID; //mm_XXXXX为你自己申请的阿里妈妈淘客pid
  132. [[AlibcTradeSDK sharedInstance] setTaokeParams:taokeParams];
  133. //设置全局的app标识,在电商模块里等同于isv_code
  134. //没有申请过isv_code的接入方,默认不需要调用该函数
  135. [[AlibcTradeSDK sharedInstance] setISVCode:@"your_isv_code"];
  136. // 设置全局配置,是否强制使用h5
  137. [[AlibcTradeSDK sharedInstance] setIsForceH5:NO];
  138. }
  139. - (void)setupKeyWindow {
  140. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  141. KBTabbarController *tab = [[KBTabbarController alloc] init];
  142. self.window.rootViewController = tab;
  143. self.window.backgroundColor = [UIColor whiteColor];
  144. [self.window makeKeyAndVisible];
  145. }
  146. - (void)setUpSVProgressHUD {
  147. [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
  148. [SVProgressHUD setMinimumDismissTimeInterval:2];
  149. }
  150. - (void)addRouteManager {
  151. [RouteManager registerRouteWithScheme:SchemeKey];
  152. }
  153. /**
  154. 点击通知跳转到指定页面
  155. */
  156. - (void)getRemoteNotificationDictWithDict:(NSDictionary *)tfdic {
  157. NSDictionary *remoteNotification = [tfdic objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  158. [self jumpViewController:remoteNotification];
  159. }
  160. - (void)jumpViewController:(NSDictionary *)remoteNotification {
  161. NSString *pushType = remoteNotification[@"pushType"];
  162. if ([pushType isEqualToString:@"1000"]) {
  163. //商品详情
  164. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:pushAction];
  165. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  166. if ([remoteNotification.allKeys containsObject:@"goods_id"]) {
  167. DetailRequestModel *requestModel = [[DetailRequestModel alloc] init];
  168. [DetailRequestModel setValuesForKeysWithDictionary:remoteNotification];
  169. detail.requestModel = requestModel;
  170. }
  171. detail.eventModel = evevtModel;
  172. [[self currentViewController].navigationController pushViewController:detail animated:YES];
  173. }else if ([pushType isEqualToString:@"1001"]){
  174. //列表
  175. KBGoodListViewController *list = [[KBGoodListViewController alloc] init];
  176. if ([remoteNotification.allKeys containsObject:@"gid"]) {
  177. list.cate_id = remoteNotification[@"gid"];
  178. }else {
  179. list.cate_id = @"";
  180. }
  181. if ([remoteNotification.allKeys containsObject:@"name"]) {
  182. list.name = remoteNotification[@"name"];
  183. }else {
  184. list.name = @"";
  185. }
  186. list.topRequest = 2;
  187. [[self currentViewController].navigationController pushViewController:list animated:YES];
  188. }else if ([pushType isEqualToString:@"1002"]){
  189. KBAdWebViewController *web = [[KBAdWebViewController alloc] init];
  190. if ([remoteNotification.allKeys containsObject:@"url"]) {
  191. web.url = remoteNotification[@"url"];
  192. }else {
  193. web.url = @"";
  194. }
  195. //h5
  196. [[self currentViewController].navigationController pushViewController:web animated:YES];
  197. }
  198. }
  199. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
  200. [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
  201. NSString *urlStr = url.absoluteString;
  202. if ([urlStr hasPrefix:@"YHProject"]) {
  203. return [[JLRoutes routesForScheme:SchemeKey] routeURL:url];
  204. }
  205. // if ([urlStr hasPrefix:WX_APPID]) {
  206. // return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
  207. // }
  208. // 新接口写法
  209. if (![[AlibcTradeSDK sharedInstance] application:application
  210. openURL:url
  211. sourceApplication:sourceApplication
  212. annotation:annotation]) {
  213. // 处理其他app跳转到自己的app
  214. }
  215. return YES;
  216. }
  217. // ios9 新api
  218. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
  219. //微信分享
  220. [[UMSocialManager defaultManager] handleOpenURL:url options:options];
  221. NSString *urlStr = url.absoluteString;
  222. if ([urlStr hasPrefix:@"YHProject"]) {
  223. return [[JLRoutes routesForScheme:SchemeKey] routeURL:url];
  224. }
  225. // if ([urlStr hasPrefix:WX_APPID]) {
  226. // return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
  227. // }
  228. // 新接口写法
  229. if (@available(iOS 9.0, *)) {
  230. if (![[AlibcTradeSDK sharedInstance] application:application
  231. openURL:url
  232. options:options]) {
  233. //处理其他app跳转到自己的app,如果百川处理过会返回YES
  234. }
  235. } else {
  236. // Fallback on earlier versions
  237. }
  238. return YES;
  239. }
  240. #pragma mark 注册push服务.
  241. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  242. {
  243. NSLog(@"APNS token: %@", [deviceToken description]);
  244. // 注册APNS成功, 注册deviceToken
  245. [MiPushSDK bindDeviceToken:deviceToken];
  246. }
  247. - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
  248. {
  249. NSLog(@"APNS error: %@", err);
  250. // 注册APNS失败.
  251. // 自行处理.
  252. }
  253. #pragma mark MiPushSDKDelegate
  254. - (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data
  255. {
  256. // 请求成功
  257. // 可在此获取regId
  258. if ([selector isEqualToString:@"bindDeviceToken:"]) {
  259. NSLog(@"regid = %@", data[@"regid"]);
  260. }
  261. }
  262. - (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data
  263. {
  264. // 请求失败
  265. }
  266. #pragma mark Local And Push Notification
  267. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  268. {
  269. NSLog(@"APNS notify: %@", userInfo);
  270. // 当同时启动APNs与内部长连接时, 把两处收到的消息合并. 通过miPushReceiveNotification返回
  271. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  272. }
  273. // iOS10新加入的回调方法
  274. // 应用在前台收到通知
  275. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) {
  276. NSDictionary * userInfo = notification.request.content.userInfo;
  277. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  278. NSLog(@"APNS notify: %@", userInfo);;
  279. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  280. }
  281. [self cancelIconBadgeNumber];
  282. // completionHandler(UNNotificationPresentationOptionAlert);
  283. }
  284. // 点击通知进入应用
  285. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) {
  286. NSDictionary * userInfo = response.notification.request.content.userInfo;
  287. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  288. NSLog(@"APNS notify: %@", userInfo);
  289. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  290. }
  291. [self jumpViewController:userInfo];
  292. completionHandler();
  293. }
  294. #pragma mark MiPushSDKDelegate
  295. - (void)applicationWillResignActive:(UIApplication *)application {
  296. // 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.
  297. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  298. }
  299. - (void)applicationDidEnterBackground:(UIApplication *)application {
  300. // 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.
  301. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  302. [self cancelIconBadgeNumber];
  303. }
  304. - (void)applicationWillEnterForeground:(UIApplication *)application {
  305. // 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.
  306. }
  307. - (void)applicationDidBecomeActive:(UIApplication *)application {
  308. [self checkPasteboardText]; //识别剪贴板
  309. }
  310. - (void)applicationWillTerminate:(UIApplication *)application {
  311. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  312. }
  313. #pragma mark ----
  314. - (void)checkPasteboardText {
  315. UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
  316. NSString *pasteText = pasteBoard.string;
  317. if (pasteText.length > 10) {
  318. KBSearchPopView *popView = [[KBSearchPopView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(310), Fitsize(246)) searchText:pasteText];
  319. CCAlertShowView *showView = [CCAlertShowView showAlertViewWithView:popView backgroundDismissEnable:YES];
  320. popView.cancelClick = ^{
  321. [showView dismiss];
  322. };
  323. popView.searchlClick = ^{
  324. KBFindSearchResultViewController *result = [[KBFindSearchResultViewController alloc] init];
  325. result.searchName = pasteText;
  326. result.hiddenNavigationWhenPop = YES;
  327. [[self currentViewController].navigationController pushViewController:result animated:YES];
  328. [showView dismiss];
  329. };
  330. pasteBoard.string = @"";
  331. }
  332. }
  333. - (UIViewController *)currentViewController{
  334. UIViewController * currVC = nil;
  335. UIWindow *window = [UIApplication sharedApplication].delegate.window;
  336. UIViewController * Rootvc = window.rootViewController;
  337. do {
  338. if ([Rootvc isKindOfClass:[UINavigationController class]]) {
  339. UINavigationController * nav = (UINavigationController *)Rootvc;
  340. UIViewController * v = [nav.viewControllers lastObject];
  341. currVC = v;
  342. Rootvc = v.presentedViewController;
  343. continue;
  344. }else if([Rootvc isKindOfClass:[UITabBarController class]]){
  345. UITabBarController * tabVC = (UITabBarController *)Rootvc;
  346. currVC = tabVC;
  347. Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
  348. continue;
  349. }
  350. } while (Rootvc!=nil);
  351. return currVC;
  352. }
  353. @end