由于众多用户更新了iOS10 ,为了适配,不得不更新Xcode了,现总结更新中遇到的各种坑
1.权限相关引起的崩溃
我们需要打开info.plist文件添加相应权限的说明,否则程序在iOS10上会出现崩溃。
涉及到的权限可能有:
麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风?
相机权限: Privacy - Camera Usage Description 是否允许此App使用你的相机?
相册权限: Privacy - Photo Library Usage Description 是否允许此App访问你的媒体资料库?通讯录权限: Privacy - Contacts Usage Description 是否允许此App访问你的通讯录?
蓝牙权限:Privacy - Bluetooth Peripheral Usage Description 是否许允此App使用蓝牙?
语音转文字权限:Privacy - Speech Recognition Usage Description 是否允许此App使用语音识别?
日历权限:Privacy - Calendars Usage Description 是否允许此App使用日历?
定位权限:Privacy - Location When In Use Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据
定位权限: Privacy - Location Always Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据
其中相机权限,相册权限以前是不需要提供描述语的,现在不提供就会崩溃,代码部分不用改动;通讯录部分比较复杂:
涉及通讯录读取的有两个库:AddressBook.framework , Contacts.framework,
看看官方文档:
AddressBook.h
- AddressBook
- Copyright (c) 2010-2015 Apple Inc. All rights reserved.
- !!!! DEPRECATED !!!!
- AddressBook framework is deprecated as of iOS 9.0
- Please use Contacts framework instead.
- !!!! DEPRECATED !!!!
各种强调有没有!!,但是有时候为了兼容老版本,不能弃用老api,下面对通过UI库操作通讯录的部分做了下对比整理(若是隐式读取通讯录,有所不同)
1 | AddressBook | Contacts |
---|---|---|
< iOS 10 | 不需要代码申请权限,不需要提供描述语 | 不需要代码申请权限,不需要提供描述语(iOS9 以上可用) |
>= iOS10 | 需要代码申请权限,需要提供描述语 | 不需要代码申请权限,需要提供描述语 |
同时提供了一个兼容方案:
//引入库,继承接口
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
@interface YDLPasswordConfigViewController ()<ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate>
- (IBAction)onContactPressed:(id)sender {
//点击选择通讯录的用户所对应的操作
if (NSClassFromString(@"CNContactPickerViewController")) {
// iOS 9, 10, use CNContactPickerViewController
CNContactPickerViewController *picker = [[CNContactPickerViewController alloc] init];
picker.delegate = self;
picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
[self presentViewController:picker animated:YES completion:nil];
}else{
// iOS 8 Below, use ABPeoplePickerNavigationController
ABPeoplePickerNavigationController *pickerViewController = [ABPeoplePickerNavigationController new];
pickerViewController.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]]; //联系人信息中仅显示联系人的电话
pickerViewController.peoplePickerDelegate = self;
pickerViewController.navigationBar.topItem.title = @"请选择要授权人的手机号";
[self presentViewController:pickerViewController animated:YES completion:NULL];
}
}
//实现代理方法
#pragma mark --ABPeoplePickerNavigationControllerDelegate
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[peoplePicker dismissViewControllerAnimated:YES completion:NULL];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
ABMultiValueRef valueRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex index = ABMultiValueGetIndexForIdentifier(valueRef,identifier);
// NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
// NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
// firstName = firstName ? firstName : @"";
// lastName = lastName ? lastName : @"";
// NSString *name = [NSString stringWithFormat:@"%@%@",lastName,firstName]; //联系人姓名
NSString *phoneNumber = (__bridge NSString *)ABMultiValueCopyValueAtIndex(valueRef,index);
[peoplePicker dismissViewControllerAnimated:YES completion:NULL];
return NO;
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
ABMultiValueRef valueRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex index = ABMultiValueGetIndexForIdentifier(valueRef,identifier);
// NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
// NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
// firstName = firstName ? firstName : @"";
// lastName = lastName ? lastName : @"";
// NSString *name = [NSString stringWithFormat:@"%@%@",lastName,firstName]; //联系人姓名
NSString *phoneNumber = (__bridge NSString *)ABMultiValueCopyValueAtIndex(valueRef,index);
self.phoneField.text = [self phonenumber:phoneNumber];
[peoplePicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark --CNContactPickerDelegate
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{
CNPhoneNumber *phoneNumber = contactProperty.value;
NSString *phoneStr = phoneNumber.stringValue;
}
蓝牙权限是不需要的,因为我没加也不影响使用,查了下官方文档:
NSBluetoothPeripheralUsageDescription
NSBluetoothPeripheralUsageDescription (String - iOS) This key lets you describe the reason your app uses Bluetooth. When the system prompts the user to allow usage, the value that you provide for this key is displayed as part of the alert.
Important: To protect user privacy, an iOS app linked on or after iOS 10.0, and which accesses the Bluetooth interface, must statically declare the intent to do so. Include the NSBluetoothPeripheralUsageDescription key in your app’s Info.plist file and provide a purpose string for this key. If your app attempts to access the Bluetooth interface without a corresponding purpose string, your app exits.
This key is supported in iOS 6.0 and later.
看这段加上说明挺关键,否则就exits!?但是并没有,看stackoverflow的回答:
The value for this key is used for a different permission, not just having bluetooth off.
If your app requests the bluetooth-peripheral background mode, the system will ask the user for permission. The default message displayed to the user is:
[App Name] would like to make data available to nearby bluetooth devices even when you're not using the app.
The NSBluetoothPeripheralUsageDescription key allows you to customize the above request for permission.
应该就是因为这段说明只会在app作为从设备才会出现吧。
2. 页面按钮突然不见了
之前一直是好好的,自从更了Xcode8 有些页面就变的怪怪的,
搜到这个回答Xcode 8 UIButtons with constraints not showing up里面给出了多个解决方法,逐一尝试,总有一款适合你。我用到了两种:
generate this button on viewDidLoad on a controller via an NSObject class which handles it. The solution for me was forcing a layoutIfNeeded before calling the method that setUps the button.
-(void)viewDidLoad {
[super viewDidLoad];
[self.view layoutIfNeeded];
//editBooking - NSObject class where the button gets configured.
[_editBooking setUpViews];
}
还有一个是:
The solution is to change the Xcode version in the interface builder..
Steps,
Select your .xib file.
Open the Utilities section.
Select "File Inspector"
Under the "Interface Builder Documents" change the Xcode version of "Opens in" to "Xcode 7.x"
3. 蓝牙状态错误问题
有用户反映iOS10 下蓝牙连接不上设备,通过调试信息发现:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
CBCentralManagerState state = central.state;
}
状态变化回调中状态是CBCentralManagerStateUnsupported
,这就很奇怪了。后台发现,这是CBCentralManager被置空之后再重新初始化就会引发这个问题。大致搜了下,以前系统更新似乎也出现过类似问题,跟manager的多次初始化有关系。于是取消了置空操作。
附录
别人总结的部分问题:
http://blog.csdn.net/jnbbwyth/article/details/52576169