你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS獲取當前設備型號等信息(全)包含iPhone7和iPhone7P

iOS獲取當前設備型號等信息(全)包含iPhone7和iPhone7P

編輯:IOS開發綜合

#include <sys/types.h> 
#include <sys/sysctl.h>
//獲得設備型號 
+ (NSString *)getCurrentDeviceModel 
{ 
int mib[2]; 
size_t len; 
charchar *machine; 
mib[0] = CTL_HW; 
mib[1] = HW_MACHINE; 
sysctl(mib, 2, NULL, &len, NULL, 0); 
machine = malloc(len); 
sysctl(mib, 2, machine, &len, NULL, 0); 
NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding]; 
free(machine); 
// iPhone 
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone2G"; 
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone3G"; 
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone3GS"; 
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone4"; 
if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone4"; 
if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone4"; 
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone4S"; 
if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone5"; 
if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone5"; 
if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone5c"; 
if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone5c"; 
if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone5s"; 
if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone5s"; 
if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone6"; 
if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone6Plus"; 
if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone6s"; 
if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone6sPlus"; 
if ([platform isEqualToString:@"iPhone8,3"]) return @"iPhoneSE"; 
if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhoneSE"; 
if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone7"; 
if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone7Plus"; 
//iPod Touch 
if ([platform isEqualToString:@"iPod1,1"]) return @"iPodTouch"; 
if ([platform isEqualToString:@"iPod2,1"]) return @"iPodTouch2G"; 
if ([platform isEqualToString:@"iPod3,1"]) return @"iPodTouch3G"; 
if ([platform isEqualToString:@"iPod4,1"]) return @"iPodTouch4G"; 
if ([platform isEqualToString:@"iPod5,1"]) return @"iPodTouch5G"; 
if ([platform isEqualToString:@"iPod7,1"]) return @"iPodTouch6G"; 
//iPad 
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad"; 
if ([platform isEqualToString:@"iPad2,1"]) return @"iPad2"; 
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad2"; 
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad2"; 
if ([platform isEqualToString:@"iPad2,4"]) return @"iPad2"; 
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad3"; 
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad3"; 
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad3"; 
if ([platform isEqualToString:@"iPad3,4"]) return @"iPad4"; 
if ([platform isEqualToString:@"iPad3,5"]) return @"iPad4"; 
if ([platform isEqualToString:@"iPad3,6"]) return @"iPad4"; 
//iPad Air 
if ([platform isEqualToString:@"iPad4,1"]) return @"iPadAir"; 
if ([platform isEqualToString:@"iPad4,2"]) return @"iPadAir"; 
if ([platform isEqualToString:@"iPad4,3"]) return @"iPadAir"; 
if ([platform isEqualToString:@"iPad5,3"]) return @"iPadAir2"; 
if ([platform isEqualToString:@"iPad5,4"]) return @"iPadAir2"; 
//iPad mini 
if ([platform isEqualToString:@"iPad2,5"]) return @"iPadmini1G"; 
if ([platform isEqualToString:@"iPad2,6"]) return @"iPadmini1G"; 
if ([platform isEqualToString:@"iPad2,7"]) return @"iPadmini1G"; 
if ([platform isEqualToString:@"iPad4,4"]) return @"iPadmini2"; 
if ([platform isEqualToString:@"iPad4,5"]) return @"iPadmini2"; 
if ([platform isEqualToString:@"iPad4,6"]) return @"iPadmini2"; 
if ([platform isEqualToString:@"iPad4,7"]) return @"iPadmini3"; 
if ([platform isEqualToString:@"iPad4,8"]) return @"iPadmini3"; 
if ([platform isEqualToString:@"iPad4,9"]) return @"iPadmini3"; 
if ([platform isEqualToString:@"iPad5,1"]) return @"iPadmini4"; 
if ([platform isEqualToString:@"iPad5,2"]) return @"iPadmini4"; 
if ([platform isEqualToString:@"i386"]) return @"iPhoneSimulator"; 
if ([platform isEqualToString:@"x86_64"]) return @"iPhoneSimulator"; 
return platform; 
}

獲取設備信息總結

1、獲取設備的信息

UIDevice *device = [[UIDevice alloc] int]; 
NSString *name = device.name; //獲取設備所有者的名稱 
NSString *model = device.name; //獲取設備的類別 
NSString *type = device.localizedModel; //獲取本地化版本 
NSString *systemName = device.systemName; //獲取當前運行的系統 
NSString *systemVersion = device.systemVersion;//獲取當前系統的版本

2、獲取設備的唯一標示符

NSString *identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

3、為系統創建一個隨機的標示符

(NSString*) createUUID 
{ 
NSString *id = [[NSUserDefaults standardUserDefaults] objectForKey:@"UUID"]; //獲取標識為"UUID"的值 
if(id == nil) 
{ 
if([[[UIDevice currentDevice] systemVersion] floatValue] > 6.0) 
{ 
NSString *identifierNumber = [[NSUUID UUID] UUIDString]; //ios 6.0 之後可以使用的api 
[[NSUserDefaults standardUserDefaults] setObject:identifierNumber forKey:@"UUID"]; //保存為UUID 
[[NSUserDefaults standardUserDefaults] synchronize]; 
} 
else{ 
CFUUIDRef uuid = CFUUIDCreate(NULL); 
CFStringRef uuidString = CFUUIDCreateString(NULL, uuid); //ios6.0之前使用的api 
NSString *identifierNumber = [NSString stringWithFormat:@"%@", uuidString]; 
[[NSUserDefaults standardUserDefaults] setObject:identifierNumber forKey:@"UUID"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 
CFRelease(uuidString); 
CFRelease(uuid); 
} 
return [[NSUserDefaults standardUserDefaults] objectForKey@"UUID"]; 
} 
return id; 
}

4、獲取當前屏幕分辨率的信息

CGRect rect = [[UIScreen mainScreen] bounds]; 
CGFloat scale = [[UIScreen mainScreen].scale]; 
CGFloat width = rect.size.width * scale; 
CGFloat height = rect.size.height * scale;

5、獲取運營商的信息

需要先導入頭文件

#import <CoreTelephony/CTCarrier.h> 
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

創建對象

CCTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];

獲取運行商的名稱

CTCarrier *carrier = [info subscriberCellularProvider]; 
NSString *mCarrier = [NSString stringWithFormat:@"%@",[carrier carrierName]];

獲取當前網絡的類型

ios7之後可以按照以下方式獲取。方便而且類型多

NSString *mConnectType = [[NSString alloc] initWithFormat:@"%@",info.currentRadioAccessTechnology];

類型有以下:

CTRadioAccessTechnologyGPRS //介於2G和3G之間,也叫2.5G ,過度技術 
CTRadioAccessTechnologyEdge //EDGE為GPRS到第三代移動通信的過渡,EDGE俗稱2.75G 
CTRadioAccessTechnologyWCDMA 
CTRadioAccessTechnologyHSDPA //亦稱為3.5G(3?G) 
CTRadioAccessTechnologyHSUPA //3G到4G的過度技術 
CTRadioAccessTechnologyCDMA1x //3G 
CTRadioAccessTechnologyCDMAEVDORev0 //3G標准 
CTRadioAccessTechnologyCDMAEVDORevA 
CTRadioAccessTechnologyCDMAEVDORevB 
CTRadioAccessTechnologyeHRPD //電信使用的一種3G到4G的演進技術, 3.75G 
CTRadioAccessTechnologyLTE //接近4G

ios7之前的話apple給我們提供了Reachability來獲取。

首先要導入SystemConfiguration.framework,把下載下來的Reachability.h和Reachability.m加進項目中

Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"]; 
switch([reach currentReachabilityStatus]) 
{ 
case NotReachable: //沒有連接上 
//do something 
break; 
case ReachableViaWiFi: //通過wifi連接 
//do something 
break; 
case ReachableViaWWAN: //通過GPRS連接 
//do something 
break; 
default: <span style="white-space:pre"> </span>//未知情況 
//do something 
break; 
}

6、獲取當前信號的強弱

這個貌似沒有給出官方的api,但是網上有人說可以用私有的api實現,但是通不過appStore的審核,方法如下:

利用linux下動態庫顯式調用api的函數。先包含頭文件 #import <dlfcn.h>

(int) getSignalLevel 
{ 
voidvoid *libHandle = dlopen("/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony",RTLD_LAZY);//獲取庫句柄 
int (*CTGetSignalStrength)(); //定義一個與將要獲取的函數匹配的函數指針 
CTGetSignalStrength = (int(*)())dlsym(libHandle,"CTGetSignalStrength"); //獲取指定名稱的函數 

if(CTGetSignalStrength == NULL) 
return -1; 
else{ 
int level = CTGetSignalStrength(); 
dlclose(libHandle); //切記關閉庫 
return level 
} 
}

7、設備震動

需要加入AudioToolbox framework,導入頭文件 #import <AudioToolbox/AudioToolbox.h>

在需要震動的地方添加代碼:

AudioServicesPlaySystemSound ( kSystemSoundID_Vibrate) ;

但是貌似這個不支持傳入震動時間和模式,自己去控制吧。

8、獲取電池的相關信息

@implementation BatterMonitor 
//獲取電池當前的狀態,共有4種狀態 
-(NSString*) getBatteryState { 
UIDevice *device = [UIDevice currentDevice]; 
if (device.batteryState == UIDeviceBatteryStateUnknown) { 
return @"UnKnow"; 
}else if (device.batteryState == UIDeviceBatteryStateUnplugged){ 
return @"Unplugged"; 
}else if (device.batteryState == UIDeviceBatteryStateCharging){ 
return @"Charging"; 
}else if (device.batteryState == UIDeviceBatteryStateFull){ 
return @"Full"; 
} 
return nil; 
} 
//獲取電量的等級,0.00~1.00 
-(float) getBatteryLevel { 
return [UIDevice currentDevice].batteryLevel; 
} 

-(void) getBatteryInfo 
{ 
NSString *state = getBatteryState(); 
float level = getBatteryLevel()*100.0; 
//yourControlFunc(state, level); //寫自己要實現的獲取電量信息後怎麼處理 
} 
//打開對電量和電池狀態的監控,類似定時器的功能 
-(void) didLoad 
{ 
[[UIDevice currentDevice] setBatteryMonitoringEnable:YES]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatteryInfo:) name:UIDeviceBatteryStateDidChangeNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatteryInfo:) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; 
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(getBatteryInfo:) userInfo:nil repeats:YES]; 
} 
@end

9、app中打開一個網頁

這個比較簡單,直接用提供的接口openURL即可。

NSString *url = @"www.apple.com" 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

10、app中打開另一個app

打開另一個app還是可以通過openURL來實現。但是要分兩種情況。第一種是啟動內置的應用,一般的電話,浏覽器,短信和

郵件可以直接調用並添加參數,譬如

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://[email protected]"]]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10086"]];

第二種情況是要打開自己開發的app,這種情況則要為將要打開的app注冊一個URL協議。這個可以在項目的文件info.plist中注冊。

主要操作為:

Step1. 右鍵,選擇“Add Row”

Step2. Key值選擇“URL types”

Step3. 打開“Item 0″,然後為該key增加一個URL identifier。可以是任何值,但建議用“反域名”(例如 “com.fcplayer.testHello”)。

Step4. 在“Item 0”下再加一行。

Step5. 選擇“URL Schemes” 作為Key。

Step6. 輸入你的URL協議名 (例如“testHello://” 應寫做“testHello”)。如果有必要,你可以在這裡加入多個協議。

其實在打開的時候只需要URL Schemes即可,URL identifier是可選項。如果需要傳送參數,可以在URL Schemes://添加你的參數,格式和網頁開發的傳遞參數差不多。(又或者URL Schemes://URL identifier@添加的參數)關鍵是要和接收參數方定義好處理的方式。然後在需要打開的地方添加代碼:

NSString *url = @"URL Schemes的路徑" 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

以上所述是小編給大家介紹的iOS獲取當前設備型號等信息包含iPhone7和iPhone7P,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved