酷店

AppDelegate.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // AppDelegate.m
  3. // KuDianProject
  4. //
  5. // Created by 学丽 on 2019/7/4.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "KDPTabBarVC.h"
  10. #import "KDPAccountTool.h"
  11. #import "KDPWelcomePageViewController.h"
  12. #import "KDPNoticeViewController.h"
  13. #import<AdSupport/AdSupport.h>
  14. #include <ifaddrs.h>
  15. #include <arpa/inet.h>
  16. #include <net/if.h>
  17. @interface AppDelegate ()
  18. @end
  19. @implementation AppDelegate
  20. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  21. [self setUpKeyWindow];
  22. [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  23. [self setUpUM];
  24. [self setUpMiPush];
  25. [self IPADDWithKD];
  26. return YES;
  27. }
  28. - (void)setUpKeyWindow{
  29. self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  30. self.window.backgroundColor=[UIColor whiteColor];
  31. self.window.rootViewController=[[KDPTabBarVC alloc]init];
  32. [self.window makeKeyAndVisible];
  33. KDPAccountModel *accountModel = [KDPAccountTool account];
  34. if (![KDPAccountTool isLogin] || [accountModel.has_kwai integerValue] != 1) {
  35. [self.window.rootViewController presentViewController:[[UINavigationController alloc]initWithRootViewController:[[KDPWelcomePageViewController alloc]init]] animated:YES completion:nil];
  36. }
  37. }
  38. #pragma mark - 推送
  39. - (void)setUpMiPush{
  40. [MiPushSDK registerMiPush:self];
  41. }
  42. #pragma mark--友盟统计
  43. -(void)setUpUM
  44. {
  45. UMConfigInstance.appKey=UMENG_KEY;
  46. UMConfigInstance.channelId=@"App Store";
  47. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  48. NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
  49. [MobClick setAppVersion:appVersion];
  50. [MobClick startWithConfigure:UMConfigInstance];
  51. [MobClick setLogEnabled:YES];
  52. }
  53. #pragma mark 注册push服务.
  54. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  55. {
  56. NSLog(@"APNS token: %@", [deviceToken description]);
  57. // 注册APNS成功, 注册deviceToken
  58. [MiPushSDK bindDeviceToken:deviceToken];
  59. }
  60. - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
  61. {
  62. NSLog(@"APNS error: %@", err);
  63. // 注册APNS失败.
  64. // 自行处理.
  65. }
  66. #pragma mark MiPushSDKDelegate
  67. - (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data
  68. {
  69. // 请求成功
  70. // 可在此获取regId
  71. if ([selector isEqualToString:@"bindDeviceToken:"]) {
  72. NSLog(@"regid = %@", data[@"regid"]);
  73. }
  74. }
  75. - (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data
  76. {
  77. // 请求失败
  78. }
  79. #pragma mark Local And Push Notification
  80. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  81. {
  82. NSLog(@"APNS notify: %@", userInfo);
  83. // 当同时启动APNs与内部长连接时, 把两处收到的消息合并. 通过miPushReceiveNotification返回
  84. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  85. }
  86. // iOS10新加入的回调方法
  87. // 应用在前台收到通知
  88. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) {
  89. NSDictionary * userInfo = notification.request.content.userInfo;
  90. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  91. NSLog(@"APNS notify: %@", userInfo);;
  92. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  93. NSString *messageId = [userInfo objectForKey:@"_id_"];
  94. [MiPushSDK openAppNotify:messageId];
  95. }
  96. [self cancelIconBadgeNumber];
  97. }
  98. - (void)cancelIconBadgeNumber {
  99. // 注册显示应用程序BadgeNumber的通知
  100. UIApplication *application = [UIApplication sharedApplication];
  101. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
  102. [application registerUserNotificationSettings:settings];
  103. if (application.applicationIconBadgeNumber > 0) {
  104. application.applicationIconBadgeNumber = 0;
  105. }
  106. }
  107. // 点击通知进入应用
  108. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) {
  109. NSDictionary * userInfo = response.notification.request.content.userInfo;
  110. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  111. NSLog(@"APNS notify: %@", userInfo);
  112. [MiPushSDK handleReceiveRemoteNotification:userInfo];
  113. NSString *messageId = [userInfo objectForKey:@"_id_"];
  114. [MiPushSDK openAppNotify:messageId];
  115. }
  116. [self jumpViewController:userInfo];
  117. completionHandler();
  118. }
  119. - (void)jumpViewController:(NSDictionary *)remoteNotification {
  120. [self cancelIconBadgeNumber];
  121. NSInteger pushType = [remoteNotification[@"pushType"] integerValue];
  122. switch (pushType) {
  123. case 1005://跳转到消息列表
  124. [self pushMessage];
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. #pragma mark---消息列表
  131. -(void)pushMessage
  132. {
  133. KDPNoticeViewController *message=[[KDPNoticeViewController alloc]init];
  134. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[[UINavigationController alloc]initWithRootViewController:message] animated:YES completion:nil];
  135. }
  136. - (void)applicationWillResignActive:(UIApplication *)application {
  137. // 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.
  138. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  139. }
  140. - (void)applicationDidEnterBackground:(UIApplication *)application {
  141. // 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.
  142. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  143. }
  144. - (void)applicationWillEnterForeground:(UIApplication *)application {
  145. // 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.
  146. }
  147. - (NSDictionary *)getrequestDIc
  148. {
  149. NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
  150. // retrieve the current interfaces - returns 0 on success
  151. struct ifaddrs *interfaces;
  152. if(!getifaddrs(&interfaces)) {
  153. // Loop through linked list of interfaces
  154. struct ifaddrs *interface;
  155. for(interface=interfaces; interface; interface=interface->ifa_next) {
  156. if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
  157. continue; // deeply nested code harder to read
  158. }
  159. const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
  160. char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
  161. if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
  162. NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
  163. NSString *type;
  164. if(addr->sin_family == AF_INET) {
  165. if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
  166. type = @"ipv4";
  167. }
  168. } else {
  169. const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
  170. if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
  171. type = @"ipv6";
  172. }
  173. }
  174. if(type) {
  175. NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
  176. addresses[key] = [NSString stringWithUTF8String:addrBuf];
  177. }
  178. }
  179. }
  180. // Free memory
  181. freeifaddrs(interfaces);
  182. }
  183. return [addresses count] ? addresses : nil;
  184. }
  185. #pragma mark----统计
  186. -(void)IPADDWithKD
  187. {
  188. //获取idfa
  189. NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  190. //获取手机系统
  191. NSString *ip=[self getIPAddressIPv4];
  192. NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
  193. //时间
  194. NSString *times=[self todayNOWTime];
  195. NSDictionary *dics=@{@"idfa":idfa,@"phonetype":[NSString stringWithFormat:@"ios %@",phoneVersion],@"time":times,@"phoneip":ip};
  196. [KDPNetworkRequestHTTP postURL:@"https://quan.726p.com/api/v2/channel/storeAppVerifyInfo" params:dics success:^(id _Nonnull json) {
  197. NSLog(@"%@",json);
  198. } failure:^(NSError * _Nonnull error) {
  199. }];
  200. }
  201. - (void)applicationDidBecomeActive:(UIApplication *)application {
  202. // 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.
  203. }
  204. - (void)applicationWillTerminate:(UIApplication *)application {
  205. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  206. }
  207. -(NSString*)todayNOWTime{
  208. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  209. // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  210. [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
  211. //现在时间,你可以输出来看下是什么格式
  212. NSDate *datenow = [NSDate date];
  213. //----------将nsdate按formatter格式转成nsstring
  214. NSString *currentTimeString = [formatter stringFromDate:datenow];
  215. NSLog(@"currentTimeString = %@",currentTimeString);
  216. return currentTimeString;
  217. }
  218. /** 获取本地ip地址
  219. * WIFI和WWAN同时存在默认返回WIFI
  220. */
  221. - (NSString *)getIPAddressIPv4
  222. {
  223. NSDictionary *addresses = [self getrequestDIc];
  224. NSLog(@"addresses: %@", addresses);
  225. if (addresses) {
  226. NSString *result = addresses[@"en0/ipv4"]?:(addresses[@"pdp_ip0/ipv4"]?:@"");
  227. return result;
  228. }else{
  229. return @"";
  230. }
  231. }
  232. @end