iOS开发之获取运营商和WIFI
下面是我們常用的一些獲取手機信息的方法,大家可以看看。
不過在目前的版本里面,很多手機信息已經獲取不到了,因為蘋果在這方面的保密性做得越來越嚴格。
IMEI,IMSI,DeviceID,手機號碼等我們都已經獲取不到了,下面一些我們可能獲取到的,也可能因為是私有API而導致審核被拒,大家在借鑒的時候要慎重。
/// @brief 獲取連接方式
+ (NSString *)getConfiguration{
? ? // 狀態欄是由當前app控制的,首先獲取當前app
? ? UIApplication *app = [UIApplication sharedApplication];
? ? NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
? ? int type = 0;
? ? for (id child in children) {
?? ? ? ? ? if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
? ? ? ? ? ? ? ? type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
? ? ? ? ? ? }
? ? }
?? ?
? ? switch (type) {
? ? ? ? case 0:return @"無網絡";break;
? ? ? ? ? ? case 1:return @"2G";break;
? ? ? ? ? ? case 2:return @"3G";break;
? ? ? ? ? ? case 3:return @"4G";break;
? ? ? ? ? ? case 5:return @"WIFI";break;
?? ? ? ? ? ?
? ? ? ? default:return @"無網絡";
? ? ? ? ? ? break;
? ? }
}
/// @brief 獲取運營商信息 可能會被封殺
+ (NSString *)getCarrier {
? ? CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
? ? CTCarrier *carrier = [info subscriberCellularProvider];
? ? return [NSString stringWithFormat:@"%@",[carrier carrierName]];
}
/// @brief 獲取當前wifi名稱
+ (NSString *)getSSID {
? ? NSString *ssid = nil;
? ? NSArray *ifs = (__bridge ? id)CNCopySupportedInterfaces();
? ? for (NSString *ifname in ifs) {
? ? ? ? NSDictionary *info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);
? ? ? ? if (info[@"SSID"])
? ? ? ? {
? ? ? ? ? ? ssid = info[@"SSID"];
? ? ? ? }
? ? }
? ? return ssid;
}
/// @brief 獲取bssid
+ (NSString *)getbSSID {
? ? NSString *bssid = nil;
? ? NSArray *ifs = (__bridge ? id)CNCopySupportedInterfaces();
? ? for (NSString *ifname in ifs) {
? ? ? ? NSDictionary *info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);
? ? ? ? if (info[@"BSSID"])
? ? ? ? {
? ? ? ? ? ? bssid = info[@"BSSID"];
? ? ? ? }
? ? }
? ? return bssid;
}
//獲取ip地址
+ (NSString *)getIpAddresses{
? ? NSString *address = @"error";
? ? 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"])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? // Get NSString from C String
? ? ? ? ? ? ? ? ? ? address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? temp_addr = temp_addr->ifa_next;
? ? ? ? }
? ? }
? ? // Free memory
? ? freeifaddrs(interfaces);
? ? return address;
}
總結
以上是生活随笔為你收集整理的iOS开发之获取运营商和WIFI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BlackBerry 10 Blac
- 下一篇: iOS-位移枚举简单介绍