// // AppDelegate.m // KuDianProject // // Created by 学丽 on 2019/7/4. // Copyright © 2019 KDP. All rights reserved. // #import "AppDelegate.h" #import "KDPTabBarVC.h" #import "KDPAccountTool.h" #import "KDPWelcomePageViewController.h" #import "KDPNoticeViewController.h" #import #include #include #include @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self setUpKeyWindow]; [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES; [self setUpUM]; [self setUpMiPush]; [self IPADDWithKD]; return YES; } - (void)setUpKeyWindow{ self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor=[UIColor whiteColor]; self.window.rootViewController=[[KDPTabBarVC alloc]init]; [self.window makeKeyAndVisible]; KDPAccountModel *accountModel = [KDPAccountTool account]; if (![KDPAccountTool isLogin] || [accountModel.has_kwai integerValue] != 1) { [self.window.rootViewController presentViewController:[[UINavigationController alloc]initWithRootViewController:[[KDPWelcomePageViewController alloc]init]] animated:YES completion:nil]; } } #pragma mark - 推送 - (void)setUpMiPush{ [MiPushSDK registerMiPush:self]; } #pragma mark--友盟统计 -(void)setUpUM { UMConfigInstance.appKey=UMENG_KEY; UMConfigInstance.channelId=@"App Store"; NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"]; [MobClick setAppVersion:appVersion]; [MobClick startWithConfigure:UMConfigInstance]; [MobClick setLogEnabled: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]; NSString *messageId = [userInfo objectForKey:@"_id_"]; [MiPushSDK openAppNotify:messageId]; } [self cancelIconBadgeNumber]; } - (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)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]; NSString *messageId = [userInfo objectForKey:@"_id_"]; [MiPushSDK openAppNotify:messageId]; } [self jumpViewController:userInfo]; completionHandler(); } - (void)jumpViewController:(NSDictionary *)remoteNotification { [self cancelIconBadgeNumber]; NSInteger pushType = [remoteNotification[@"pushType"] integerValue]; switch (pushType) { case 1005://跳转到消息列表 [self pushMessage]; break; default: break; } } #pragma mark---消息列表 -(void)pushMessage { KDPNoticeViewController *message=[[KDPNoticeViewController alloc]init]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[[UINavigationController alloc]initWithRootViewController:message] animated:YES completion:nil]; } - (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. } - (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. } - (NSDictionary *)getrequestDIc { NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8]; // retrieve the current interfaces - returns 0 on success struct ifaddrs *interfaces; if(!getifaddrs(&interfaces)) { // Loop through linked list of interfaces struct ifaddrs *interface; for(interface=interfaces; interface; interface=interface->ifa_next) { if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) { continue; // deeply nested code harder to read } const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr; char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ]; if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) { NSString *name = [NSString stringWithUTF8String:interface->ifa_name]; NSString *type; if(addr->sin_family == AF_INET) { if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) { type = @"ipv4"; } } else { const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr; if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) { type = @"ipv6"; } } if(type) { NSString *key = [NSString stringWithFormat:@"%@/%@", name, type]; addresses[key] = [NSString stringWithUTF8String:addrBuf]; } } } // Free memory freeifaddrs(interfaces); } return [addresses count] ? addresses : nil; } #pragma mark----统计 -(void)IPADDWithKD { //获取idfa NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; //获取手机系统 NSString *ip=[self getIPAddressIPv4]; NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; //时间 NSString *times=[self todayNOWTime]; NSDictionary *dics=@{@"idfa":idfa,@"phonetype":[NSString stringWithFormat:@"ios %@",phoneVersion],@"time":times,@"phoneip":ip}; [KDPNetworkRequestHTTP postURL:@"https://quan.726p.com/api/v2/channel/storeAppVerifyInfo" params:dics success:^(id _Nonnull json) { NSLog(@"%@",json); } failure:^(NSError * _Nonnull error) { }]; } - (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:. } -(NSString*)todayNOWTime{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //现在时间,你可以输出来看下是什么格式 NSDate *datenow = [NSDate date]; //----------将nsdate按formatter格式转成nsstring NSString *currentTimeString = [formatter stringFromDate:datenow]; NSLog(@"currentTimeString = %@",currentTimeString); return currentTimeString; } /** 获取本地ip地址 * WIFI和WWAN同时存在默认返回WIFI */ - (NSString *)getIPAddressIPv4 { NSDictionary *addresses = [self getrequestDIc]; NSLog(@"addresses: %@", addresses); if (addresses) { NSString *result = addresses[@"en0/ipv4"]?:(addresses[@"pdp_ip0/ipv4"]?:@""); return result; }else{ return @""; } } @end