你好,歡迎來到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/CTTelephon.networkInfo.h>

創立對象

CCTelephon.networkInfo *info = [[CTTelephon.networkInfo 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 > </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 DosBat target=_blank class=infotextkey>BatterMonitor 
//獲得電池以後的狀況,共有4種狀況 
-(NSString*) getDosBat target=_blank class=infotextkey>BatteryState { 
UIDevice *device = [UIDevice currentDevice]; 
if (device.DosBat target=_blank class=infotextkey>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,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對本站網站的支撐!

【iOS獲得以後裝備型號等信息(全)包括iPhone7和iPhone7P】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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