天天省钱快报

AppDelegate.m 17KB

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