AppDelegate.m 8.32 KB
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"
#import "xn_base_Client_AF.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <RCTSplashScreen/RCTSplashScreen.h>
#import "IQKeyboardManager.h"
#import <UMCommon/UMCommon.h>           // 公共组件是所有友盟产品的基础组件,必选
#import <UMAnalytics/MobClick.h>
#import <UMPush/UMessage.h>
//#import <PgySDK/PgyManager.h>
//#import <PgyUpdate/PgyUpdateManager.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#include <arpa/inet.h>

@implementation AppDelegate

#pragma mark 创建单例
/**
 *  @author zsq
 *
 *  @brief  创建单例
 *
 *  @return self
 */
+ (instancetype)sharedInstance {
  static id sharedInstance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedInstance = [[self alloc] init];
  });
  return sharedInstance;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//  NSURL *jsCodeLocation;
//
//  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
  
  if(!launchOptions)
  {
    [AppDelegate sharedInstance].isJump = NO;

  }else{
    [AppDelegate sharedInstance].isJump = YES;
  }

  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
#ifdef DEBUG

//  jsCodeLocation = [NSURL URLWithString:@"http://192.168.81.6:8081/index.ios.bundle?platform=ios&dev=true"];
  //
//    jsCodeLocation =  [NSURL URLWithString:@"http://192.168.81.184:8081/index.ios.bundle?platform=ios&dev=true"];
//    jsCodeLocation =  [NSURL URLWithString:@"http://192.168.1.105:8081/index.ios.bundle?platform=ios&dev=true"];
//  jsCodeLocation =  [NSURL URLWithString:@"http://192.168.80.115:8081/index.ios.bundle?platform=ios&dev=true"];
  
//  172.20.10.2
//  zsq手机热点
  jsCodeLocation =  [NSURL URLWithString:@"http://172.20.10.2:8081/index.ios.bundle?platform=ios&dev=true"];

#else
  //release 模式
  jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  
  
#endif
  [UMConfigure initWithAppkey:@"5af105bfb27b0a5c8a00019e" channel:@"App Store"];
  [MobClick setScenarioType:E_UM_NORMAL];
  // Push组件基本功能配置
  UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
  //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
  entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionSound|UMessageAuthorizationOptionAlert;
  [UNUserNotificationCenter currentNotificationCenter].delegate=self;
  [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity     completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
    }else{
    }
  }];
  
  //启动基本SDK
//  [[PgyManager sharedPgyManager] startManagerWithAppId:@"a8071f84ef516389d414860d8a62c387"];
//  //启动更新检查SDK
//  [[PgyUpdateManager sharedPgyManager] startManagerWithAppId:@"a8071f84ef516389d414860d8a62c387"];

//  [[PgyUpdateManager sharedPgyManager] checkUpdate];

  [[xn_base_Client_AF sharedInstance] f_configByUrl:@"https://railway-dt.shmetro.com/router" appKey:@"0617CA8376F9901F28FF46B69BF9CF44" appSecret:@"28570C9D069ED51226DD9F028BD5E6DD" maxTask:15];

  //设置全局的键盘处理,不显示工具条
  [[IQKeyboardManager sharedManager] setEnable:YES];
  [[IQKeyboardManager sharedManager] setEnableAutoToolbar: NO];
  [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  [IQKeyboardManager sharedManager].shouldToolbarUsesTextFieldTintColor = YES;

  [[AMapServices sharedServices] setEnableHTTPS:YES];
#ifdef test
  [AMapServices sharedServices].apiKey =@"280cb15fd7417ef11542a0ea0e7e3395";
#else
  [AMapServices sharedServices].apiKey =@"326a03ead349468ccbe3f4710864fd02";
#endif

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"metroApp"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  [RCTSplashScreen show:rootView];
  return YES;
}

#pragma mark - app之间通讯的唤起操作
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
{
  
  if ([[NSString stringWithFormat:@"%@",url] containsString:@"shpadtcomcn"]) {
    NSString *str = [[NSString stringWithFormat:@"%@",url] substringFromIndex:[[NSString stringWithFormat:@"%@",url] rangeOfString:@"mobilePhone="].location+12];
    
    NSString *strend = [str substringToIndex:[str rangeOfString:@"from="].location];
    
    [AppDelegate sharedInstance].str_phone = strend;
  }

  return YES;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
  
  if ([[NSString stringWithFormat:@"%@",url] containsString:@"shpadtcomcn"]) {
    NSString *str = [[NSString stringWithFormat:@"%@",url] substringFromIndex:[[NSString stringWithFormat:@"%@",url] rangeOfString:@"mobilePhone="].location+12];
    
    NSString *strend = [str substringToIndex:[str rangeOfString:@"from="].location];
    
    [AppDelegate sharedInstance].str_phone = strend;
  }

  return YES;
}


- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  if (![deviceToken isKindOfClass:[NSData class]]) return;
  const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
  NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                        ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                        ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                        ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
  NSLog(@"deviceToken:%@",hexToken);
}

//iOS10以下使用这两个方法接收通知
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [UMessage setAutoAlert:NO];
  if([[[UIDevice currentDevice] systemVersion]intValue] < 10){
    [UMessage didReceiveRemoteNotification:userInfo];
  }
  completionHandler(UIBackgroundFetchResultNewData);
}

//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
  NSDictionary * userInfo = notification.request.content.userInfo;
  if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [UMessage setAutoAlert:NO];
    //应用处于前台时的远程推送接受
    //必须加这句代码
    [UMessage didReceiveRemoteNotification:userInfo];
  }else{
    //应用处于前台时的本地推送接受
  }
  completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}

//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    //应用处于后台时的远程推送接受
    //必须加这句代码
    [UMessage didReceiveRemoteNotification:userInfo];
  }else{
    //应用处于后台时的本地推送接受
  }
  
}


@end