NSString+Category.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // NSString+Category.m
  3. // MobileVideo
  4. //
  5. // Created by XM on 2018/4/23.
  6. // Copyright © 2018年 XM. All rights reserved.
  7. //
  8. #import "NSString+Category.h"
  9. #import <SystemConfiguration/CaptiveNetwork.h>
  10. #include <arpa/inet.h>
  11. #include <netdb.h>
  12. #include <net/if.h>
  13. #include <ifaddrs.h>
  14. #import <dlfcn.h>
  15. #import <SystemConfiguration/SystemConfiguration.h>
  16. #import <SystemConfiguration/CaptiveNetwork.h>
  17. #import "Header.h"
  18. @implementation NSString (Category)
  19. #pragma mark 国际化语言翻译
  20. + (NSString *)ToNSStr:(const char*)szStr {
  21. if (szStr == NULL) {
  22. NSLog(@"Error szStr is null!");
  23. return @"";
  24. }
  25. NSString *retStr = [NSString stringWithUTF8String:szStr];
  26. if (retStr == nil || (retStr.length == 0 && strlen(szStr) > 0)) {
  27. NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
  28. NSData *data = [NSData dataWithBytes:szStr length:strlen(szStr)];
  29. retStr = [[NSString alloc] initWithData:data encoding:enc];
  30. }
  31. if (retStr == nil) {
  32. retStr = @"";
  33. }
  34. return retStr;
  35. }
  36. #pragma mark 是否包含字符串
  37. - (BOOL)isContainsString:(NSString *)sFind {
  38. if (sFind == nil) {
  39. return FALSE;
  40. }
  41. NSRange range = [self rangeOfString:sFind];
  42. return range.length != 0;
  43. }
  44. #pragma mark 获取字符串长度
  45. + (int)countLengthWithString:(NSString *)str {
  46. int strlength = 0;
  47. char* p = (char*)[str cStringUsingEncoding:NSUnicodeStringEncoding];
  48. for (int i=0 ; i<[str lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
  49. if (*p) {
  50. p++;
  51. strlength++;
  52. }
  53. else {
  54. p++;
  55. }
  56. }
  57. return (strlength+1)/2;
  58. }
  59. #pragma mark 获得当前网络连接的SSID、IP、Wi-Fi名字
  60. + (NSString *)getCurrent_SSID {
  61. NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
  62. id infoDic = nil;
  63. for (NSString *ifnam in ifs) {
  64. infoDic = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
  65. }
  66. const char *charSSID = [[infoDic objectForKey:@"SSID"] UTF8String];
  67. if (TARGET_IPHONE_SIMULATOR) {
  68. charSSID = "kuozhanbu";
  69. }
  70. if (charSSID == NULL) {
  71. return @"";
  72. }
  73. NSString *ssid = [NSString stringWithUTF8String:charSSID];
  74. return ssid;
  75. }
  76. + (NSString *)getCurrent_IP_Address {
  77. NSString *address = @"error";
  78. struct ifaddrs *interfaces = NULL;
  79. struct ifaddrs *temp_addr = NULL;
  80. int success = 0;
  81. // retrieve the current interfaces - returns 0 on success
  82. success = getifaddrs(&interfaces);
  83. if (success == 0) {
  84. // Loop through linked list of interfaces
  85. temp_addr = interfaces;
  86. while(temp_addr != NULL) {
  87. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  88. // Check if interface is en0 which is the wifi connection on the iPhone
  89. if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
  90. // Get NSString from C String
  91. address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
  92. }
  93. }
  94. temp_addr = temp_addr->ifa_next;
  95. }
  96. }
  97. freeifaddrs(interfaces);
  98. return address;
  99. }
  100. + (NSString *)getWifiName {
  101. NSString *wifiName = nil;
  102. CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
  103. if (!wifiInterfaces) {
  104. return nil;
  105. }
  106. NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
  107. for (NSString *interfaceName in interfaces) {
  108. CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
  109. if (dictRef) {
  110. NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
  111. wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
  112. CFRelease(dictRef);
  113. }
  114. }
  115. CFRelease(wifiInterfaces);
  116. return wifiName;
  117. }
  118. #pragma mark 判断是否是直连设备
  119. + (BOOL)checkSSID:(NSString*)ssid {
  120. if ([ssid hasPrefix:@"robot_"] || [ssid hasPrefix:@"card"]|| [ssid hasPrefix:@"car_"]
  121. || [ssid hasPrefix:@"seye_"] ||[ssid hasPrefix:@"NVR"]|| [ssid hasPrefix:@"DVR"]
  122. || [ssid hasPrefix:@"beye_"] ||[ssid hasPrefix:@"IPC"]|| [ssid hasPrefix:@"Car_"] || [ssid hasPrefix:@"BOB_"] || [ssid hasPrefix:@"xmjp_"] || [ssid hasPrefix:@"UTEC"] || [ssid hasPrefix:@"camera_"])
  123. {
  124. return YES;
  125. }else{
  126. return NO;
  127. }
  128. }
  129. #pragma mark 字符串转NSData
  130. + (NSData *)AutoCopyUTF8Str:(NSString*)string {
  131. if ([NSString IsChinese:string]) {
  132. return [NSString UTF8StrToGBKData:string];
  133. }else{
  134. return [string dataUsingEncoding:NSUTF8StringEncoding];
  135. }
  136. }
  137. //汉语字符串转NSData
  138. + (NSData *)UTF8StrToGBKData:(NSString *)strUTF8 {
  139. NSStringEncoding encoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
  140. NSData* gb2312data = [strUTF8 dataUsingEncoding:encoding];
  141. return gb2312data;
  142. }
  143. + (BOOL)IsChinese:(NSString *)str {
  144. for(int i=0; i< [str length];i++){
  145. int a = [str characterAtIndex:i];
  146. if( a > 0x4e00 && a < 0x9fff)
  147. {
  148. return YES;
  149. }
  150. }
  151. return NO;
  152. }
  153. + (NSString *)GetSystemTimeString {
  154. NSDate *nowDate = [NSDate date];
  155. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  156. [dateFormatter setDateFormat:TimeFormatter];
  157. NSString *dateString = [dateFormatter stringFromDate:nowDate];
  158. return dateString;
  159. }
  160. #pragma mark - 保存鱼眼模式
  161. + (void)saveFisheye:(NSString *)devId mode:(int)fisheyeMode {
  162. NSString *path = [NSString fisheyeInfoFile];
  163. NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
  164. if (!plist) {
  165. plist = [[NSMutableDictionary alloc] initWithCapacity:1];
  166. }
  167. NSMutableDictionary *fisheyeDic = plist[devId];//取出该设备的信息
  168. if (!fisheyeDic) {
  169. fisheyeDic = [NSMutableDictionary dictionaryWithCapacity:3];
  170. plist[devId] = fisheyeDic;
  171. }
  172. fisheyeDic[KFisheyeMode] = [NSString stringWithFormat:@"%d", fisheyeMode];
  173. [plist writeToFile:path atomically:YES];
  174. }
  175. #pragma mark - 读取鱼眼的模式
  176. + (int)fisheyeMode:(NSString *)devId {
  177. if ([NSString checkSSID:[NSString getCurrent_SSID]]) {
  178. return -1;
  179. }
  180. NSString *path = [NSString fisheyeInfoFile];
  181. NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
  182. NSMutableDictionary *fisheyeDic = plist[devId];
  183. if (!fisheyeDic) {
  184. return -1;
  185. }
  186. NSString* sFisheyeMode = [fisheyeDic valueForKey:KFisheyeMode];
  187. if (sFisheyeMode == nil) {
  188. return -1;
  189. } else {
  190. return [sFisheyeMode intValue];
  191. }
  192. }
  193. #pragma mark - 取出当前设备是否支持矫正
  194. +(NSString*)getCorrectdev:(NSString*)devId
  195. {
  196. NSString *path = [NSString correctInfoFile];
  197. NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
  198. if (!plist) {
  199. plist = [[NSMutableDictionary alloc] initWithCapacity:0];
  200. }
  201. return [plist objectForKey:devId];
  202. }
  203. #pragma mark 获取设备网络状态
  204. + (NSString *)getDeviceNetType:(int)type {
  205. switch (type) {
  206. case 0:
  207. return TS("P2P_Mode");
  208. break;
  209. case 1:
  210. return TS("Transmit_Mode");
  211. break;
  212. case 2:
  213. return TS("IP_Mode");
  214. break;
  215. case 5:
  216. return TS("RPS_Mode");
  217. break;
  218. case 6:
  219. return TS("RTS_P2P");
  220. break;
  221. case 7:
  222. return TS("RTS_Proxy");
  223. break;
  224. case 8:
  225. return TS("P2P_V2");
  226. break;
  227. case 9:
  228. return TS("Proxy_V2");
  229. break;
  230. default:
  231. return TS("no_alarm_type");
  232. break;
  233. }
  234. }
  235. #pragma mark 获取设备类型对应的设备图片字符串
  236. + (NSString *)getDeviceImageType:(int)type {
  237. NSString *imgString = @"xmjp_seye.png";
  238. if (type == XM_DEV_DEV) {
  239. imgString = @"xmjp_camera.png";
  240. }else if (type == XM_DEV_SOCKET) {
  241. imgString = @"xmjp_socket.png";
  242. }else if (type == XM_DEV_BULB) {
  243. imgString = @"xmjp_bulb.png";
  244. }else if (type == XM_DEV_BULB_SOCKET) {
  245. imgString = @"xmjp_bulbsocket.png";
  246. }else if (type == XM_DEV_CAR) {
  247. imgString = @"xmjp_car.png";
  248. }else if (type == XM_DEV_BEYE) {
  249. imgString = @"xmjp_beye.png";
  250. }else if (type == XM_DEV_SEYE) {
  251. imgString = @"xmjp_seye.png";
  252. }else if (type == XM_DEV_ROBOT) {
  253. imgString = @"xmjp_rotot.png";
  254. }else if (type == XM_DEV_SPORT_CAMERA) {
  255. imgString = @"xmjp_mov.png";
  256. }else if (type == XM_DEV_FEYE) {
  257. imgString = @"xmjp_feye.png";
  258. }else if (type == XM_DEV_FISH_BULB) {
  259. imgString = @"xmjp_fbulb.png";
  260. }else if (type == XM_DEV_BOB) {
  261. imgString = @"xmjp_bob.png";
  262. }else if (type == XM_DEV_MUSIC_BOX) {
  263. imgString = @"xmjp_cloudbox_klok.png";
  264. }else if (type == XM_DEV_INTELLIGENT_CENTER) {
  265. imgString = @"智联中心";
  266. }else if (type == XM_DEV_STRIP) {
  267. imgString = @"插排";
  268. }else if (type == XM_DEV_DOORLOCK) {
  269. imgString = @"门磁";
  270. }else if (type == XM_DEV_CENTER_COPY) {
  271. imgString = @"智能中心";
  272. }else if (type == XM_DEV_UFO) {
  273. imgString = @"飞碟";
  274. }else if (type == XM_DEV_DOORBELL) {
  275. imgString = @"智能门铃";
  276. }else if (type == XM_DEV_BULLET) {
  277. imgString = @"雄迈枪机";
  278. }else if (type == XM_DEV_GUNLOCK_510) {
  279. imgString = @"雄迈枪机";
  280. }else if (type == XM_DEV_DRUM) {
  281. imgString = @"架子鼓";
  282. }else if (type == XM_DEV_FEEDER) {
  283. imgString = @"喂食器";
  284. }else if (type == XM_DEV_CAT) {
  285. imgString = @"猫眼";
  286. }else if (type == XM_DEV_NSEYE) {
  287. imgString = @"xmjp_seye.png";
  288. }else if (type == XM_DEV_INTELLIGENT_LOCK) {
  289. imgString = @"门铃锁";
  290. }else if (type == CZ_DOORBELL) {
  291. imgString = @"创泽门铃";
  292. }else{
  293. imgString = @"xmjp_seye.png";
  294. }
  295. return imgString;
  296. }
  297. #pragma mark 获取设备类型字符串
  298. + (NSString *)getDeviceType:(int)type {
  299. NSString *mDefaultName = TS("Device");
  300. if (type == XM_DEV_DEV) {
  301. mDefaultName = TS("传统监控设备");
  302. }else if (type == XM_DEV_SOCKET) {
  303. mDefaultName = TS("智能插座");
  304. }else if (type == XM_DEV_BULB) {
  305. mDefaultName = TS("情景灯泡");
  306. }else if (type == XM_DEV_BULB_SOCKET) {
  307. mDefaultName = TS("智能灯座");
  308. }else if (type == XM_DEV_CAR) {
  309. mDefaultName = TS("汽车伴侣");
  310. }else if (type == XM_DEV_BEYE) {
  311. mDefaultName = TS("大眼睛");
  312. }else if (type == XM_DEV_SEYE) {
  313. mDefaultName = TS("小眼睛");
  314. }else if (type == XM_DEV_ROBOT) {
  315. mDefaultName = TS("雄迈摇头机");
  316. }else if (type == XM_DEV_SPORT_CAMERA) {
  317. mDefaultName = TS("运动摄像机");
  318. }else if (type == XM_DEV_FEYE) {
  319. mDefaultName = TS("鱼眼小雨点");
  320. }else if (type == XM_DEV_FISH_BULB) {
  321. mDefaultName = TS("鱼眼灯泡");
  322. }else if (type == XM_DEV_BOB) {
  323. mDefaultName = TS("小黄人");
  324. }else if (type == XM_DEV_MUSIC_BOX) {
  325. mDefaultName = TS("Wi-Fi音乐盒");
  326. }else if (type == XM_DEV_INTELLIGENT_CENTER) {
  327. mDefaultName = TS("智联中心");
  328. }else if (type == XM_DEV_STRIP) {
  329. mDefaultName = TS("插排");
  330. }else if (type == XM_DEV_DOORLOCK) {
  331. mDefaultName = TS("门磁");
  332. }else if (type == XM_DEV_CENTER_COPY) {
  333. mDefaultName = TS("智能中心");
  334. }else if (type == XM_DEV_UFO) {
  335. mDefaultName = TS("飞碟");
  336. }else if (type == XM_DEV_DOORBELL) {
  337. mDefaultName = TS("智能门铃");
  338. }else if (type == XM_DEV_BULLET) {
  339. mDefaultName = TS("雄迈枪机");
  340. }else if (type == XM_DEV_GUNLOCK_510) {
  341. mDefaultName = TS("雄迈枪机");
  342. }else if (type == XM_DEV_DRUM) {
  343. mDefaultName = TS("架子鼓");
  344. }else if (type == XM_DEV_FEEDER) {
  345. mDefaultName = TS("喂食器");
  346. }else if (type == XM_DEV_CAT) {
  347. mDefaultName = TS("猫眼");
  348. }else if (type == XM_DEV_NSEYE) {
  349. mDefaultName = TS("直播小雨点");
  350. }else if (type == XM_DEV_INTELLIGENT_LOCK) {
  351. mDefaultName = TS("门铃锁");
  352. }else if (type == CZ_DOORBELL) {
  353. mDefaultName = TS("创泽门铃");
  354. }else{
  355. mDefaultName = TS("传统监控设备");
  356. }
  357. return mDefaultName;
  358. }
  359. #pragma mark 获取当前设备类型对应的设备图片
  360. + (NSString*)getDeviceImageNameWithType:(int)type {
  361. NSString *devTypePath = [[NSBundle mainBundle] pathForResource:@"DeviceType" ofType:@"plist"];
  362. NSMutableDictionary *devtypeDic = [[NSMutableDictionary alloc] initWithContentsOfFile:devTypePath];
  363. NSMutableDictionary *deviceDic = [devtypeDic objectForKey:[NSString stringWithFormat:@"%d",type]];
  364. NSString *imagename = [deviceDic objectForKey:@"Image"];
  365. if (imagename) {
  366. return imagename;
  367. }
  368. return @"icon_funsdk.png";
  369. }
  370. @end