最近项目中需要获取一些权限,于是就大致整理搜集了下,都是基于系统最基本的
这些都是不需要获取用户权限的
- 手机型号
- 手机版本号
- 手机网络类型
- 手机运营商
这些是需要获取用户权限的
- 手机通讯录
- 手机定位(系统自带)
- 手机型号
//获得设备型号
-(void)mobileType
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
NSString * vv = @"";
if ([platform isEqualToString:@"iPhone4,1"]) vv = @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,2"]) vv = @"iPhone 5";
if ([platform isEqualToString:@"iPhone6,2"]) vv = @"iPhone 5S";
if ([platform isEqualToString:@"iPhone7,1"]) vv = @"iPhone 6Plus";
if ([platform isEqualToString:@"iPhone7,2"]) vv = @"iPhone 6";
if ([platform isEqualToString:@"iPhone8,2"]) vv = @"iPhone 6s Plus";
if ([platform isEqualToString:@"iPhone8,1"]) vv = @"iPhone 6s ";
if ([platform isEqualToString:@"iPhone9,1"]) vv = @"iPhone 7Plus"; //不确定
if ([platform isEqualToString:@"iPhone9,2"]) vv = @"iPhone 7"; //不确定
if ([platform isEqualToString:@"i386"]) vv = @"iPhone Simulator";
if ([platform isEqualToString:@"x86_64"]) vv = @"iPhone Simulator";
self.mobileModel = vv;
}
- 手机版本号
-(void)deviceVision{
self.devVision = [[UIDevice currentDevice]systemVersion];
}
- 手机网络类型
-(void)networktype{
NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
NSNumber *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}
switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
case 0:
NSLog(@"No wifi or cellular");
self.netSignal =@"无服务";
break;
case 1:
NSLog(@"2G");
self.netSignal=@"2G";
break;
case 2:
NSLog(@"3G");
self.netSignal=@"3G";
break;
case 3:
NSLog(@"4G");
self.netSignal=@"4G";
break;
case 4:
NSLog(@"LTE");
self.netSignal=@"LTE";
break;
case 5:
NSLog(@"Wifi");
self.netSignal=@"Wifi";
break;
default:
break;
}
}
- 手机运营商
//CoreTelephony.framework
//#import <CoreTelephony/CTTelephonyNetworkInfo.h>
//#import <CoreTelephony/CTCarrier.h>
-(void)getcarrierName{
CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
NSString *currentCountry=[carrier carrierName];
NSLog(@"[carrier isoCountryCode]==%@,[carrier allowsVOIP]=%d,[carrier mobileCountryCode=%@,[carrier mobileCountryCode]=%@",[carrier isoCountryCode],[carrier allowsVOIP],[carrier mobileCountryCode],[carrier mobileNetworkCode]);
self.mobileNet=currentCountry;
}
- 手机通讯录
//#import <AddressBook/ABPerson.h>
//#import <AddressBookUI/ABPersonViewController.h>
//获取权限
-(void)getrightFromUser {
//这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录
int __block tip=0;
//声明一个通讯簿的引用
ABAddressBookRef addBook =nil;
//创建通讯簿的引用
addBook=ABAddressBookCreateWithOptions(NULL, NULL);
//创建一个出事信号量为0的信号
dispatch_semaphore_t sema=dispatch_semaphore_create(0);
//申请访问权限
ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error) {
//greanted为YES是表示用户允许,否则为不允许
if (!greanted) {
tip=1;
}
//发送一次信号
dispatch_semaphore_signal(sema);
});
//等待信号触发
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
if (tip) {
//我们要在这里做一个提示框提示用户给了授权才能获取
return;
}
//这个是用来获取数据的
[self getPhoneContactAddress:addBook];
}
//获取手机通讯录中的信息
- (void)getPhoneContactAddress:(ABAddressBookRef )addBook {
//手机通讯录获取存放的数组
self.contactArray = [NSMutableArray array];
//获取所有联系人的数组
CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
//获取联系人总数
CFIndex number = ABAddressBookGetPersonCount(addBook);
for (int i = 0; i < number; i++) {
ABRecordRef people = CFArrayGetValueAtIndex(allLinkPeople, i);
//获取当前联系人名字
NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
//获取当前联系人姓氏
NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty)); //获取当前联系人中间名
NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
//获取联系人的头像
NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(people));
//注意这里的手机号是一个数组, 因为一人可以有很多个手机号, 这里我处理的是一个手机号对应一个姓名, 多个手机号就有多个相同的姓名不同手机号的模型
NSMutableArray * phoneArr = [[NSMutableArray alloc]init];
ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty);
for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
[phoneArr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))];
}
for (int i = 0; i< phoneArr.count; i++) {
MyAddressBook *contactModel = [[MyAddressBook alloc] init];//把名字拼接在一块
contactModel.name = [NSString stringWithFormat:@"%@%@%@", lastName, middleName, firstName];
contactModel.name = [contactModel.name stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
if (contactModel.name == nil || [contactModel.name isEqualToString:@""]) {
contactModel.name = @"无姓名";
}
contactModel.tel = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i));
contactModel.tel = [contactModel.tel stringByReplacingOccurrencesOfString:@"-" withString:@""];
[self.contactArray addObject:contactModel];
}
}
_tv.text = [self replaceUnicode:self.contactArray.description];
NSLog(@"%@",self.contactArray);
}
//unicode编码以\u开头
- (NSString *)replaceUnicode:(NSString *)unicodeStr
{
NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}
- 手机定位(系统自带)
//CoreLocation.framework
//#import <CoreLocation/CoreLocation.h>//定位
- (void)loaction
{
if (!_locationManager) {
_locationManager= [[CLLocationManager alloc] init];
}
_locationManager.delegate=self;
_locationManager.desiredAccuracy=kCLLocationAccuracyBest;
_locationManager.distanceFilter=kCLDistanceFilterNone;
if (IOS8){
[_locationManager requestWhenInUseAuthorization];
}
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error){
if(error == nil && [placemarks count]>0)
{
CLPlacemark *placemark=[placemarks firstObject];
NSString * addName = placemark.addressDictionary[@"Name"];//详细
NSString *country = placemark.addressDictionary[@"Country"];//国家
NSString *province = placemark.addressDictionary[@"State"];//省
NSString *city = placemark.addressDictionary[@"City"];//城市
NSString *district = placemark.addressDictionary[@"SubLocality"];//小城市
NSString *street = placemark.addressDictionary[@"Street"];//街道
NSMutableDictionary *addressDic = [[NSMutableDictionary alloc]init];
[addressDic setObject:addName forKey:@"Address"];
[addressDic setObject:country forKey:@"Country"];
[addressDic setObject:province forKey:@"Province"];
[addressDic setObject:city forKey:@"City"];
[addressDic setObject:district forKey:@"District"];
[addressDic setObject:street forKey:@"Street"];
//这里是将NSString转化为json类型,相信很多都会用到
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:addressDic options:NSJSONWritingPrettyPrinted error:nil];
self.placeName = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
else
{
DLOG(@"获取当前地址失败");
}
}];
[_locationManager stopUpdatingLocation];
}
网友评论