iOS开发之获取运营商和WIFI
下面是我們常用的一些獲取手機(jī)信息的方法,大家可以看看。
不過(guò)在目前的版本里面,很多手機(jī)信息已經(jīng)獲取不到了,因?yàn)樘O果在這方面的保密性做得越來(lái)越嚴(yán)格。
IMEI,IMSI,DeviceID,手機(jī)號(hào)碼等我們都已經(jīng)獲取不到了,下面一些我們可能獲取到的,也可能因?yàn)槭撬接蠥PI而導(dǎo)致審核被拒,大家在借鑒的時(shí)候要慎重。
/// @brief 獲取連接方式
+ (NSString *)getConfiguration{
? ? // 狀態(tài)欄是由當(dāng)前app控制的,首先獲取當(dāng)前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 @"無(wú)網(wǎng)絡(luò)";break;
? ? ? ? ? ? case 1:return @"2G";break;
? ? ? ? ? ? case 2:return @"3G";break;
? ? ? ? ? ? case 3:return @"4G";break;
? ? ? ? ? ? case 5:return @"WIFI";break;
?? ? ? ? ? ?
? ? ? ? default:return @"無(wú)網(wǎng)絡(luò)";
? ? ? ? ? ? break;
? ? }
}
/// @brief 獲取運(yùn)營(yíng)商信息 可能會(huì)被封殺
+ (NSString *)getCarrier {
? ? CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
? ? CTCarrier *carrier = [info subscriberCellularProvider];
? ? return [NSString stringWithFormat:@"%@",[carrier carrierName]];
}
/// @brief 獲取當(dāng)前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;
}
總結(jié)
以上是生活随笔為你收集整理的iOS开发之获取运营商和WIFI的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: BlackBerry 10 Blac
- 下一篇: php对英语单词,php英语单词,php