1.获取当前设备可用内存(单位:MB)
+ (double)availableMemory {
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
if (kernReturn != KERN_SUCCESS) {
return NSNotFound;
}
return ((vm_page_size *vmStats.free_count) / 1024.0) / 1024.0;
}
2.获取当前任务所占用的内存(单位:MB)
+ (double)usedMemory {
task_basic_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&taskInfo,
&infoCount);
if (kernReturn != KERN_SUCCESS ) {
return NSNotFound;
}
return taskInfo.resident_size / 1024.0 / 1024.0;
}
3.获取当前设备的型号
+(NSString*)deviceString
{
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
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:@"iPhone4,1"]) return @"iPhone 4S";
if ([deviceString isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([deviceString isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
if ([deviceString isEqualToString:@"iPhone7,1"]) return @"iPhone 6Plus";
if ([deviceString isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([deviceString isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4";
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:@"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:@"i386"]) return @"Simulator";
if ([deviceString isEqualToString:@"x86_64"]) return @"Simulator";
NSLog(@"NOTE: Unknown device type: %@", deviceString);
return deviceString;
}
4.获取通讯录信息(AddressBook)
+(NSMutableArray*)getAddressBook
{
NSMutableArray *address_array=[NSMutableArray array];
ABAddressBookRef addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);
//获取通讯录权限
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
CFArrayRef allperson =ABAddressBookCopyArrayOfAllPeople(addressBooks);
for (id person_record in (__bridge NSArray *)allperson) {
NSMutableArray *phoneNumber_array=[NSMutableArray array];
ABRecordRef person=(__bridge ABRecordRef)(person_record);
//获取个人名字
CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
CFStringRef abFullName = ABRecordCopyCompositeName(person);
NSString *person_name = (__bridge NSString *)abName;
NSString *lastNameString = (__bridge NSString *)abLastName;
if ((__bridge id)abFullName != nil) {
person_name = (__bridge NSString *)abFullName;
} else {
if ((__bridge id)abLastName != nil)
{
person_name = [NSString stringWithFormat:@"%@ %@", person_name, lastNameString];
}
}
ABPropertyID multiProperties[] = {
kABPersonPhoneProperty,
};
ABPropertyID property = multiProperties[0];
ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
NSInteger valuesCount = 0;
if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
if (valuesCount == 0) {
CFRelease(valuesRef);
continue;
}
//获取电话号码
for (NSInteger k = 0; k < valuesCount; k++) {
CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
NSString *phoneNumber=[NSString stringWithFormat:@"%@",(__bridge NSString*)value];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"- ()"];
phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet: numbers] componentsJoinedByString:@""];
phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@"+86" withString:@""];
[phoneNumber_array addObject:phoneNumber];
CFRelease(value);
}
UIImage * image=nil;
//联系人头像
if(ABPersonHasImageData(person))
{
NSData * imageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(person,kABPersonImageFormatThumbnail);
image = [[UIImage alloc] initWithData:imageData];
}
CFRelease(valuesRef);
if([Utils isEmptyString:person_name])
{
if(phoneNumber_array.count>0)
{
person_name=[phoneNumber_array objectAtIndex:0];
}
else
{
person_name=@"无号码";
}
}
NSString* personName=[NSString stringWithFormat:@"%@",person_name];
if (abName) CFRelease(abName);
if (abLastName) CFRelease(abLastName);
if (abFullName) CFRelease(abFullName);
NSDictionary *dict=@{@"name":personName,@"phoneNumbers":phoneNumber_array,@"invite":@(NO)};
NSMutableDictionary *m_dict=[NSMutableDictionary dictionaryWithDictionary:dict];
if(image)
{
[m_dict setObject:image forKey:@"image"];
}
[address_array addObject:m_dict];
}
return address_array;
}
5.判断应用是否是第一次启动
在didFinishLaunchingWithOptions方法里添加:
if !UserDefaults.standard.bool(forKey: "everLaunched") {
UserDefaults.standard.set(true, forKey: "everLaunched")
UserDefaults.standard.set(true, forKey: "firstLaunch")
} else {
UserDefaults.standard.set(false, forKey: "firstLaunch")
}
"everLaunched"判断用户以前是否登录, "firstLaunch" 用来开发者在程序的其他部分判断.
在第一次启动的时候 key "everLaunched" 不会被赋址的, 并且设置为YES. "firstLaunch"被设置为 YES.
在其他代码段进行调用判断:
if UserDefaults.standard.bool(forKey: "firstLaunch") {
//是第一次启动应用
} else {
//不是第一次启动应用
}