目录:
1.获取当前屏幕宽度、高度
2.获取当前设备ip
3.获取手机系统版本
4.获取设备型号
5.获取设备版本号
6.判断当前的iPhone设备/系统版本
7.判断是真机还是模拟器
8.获取唯一设备标识 UUID
先了解iPhone相关尺寸:
1.获取当前屏幕宽度、高度
CGFloat *UI_View_Width = [UIScreen mainScreen].bounds.size.width
CGFloat *UI_View_Height = [UIScreen mainScreen].bounds.size.height
拿到尺寸我们可以做什么呢:
- 1.1 针对设备机型5以下 由于屏幕宽度相对较,我们的产品大大让我针对5以下将某些控件字段的字号单独对待相对调小字号,解决展示不全问题
if ([UIScreen mainScreen].bounds.size.width < 375.0) {
orderNo.font = [UIFont systemFontOfSize:12];
} else {
orderNo.font = [UIFont systemFontOfSize:14];
}
- 1.2 当一张展示图需求是等比例的缩放,这时候也需要拿到当前屏幕的宽高去展示,
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UI_View_Width, UI_View_Height)];
scrollView.backgroundColor = QTXBackgroundColor;
[self.view addSubview:scrollView];
CGFloat height = 2920 * UI_View_Width / 750; // 我的原图尺寸是750*2920
scrollView.contentSize = CGSizeMake(0, height);
UIImageView *imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"consume"]];
imageview.frame = CGRectMake(0, 0, UI_View_Width, height);
[scrollView addSubview:imageview];
2.获取当前设备ip
#import "QTXGetIPTool.h"
#import <ifaddrs.h>
#import <arpa/inet.h>
@implementation QTXGetIPTool
+ (NSString *)deviceIPAdress {
NSString *address = @"an error occurred when obtaining ip address";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0) { // 0 表示获取成功
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;
}
}
freeifaddrs(interfaces);
NSLog(@"手机设备的IP是:%@", address);
return address;
}
3.获取手机系统版本
+(NSString *)phoneVersion{
return [[UIDevice currentDevice] systemVersion];
}
4.获取设备型号
这个方法只能获取到iPhone、iPad这种信息,无法获取到是iPhone 4、iPhpone5这种具体的型号
+(NSString *)phoneModel{
return [[UIDevice currentDevice] model];
}
5.获取设备版本号
+ (NSString*)deviceVersion
{
// 需要#import "sys/utsname.h"
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
//iPhone
if ([deviceString isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([deviceString isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([deviceString isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([deviceString isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([deviceString isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4";
if ([deviceString isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([deviceString isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([deviceString isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([deviceString isEqualToString:@"iPhone5,3"]) return @"iPhone 5C";
if ([deviceString isEqualToString:@"iPhone5,4"]) return @"iPhone 5C";
if ([deviceString isEqualToString:@"iPhone6,1"]) return @"iPhone 5S";
if ([deviceString isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
if ([deviceString isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([deviceString isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([deviceString isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
if ([deviceString isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
//iPod
if ([deviceString isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([deviceString isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([deviceString isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([deviceString isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([deviceString isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G"; return @"iPod Touch 5G";
//iPad
if ([deviceString isEqualToString:@"iPad1,1"]) return @"iPad";
if ([deviceString isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
if ([deviceString isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([deviceString isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([deviceString isEqualToString:@"iPad2,4"]) return @"iPad 2 (32nm)";
if ([deviceString isEqualToString:@"iPad2,5"]) return @"iPad mini (WiFi)";
if ([deviceString isEqualToString:@"iPad2,6"]) return @"iPad mini (GSM)";
if ([deviceString isEqualToString:@"iPad2,7"]) return @"iPad mini (CDMA)";
if ([deviceString isEqualToString:@"iPad3,1"]) return @"iPad 3(WiFi)";
if ([deviceString isEqualToString:@"iPad3,2"]) return @"iPad 3(CDMA)";
if ([deviceString isEqualToString:@"iPad3,3"]) return @"iPad 3(4G)";
if ([deviceString isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
if ([deviceString isEqualToString:@"iPad3,5"]) return @"iPad 4 (4G)";
if ([deviceString isEqualToString:@"iPad3,6"]) return @"iPad 4 (CDMA)";
if ([deviceString isEqualToString:@"iPad4,1"]) return @"iPad Air";
if ([deviceString isEqualToString:@"iPad4,2"]) return @"iPad Air";
if ([deviceString isEqualToString:@"iPad4,3"]) return @"iPad Air";
if ([deviceString isEqualToString:@"iPad5,3"]) return @"iPad Air 2";
if ([deviceString isEqualToString:@"iPad5,4"]) return @"iPad Air 2";
if ([deviceString isEqualToString:@"i386"]) return @"Simulator";
if ([deviceString isEqualToString:@"x86_64"]) return @"Simulator";
if ([deviceString isEqualToString:@"iPad4,4"]||[deviceString isEqualToString:@"iPad4,5"]@"iPad4,5"]||[deviceString isEqualToString:@"iPad4,6"]) return @"iPad mini 2";
if ([deviceString isEqualToString:@"iPad4,7"]||[deviceString isEqualToString:@"iPad4,8"]||[deviceString isEqualToString:@"iPad4,9"]) return @"iPad mini 3";
return deviceString;
}
6.判断当前的iPhone设备/系统版本
//判断是否为iPhone
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
//判断是否为iPad
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//判断是否为ipod
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
// 判断是否为 iPhone 5SE
#define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f
// 判断是否为iPhone 6/6s
#define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f
// 判断是否为iPhone 6Plus/6sPlus
#define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f
//获取系统版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//判断 iOS 8 或更高的系统版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
7.判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
8.获取唯一设备标识 UUID
不要忘了打开Keychain Sharing, 即使卸载也不会更改 UUID
ZLGetUUID文件:
// 获取设备唯一标识 UUID
#import <Foundation/Foundation.h>
@interface ZLGetUUID : NSObject
+(NSString *)getUUID;
@end
#import "ZLGetUUID.h"
#import "ZLKeyChainStore.h"
#define KEY_USERNAME_PASSWORD @"com.company.app.usernamepassword"
#define KEY_USERNAME @"com.company.app.username"
#define KEY_PASSWORD @"com.company.app.password"
@implementation ZLGetUUID
+ (NSString *)getUUID {
NSString * strUUID = (NSString *)[ZLKeyChainStore load:@"com.company.app.usernamepassword"];
// 首次执行该方法时,uuid为空
if ([strUUID isEqualToString:@""] || !strUUID)
{
//生成一个uuid的方法
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
//将该uuid保存到keychain
[ZLKeyChainStore save:KEY_USERNAME_PASSWORD data:strUUID];
}
return strUUID;
}
@end
ZLKeyChainStore文件:
#import <Foundation/Foundation.h>
@interface ZLKeyChainStore : NSObject
+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)deleteKeyData:(NSString *)service;
@end
#import "ZLKeyChainStore.h"
@implementation ZLKeyChainStore
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword,(id)kSecClass,
service, (id)kSecAttrService,
service, (id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
nil];
}
+ (void)save:(NSString *)service data:(id)data {
//Get search dictionary
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Delete old item before add new item
SecItemDelete((CFDictionaryRef)keychainQuery);
//Add new object to search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
//Add item to keychain with the search dictionary
SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Configure the search setting
//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}
+ (void)deleteKeyData:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
@end
举个🌰,我们产品要求一个手机号就绑定两个设备,那么我们就需要读取这个设备唯一标识号,在登录或者注册接口里,作为一个参数传给后台,后台去判断.
#import "ZLGetUUID.h"
params[@"equipmentNumber"] = [ZLGetUUID getUUID]; // 唯一设备号