2
0

app_callback.mm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // app_callback.mm
  3. // WFChatClient
  4. //
  5. // Created by heavyrain on 2017/11/5.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #include "app_callback.h"
  9. #import <UIKit/UIKit.h>
  10. #import "WFCCUtilities.h"
  11. #import "sys/utsname.h"
  12. #import "WFCCNetworkService.h"
  13. #import <CoreTelephony/CTCarrier.h>
  14. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  15. namespace mars {
  16. namespace app {
  17. AppCallBack* AppCallBack::instance_ = NULL;
  18. AppCallBack* AppCallBack::Instance() {
  19. if(instance_ == NULL) {
  20. instance_ = new AppCallBack();
  21. }
  22. return instance_;
  23. }
  24. void AppCallBack::Release() {
  25. delete instance_;
  26. instance_ = NULL;
  27. }
  28. AppCallBack::AppCallBack() {
  29. }
  30. void AppCallBack::SetAccountUserName(const std::string &userName) {
  31. info.username = userName;
  32. NSString *path = [WFCCUtilities getDocumentPathWithComponent:[NSString stringWithUTF8String:info.username.c_str()]];
  33. filePath = [path UTF8String];
  34. }
  35. bool AppCallBack::isDBAlreadyCreated(const std::string &clientId) {
  36. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  37. NSUserDomainMask, YES);
  38. NSString *documentDirectory = [paths objectAtIndex:0];
  39. NSFileManager *myFileManager = [NSFileManager defaultManager];
  40. NSString *cid = [NSString stringWithUTF8String:clientId.c_str()];
  41. BOOL isDir = NO;
  42. BOOL isExist = NO;
  43. for (NSString *path in [myFileManager contentsOfDirectoryAtPath:documentDirectory error:nil]) {
  44. NSString *dbPath = [[[documentDirectory stringByAppendingPathComponent:path] stringByAppendingPathComponent:cid] stringByAppendingPathComponent:@"data"];;
  45. isExist = [myFileManager fileExistsAtPath:dbPath isDirectory:&isDir];
  46. if(isExist && !isDir) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. void AppCallBack::SetAccountLogoned(bool isLogoned) {
  53. info.is_logoned = isLogoned;
  54. }
  55. // return your app path
  56. std::string AppCallBack::GetAppFilePath(){
  57. return filePath;
  58. }
  59. AccountInfo AppCallBack::GetAccountInfo() {
  60. return info;
  61. }
  62. unsigned int AppCallBack::GetClientVersion() {
  63. return 0;
  64. }
  65. static BOOL PSPDFIsDevelopmentBuild() {
  66. #if TARGET_IPHONE_SIMULATOR
  67. return YES;
  68. #else
  69. static BOOL isDevelopment = NO;
  70. static dispatch_once_t onceToken;
  71. dispatch_once(&onceToken, ^{
  72. // There is no provisioning profile in AppStore Apps.
  73. NSData *data = [NSData dataWithContentsOfFile:[NSBundle.mainBundle pathForResource:@"embedded" ofType:@"mobileprovision"]];
  74. if (data) {
  75. const char *bytes = (const char *)[data bytes];
  76. NSMutableString *profile = [[NSMutableString alloc] initWithCapacity:data.length];
  77. for (NSUInteger i = 0; i < data.length; i++) {
  78. [profile appendFormat:@"%c", bytes[i]];
  79. }
  80. // Look for debug value, if detected we're a development build.
  81. NSString *cleared = [[profile componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] componentsJoinedByString:@""];
  82. isDevelopment = [cleared rangeOfString:@"<key>get-task-allow</key><true/>"].length > 0;
  83. }
  84. });
  85. return isDevelopment;
  86. #endif
  87. }
  88. int AppCallBack::GetPushType() {
  89. return PSPDFIsDevelopmentBuild() ? 1 : 0;
  90. }
  91. DeviceInfo AppCallBack::GetDeviceInfo() {
  92. DeviceInfo info;
  93. struct utsname systemInfo;
  94. uname(&systemInfo);
  95. NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
  96. info.clientid = [[[WFCCNetworkService sharedInstance] getClientId] UTF8String];
  97. info.platform = PlatformType_iOS;
  98. //如果使用pad端类型,这里平台改成pad类型,另外在获取token时,也需要把平台改成pad类型,请到AppService.m文件中搜索"iPad"
  99. //if(当前设备是iPad)
  100. //platform = Platform_iPad
  101. info.packagename = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"] UTF8String];
  102. info.pushtype = mars::app::AppCallBack::Instance()->GetPushType();
  103. info.device = [deviceString UTF8String];
  104. info.deviceversion = [[UIDevice currentDevice].systemVersion UTF8String];
  105. info.phonename = [[UIDevice currentDevice].name UTF8String];
  106. NSArray *languages = [NSLocale preferredLanguages];
  107. NSString *currentLanguage = [languages objectAtIndex:0];
  108. info.language = [currentLanguage UTF8String];
  109. CTTelephonyNetworkInfo *nwinfo = [[CTTelephonyNetworkInfo alloc] init];
  110. CTCarrier *carrier = nwinfo.subscriberCellularProvider;
  111. info.carriername = carrier.carrierName ? [carrier.carrierName UTF8String] : "";
  112. NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  113. if (appVersion) {
  114. info.appversion = [appVersion UTF8String];
  115. }
  116. info.sdkversion = [SDKVERSION UTF8String];
  117. return info;
  118. }
  119. }}