iOS常用的公共方法详解

2020-01-18 19:03:31于海丽

27. 将字典对象转换为 JSON 字符串


+ (NSString *)jsonPrettyStringEncoded:(NSDictionary *)dictionary{
 if ([NSJSONSerialization isValidJSONObject:dictionary ]) {
  NSError *error;
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
  if (!error) {
   NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   return json;
  }
 }
 return nil;
}

28.将数组对象转换为 JSON 字符串


+ (NSString *)jsonPrettyStringEncoded:(NSArray *)array{
 if ([NSJSONSerialization isValidJSONObject:array]) {
  NSError *error;
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
  if (!error) {
   NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   return json;
  }
 }
 return nil;
}

29. 获取 WiFi 信息

需要引入头文件:

#import <SystemConfiguration/CaptiveNetwork.h>

代码:


//获取 WiFi 信息
- (NSDictionary *)fetchSSIDInfo {
 NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
 if (!ifs) {
  return nil;
 }
 NSDictionary *info = nil;
 for (NSString *ifnam in ifs) {
  info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
  if (info && [info count]) { break; }
 }
 return info;
}

30. 获取广播地址、本机地址、子网掩码、端口信息

需要引入头文件:


p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #ff4647}span.s1 {font-variant-ligatures: no-common-ligatures; color: #eb905a}span.s2 {font-variant-ligatures: no-common-ligatures}
#import <ifaddrs.h>
#import <arpa/inet.h>

//获取广播地址、本机地址、子网掩码、端口信息
- (NSMutableDictionary *)getLocalInfoForCurrentWiFi {
 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
 struct ifaddrs *interfaces = NULL;
 struct ifaddrs *temp_addr = NULL;
 int success = 0;
 // retrieve the current interfaces - returns 0 on success
 success = getifaddrs(&interfaces);
 if (success == 0) {
  // Loop through linked list of interfaces
  temp_addr = interfaces;
  //*/
  while(temp_addr != NULL) {
   if(temp_addr->ifa_addr->sa_family == AF_INET) {
    // Check if interface is en0 which is the wifi connection on the iPhone
    if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
     //广播地址
     NSString *broadcast = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)];
     if (broadcast) {
      [dict setObject:broadcast forKey:@"broadcast"];
     }
//     NSLog(@"broadcast address--%@",broadcast);
     //本机地址
     NSString *localIp = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
     if (localIp) {
      [dict setObject:localIp forKey:@"localIp"];
     }
//     NSLog(@"local device ip--%@",localIp);
     //子网掩码地址
     NSString *netmask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
     if (netmask) {
      [dict setObject:netmask forKey:@"netmask"];
     }
//     NSLog(@"netmask--%@",netmask);
     //--en0 端口地址
     NSString *interface = [NSString stringWithUTF8String:temp_addr->ifa_name];
     if (interface) {
      [dict setObject:interface forKey:@"interface"];
     }
//     NSLog(@"interface--%@",interface);
     return dict;
    }
   }
   temp_addr = temp_addr->ifa_next;
  }
 }
 // Free memory
 freeifaddrs(interfaces);
 return dict;
}