iOS 10 与 Xcode8 中遇到的坑

由于众多用户更新了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.


屏幕快照 2016-10-25 下午6.13.53.png

看这段加上说明挺关键,否则就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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容