AppDelegate.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // AppDelegate.m
  3. // smartRhino
  4. //
  5. // Created by tederen on 2019/10/21.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "TabBarController.h"
  10. #import "WorkFlowTabbarController.h"
  11. #import "WXApi.h"
  12. #import "HKFloatManager.h"
  13. @interface AppDelegate ()<JPUSHRegisterDelegate,WXApiDelegate>
  14. @property (nonatomic, strong) TabBarController *tabBar;
  15. @end
  16. @implementation AppDelegate
  17. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  18. // Override point for customization after application launch.
  19. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  20. self.tabBar = [TabBarController sharedTabBarController];
  21. [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  22. [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
  23. ///友盟分享
  24. [self configUSharePlatforms];
  25. //初始化环信
  26. [[EaseSDKHelper shareHelper] hyphenateApplication:application appkey:kHuanXinAppKey apnsCertName:kHuanXinApnsName];
  27. // [WXApi registerApp:wxAppKey enableMTA:NO];//不崩溃
  28. [WXApi registerApp:wxAppKey universalLink:@"https://apk.tederen.com"];
  29. [self settingPush: launchOptions];
  30. if (![UserManager checkStringNull:kToken]){
  31. [[EMClient sharedClient] logout:YES completion:^(EMError *aError) {
  32. NSData * data = USERDEFAULTSGET(@"USERINFO");
  33. AppUserModel * model = (AppUserModel*) [NSKeyedUnarchiver unarchiveObjectWithData:data];
  34. [[EaseSDKHelper shareHelper] loginEaseWithUsername:model.ImId password:@"123456"];
  35. }];
  36. self.window.rootViewController = self.tabBar;
  37. }else{
  38. self.window.rootViewController = [StoryboardManager shared].login.instantiateInitialViewController;
  39. }
  40. [self.window makeKeyAndVisible];
  41. [HKFloatManager addFloatVcs:@[@"HKSecondViewController"]];
  42. return YES;
  43. }
  44. - (void)configUSharePlatforms
  45. {
  46. [UMConfigure initWithAppkey:UMengAppKey channel:nil];
  47. /* 设置微信的appKey和appSecret */
  48. [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:wxAppKey appSecret:wxAppSecret redirectURL:@"http://mobile.umeng.com/social"];
  49. [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatTimeLine appKey:wxAppKey appSecret:wxAppSecret redirectURL:@"http://mobile.umeng.com/social"];
  50. /* 设置分享到QQ互联的appID
  51. * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
  52. */
  53. [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAppKey/*设置QQ平台的appID*/ appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
  54. /* 设置新浪的appKey和appSecret */
  55. [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:WBAppKey appSecret:WBAppSecret redirectURL:@"http://mobile.umeng.com/social"];
  56. }
  57. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  58. {
  59. NSString * str = [url absoluteString];
  60. if ([str hasPrefix:@"iossmartrhino"]) {
  61. if (![UserManager checkStringNull:kToken]){
  62. str = [str substringFromIndex:20];
  63. NSArray *array = [str componentsSeparatedByString:@"&"];
  64. NSDictionary * dict= @{@"Id":array[0],@"type":array[1],@"userId":array[2],@"name":array[3],@"describe":array[4]};
  65. [[NSNotificationCenter defaultCenter] postNotificationName:SHARERETURNSUCCESS object:nil userInfo:dict];
  66. UIWindow *window = [UtilsTools getWindow];
  67. window.rootViewController = [TabBarController sharedTabBarController];
  68. [[TabBarController sharedTabBarController] setSelectedIndex:0];
  69. [window makeKeyAndVisible];
  70. }else{
  71. UIWindow *window = [UtilsTools getWindow];
  72. window.rootViewController = [StoryboardManager shared].login.instantiateInitialViewController;
  73. [window makeKeyAndVisible];
  74. }
  75. }
  76. return [WXApi handleOpenURL:url delegate:self];
  77. }
  78. /// 分享回调
  79. - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
  80. {
  81. if ([url.host isEqualToString:@"oauth"]){//微信登录
  82. return [WXApi handleOpenURL:url delegate:self];
  83. }
  84. //6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
  85. NSString * str = [url absoluteString];
  86. if ([str hasPrefix:@"iossmartrhino"]) {
  87. if (![UserManager checkStringNull:kToken]){
  88. str = [str substringFromIndex:20];
  89. NSArray *array = [str componentsSeparatedByString:@"&"];
  90. NSDictionary * dict= @{@"Id":array[0],@"type":array[1],@"userId":array[2],@"name":array[3],@"describe":array[4]};
  91. [[NSNotificationCenter defaultCenter] postNotificationName:SHARERETURNSUCCESS object:nil userInfo:dict];
  92. UIWindow *window = [UtilsTools getWindow];
  93. window.rootViewController = [TabBarController sharedTabBarController];
  94. [[TabBarController sharedTabBarController] setSelectedIndex:0];
  95. [window makeKeyAndVisible];
  96. }else{
  97. UIWindow *window = [UtilsTools getWindow];
  98. window.rootViewController = [StoryboardManager shared].login.instantiateInitialViewController;
  99. [window makeKeyAndVisible];
  100. }
  101. }
  102. BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url options:options];
  103. if (!result) {
  104. }
  105. return result;
  106. }
  107. + (UINavigationController *)getRootController {
  108. return (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
  109. }
  110. + (UIViewController *)getTopView {
  111. UINavigationController *rootNavigation = [self getRootController];
  112. return rootNavigation.topViewController;
  113. }
  114. + (instancetype)appdelegate {
  115. return [UIApplication sharedApplication].delegate;
  116. }
  117. - (void)applicationWillResignActive:(UIApplication *)application {
  118. NSLog(@"applicationWillResignActive");
  119. // [[NSNotificationCenter defaultCenter] postNotificationName:NOTEBOOKSAVE object:nil];
  120. }
  121. - (void)applicationDidEnterBackground:(UIApplication *)application {
  122. NSLog(@"applicationDidEnterBackground");
  123. }
  124. - (void)applicationWillEnterForeground:(UIApplication *)application {
  125. // 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.
  126. }
  127. - (void)applicationDidBecomeActive:(UIApplication *)application {
  128. NSLog(@"applicationDidBecomeActive");
  129. }
  130. - (void)applicationWillTerminate:(UIApplication *)application {
  131. NSLog(@"applicationWillTerminate");
  132. }
  133. #pragma mark - 极光
  134. - (void)settingPush:(NSDictionary *)launchOptions {
  135. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  136. entity.types = JPAuthorizationOptionAlert | JPAuthorizationOptionBadge | JPAuthorizationOptionSound;
  137. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  138. [JPUSHService setupWithOption:launchOptions appKey:kJGAppKey
  139. channel:@"tederen"
  140. apsForProduction:YES // 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES.
  141. advertisingIdentifier:nil];
  142. [JpushTool setAlias:@"18224073257" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
  143. } seq:18224073257];
  144. }
  145. // 此方法是必须要有实现,否则SDK将无法处理应用注册的Token,推送也就不会成功
  146. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  147. NSLog(@"%@",deviceToken);
  148. [JPUSHService registerDeviceToken:deviceToken];
  149. // [[EaseSDKHelper shareHelper] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  150. }
  151. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  152. }
  153. #pragma mark - JPush
  154. // 前台界面收到通知
  155. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  156. // Required
  157. NSDictionary * userInfo = notification.request.content.userInfo;
  158. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  159. [JPUSHService handleRemoteNotification:userInfo];
  160. }
  161. completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
  162. }
  163. // 点击通知消息从后台进入前台 willPresentNotification 接收之后会走这个方法
  164. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  165. // Required
  166. NSDictionary * userInfo = response.notification.request.content.userInfo;
  167. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  168. [JPUSHService handleRemoteNotification:userInfo];
  169. NSString * message = userInfo[@"aps"][@"alert"];
  170. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:(UIAlertControllerStyleAlert)];
  171. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
  172. [alert addAction:sureAction];
  173. [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  174. [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
  175. }
  176. completionHandler(); // 系统要求执行这个方法
  177. }
  178. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  179. // Required, iOS 7 Support
  180. [JPUSHService handleRemoteNotification:userInfo];
  181. NSString * message = userInfo[@"aps"][@"alert"];
  182. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:(UIAlertControllerStyleAlert)];
  183. UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
  184. [alert addAction:sureAction];
  185. [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  186. [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
  187. completionHandler(UIBackgroundFetchResultNewData);
  188. }
  189. //#pragma mark 关闭第三方键盘
  190. //- (BOOL)application:(UIApplication*)application shouldAllowExtensionPointIdentifier:(NSString*)extensionPointIdentifier {
  191. // if([extensionPointIdentifier isEqualToString:UIApplicationKeyboardExtensionPointIdentifier]) {
  192. // return NO;
  193. // }
  194. // return YES;
  195. //}
  196. #pragma mark - 微信登录
  197. - (void)onResp:(BaseResp *)resp{
  198. // =============== 获得的微信登录授权回调 ============
  199. if ([resp isMemberOfClass:[SendAuthResp class]]) {
  200. NSLog(@"******************获得的微信登录授权******************");
  201. SendAuthResp *aresp = (SendAuthResp *)resp;
  202. if (aresp.errCode != 0 ) {
  203. dispatch_async(dispatch_get_main_queue(), ^{
  204. dispatch_async(dispatch_get_main_queue(), ^{
  205. SHOWERROR(@"微信授权失败");
  206. });
  207. });
  208. return;
  209. }
  210. //授权成功获取 OpenId
  211. NSString *code = aresp.code;
  212. [self getWeiXinOpenId:code];
  213. }
  214. }
  215. - (void)getWeiXinOpenId:(NSString *)code{
  216. /*
  217. appid 是 应用唯一标识,在微信开放平台提交应用审核通过后获得
  218. secret 是 应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
  219. code 是 填写第一步获取的code参数
  220. grant_type 是 填authorization_code
  221. */
  222. NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",wxAppKey,wxAppSecret,code];
  223. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  224. NSURL *zoneUrl = [NSURL URLWithString:url];
  225. NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
  226. NSData *data1 = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
  227. if (!data1) {
  228. dispatch_async(dispatch_get_main_queue(), ^{
  229. SHOWERROR(@"微信授权失败");
  230. });
  231. return ;
  232. }
  233. // 授权成功,获取token、openID字典
  234. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];
  235. NSLog(@"token、openID字典===%@",dic);
  236. // NSString *access_token = dic[@"access_token"];
  237. // NSString *openid= dic[@"openid"];
  238. if ([dic[@"openid"] length] > 0) {
  239. [[NSNotificationCenter defaultCenter] postNotificationName:WeiChatSuccess object:nil userInfo:dic];
  240. }else{
  241. dispatch_async(dispatch_get_main_queue(), ^{
  242. SHOWERROR(@"微信授权失败");
  243. });
  244. return ;
  245. }
  246. });
  247. }
  248. - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
  249. if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
  250. NSURL *webpageURL = userActivity.webpageURL;
  251. NSString * str = [webpageURL absoluteString];
  252. if ([str hasPrefix:@"iossmartrhino"]) {
  253. if (![UserManager checkStringNull:kToken]){
  254. str = [str substringFromIndex:20];
  255. NSArray *array = [str componentsSeparatedByString:@"&"];
  256. NSDictionary * dict= @{@"Id":array[0],@"type":array[1],@"userId":array[2],@"name":array[3],@"describe":array[4]};
  257. [[NSNotificationCenter defaultCenter] postNotificationName:SHARERETURNSUCCESS object:nil userInfo:dict];
  258. UIWindow *window = [UtilsTools getWindow];
  259. window.rootViewController = [TabBarController sharedTabBarController];
  260. [[TabBarController sharedTabBarController] setSelectedIndex:0];
  261. [window makeKeyAndVisible];
  262. }else{
  263. UIWindow *window = [UtilsTools getWindow];
  264. window.rootViewController = [StoryboardManager shared].login.instantiateInitialViewController;
  265. [window makeKeyAndVisible];
  266. }
  267. }
  268. // TODO 根据需求进行处理
  269. }
  270. // TODO 根据需求进行处理
  271. return YES;
  272. }
  273. @end