iOS 10 开发 & Xcode 8 新特性及常见问题解决

1,888 阅读9分钟
原文链接: www.jianshu.com

iOS 10 开发这次更新主要表现在以下这几个方面。

1.语音识别
苹果官方在文档中新增了API Speech,那么在以前我们处理语音识别非常的繁琐甚至很多时候可能需要借助于第三方框架处理,那么苹果推出了这个后,我们以后处理起来就非常的方便了,speech具有以下特点:

  • 可以实现连续的语音识别
  • 可以对语 音文件或者语音流进行识别
  • 最佳化自由格式的听写(可理解为多语言支持)和搜索式的字符串

核心代码:

#import  
/** 语音识别同样的需要真机进行测试 ,因为需要硬件的支持,还需要访问权限 */ 
//1.创建本地化标识符 
NSLocale *local =[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];  
//2.创建一个语音识别对象 
SFSpeechRecognizer *sf =[[SFSpeechRecognizer alloc] initWithLocale:local];  
//3.将bundle 中的资源文件加载出来返回一个url 
NSURL *url =[[NSBundle mainBundle] URLForResource:@"太想爱你(张信哲)mp3" withExtension:nil];  
//4.将资源包中获取的url 传递给 request 对象
SFSpeechURLRecognitionRequest *res =[[SFSpeechURLRecognitionRequest alloc] initWithURL:url];  
//5.发送一个请求 
[sf recognitionTaskWithRequest:res resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
 if (error!=nil) {
 NSLog(@"语音识别解析失败,%@",error); }else{//解析正确NSLog(@"---%@",result.bestTranscription.formattedString); 
} 
    }];

2.UITabBarController 中的改进
在iOS 10之前,tabBarItem上的文字颜色,默认是 蓝色,上面的新消息提醒数字badge 默认是红色的,未选中的TabBarItem的文字颜色默认是黑色的,我们修改的话,也只能修改它的默认颜色 ,其它的就不能进行个性化定制,使用起来非常的不方便,iOS10之后我们可以轻松个性化定制了。

核心代码:

//1.创建出三个UIViewcontroller 对象  
OneViewController *oneVc =[[OneViewController alloc] init];  
//2.设置每一个控制器上的
tabbar oneVc.view.backgroundColor =[UIColor redColor];     
//设置标题 
oneVc.tabBarItem.title = @"首页";    
TwoViewController *twovC =[[TwoViewController alloc] init];  twovC.view.backgroundColor =[UIColor purpleColor];   
//设置标题 twovC.tabBarItem.title = @"圈子"; 
ThreeViewController *threVC =[[ThreeViewController alloc] init];
threVC.view.backgroundColor =[UIColor blueColor];  
//设置标题 threVC.tabBarItem.title = @"社交";   
//2.将创建好的三个普通控制器加入到tabbarController 控制器中  
[self addChildViewController:oneVc];  
[self addChildViewController:twovC]; 
[self addChildViewController:threVC];    
//改变tabbar 上面的文字默认颜色  oneVc.tabBarController.tabBar.tintColor =[UIColor yellowColor];  
twovC.tabBarController.tabBar.tintColor =[UIColor yellowColor];  
threVC.tabBarController.tabBar.tintColor =[UIColor yellowColor];   //使用iOS 10新推出的 修改 tabbar 未选中的tintColor 颜色  
//这一句代码将 tabbar 未选中的时候的默认色- 黑色改为红色
oneVc.tabBarController.tabBar.unselectedItemTintColor =[UIColor redColor];   
//tabbarItem 中属性  
//数字提醒的颜色 在iOS 10之前的版本默认都是数字提醒都是红色 oneVc.tabBarItem.badgeColor =[UIColor orangeColor]; oneVc.tabBarItem.badgeValue =@"90";  
//将tabBarItem 中数字提醒默认的白色改掉 使用富文本修改 
[oneVc.tabBarItem setBadgeTextAttributes:@{ NSForegroundColorAttributeName:[UIColor blackColor] } forState:UIControlStateNormal];

3.iOS10.0中字体跟随系统设置变化大小
在以前如果说我们想改变APP中程序的字体大小,我们只能自定义字体或者使用runtime进行处理,或者都得设置UIFont,非常的不方便,从iOS 10苹果官方允许我们自定义设置

核心代码:

/*在iOS 10当中,当咱们用户将手机的字体大小进行了设置调整之后,那么app中设置相关代码字体也会跟着一起变化 ,支持常见一些字体UI控件 比如UILabel UIButton **/ 
[super viewDidLoad];  
//设置字体的改变大小 
self.labels.font =[UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 
//允许改变  
/* 苹果官方明确的告诉你必须和 preferredFontForTextStyle 或者preferredFontForTextStyle:(NSString *)style compatibleWithTraitCollection 进行结合使用 注意这里不支持模拟器操作 **/  
self.labels.adjustsFontForContentSizeCategory = YES;

4.UIViewPropertyAnimator属性动画器
那么在iOS 10之前,我们使用UIView 做动画效果或者自定义一些layer 的动画,如果开始了,一般无法进行停止操作更不能暂停操作,而且一些非常复杂的动画处理也比较麻烦,但是在iOS10,苹果退出了一个全新的API UIViewPropertyAnimator,可供我们处理动画操作UIViewPropertyAnimator 是 iOS 10 中新增的一个执行 View 动画的类,具有以下特点:

  • 可中断性
  • 可擦除
  • 可反转性
  • 丰富的动画时间控制功能

官方文档: Snip20160618_30.pngSnip20160618_31.png

核心代码:

#import "ViewController.h"
@interface ViewController ()

@property(nonatomic,strong)UIView *myView;
@property(nonatomic,strong)UIViewPropertyAnimator *myViewPro;
@end

@implementation ViewController
- (void)viewDidLoad { 
[super viewDidLoad];  
//1.创建一个View对象 
UIView *Views =[[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)]; 
Views.backgroundColor =[UIColor yellowColor]; [self.view addSubview:Views]; 
//2.创建一个外部的变量进行引用  
self.myView = Views;   
//3.创建一个view 动画器 
UIViewPropertyAnimator *viewPro =[UIViewPropertyAnimator runningPropertyAnimatorWithDuration:1.0 delay:30.0 options:UIViewAnimationOptionCurveLinear animations:^{ 

//使用View动画器修改View的frame 
self.myView.frame = CGRectMake(230, 230, 130, 130); } completion:nil]; 

self.myViewPro = viewPro;
}

//结束
- (IBAction)stop:(id)sender{  
// YES 和NO 适用于设置当前这个属性动画器是否可以继续使用 [self.myViewPro stopAnimation:YES];
}

//继续
- (IBAction)continued:(id)sender {   
//UITimingCurveProvider /** 
@property(nullable, nonatomic, readonly) UICubicTimingParameters *cubicTimingParameters; @property(nullable, nonatomic, readonly) UISpringTimingParameters *springTimingParameters;  **/ 
//设置弹簧效果 DampingRatio取值范围是 0-1  
//这个取值 决定弹簧抖动效果 的大小 ,越往 0 靠近那么就越明显
 UISpringTimingParameters *sp =[[UISpringTimingParameters alloc] initWithDampingRatio:0.01];  
//设置一个动画的效果// 
UICubicTimingParameters *cub =[[UICubicTimingParameters alloc] initWithAnimationCurve:UIViewAnimationCurveEaseInOut];
  //durationFactor 给一个默认值 1就可以 
[self.myViewPro continueAnimationWithTimingParameters:sp durationFactor:1.0];
}

//暂停
- (IBAction)puase:(id)sender {  
[self.myViewPro pauseAnimation];
}

//开始
- (IBAction)start:(id)sender {  
[self.myViewPro startAnimation];
}

效果图: 222.gif
5.UIColor 新增方法
在iOS10之前,UIColor中设置颜色只能通过RGB 来表示,在iOS原生还不支持#16进制写法,还得自己写分类去处理,我们知道RGB表示的颜色是优先的,而且也是不精准的,那么在iOS10中,苹果官方新增了colorWithDisplayP3Red方法。

核心代码:

+ (UIColor*)colorWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha NS_AVAILABLE_IOS(10_0);
//方法可以更加精准的设置颜色了。

6.UIApplication对象中openUrl被废弃
在iOS 10.0以前的年代,我们要想使用应用程序去打开一个网页或者进行跳转,直接使用[[UIApplication sharedApplication] openURL 方法就可以了,但是在iOS 10 已经被废弃了,因为使用这种方式,处理的结果我们不能拦截到也不能获取到,对于开发是非常不利的,在iOS 10全新的退出了
[[UIApplication sharedApplication] openURL:nil options:nilcompletionHandler: nil];有一个成功的回调block 可以进行监视。

苹果官方解释: //说明在iOS 10.0中openUrl方法已经废弃了 改为openURL:nil options:nil completionHandler:^(BOOL success /* // Options are specified in the section below for openURL options. An empty options dictionary will result in the same // behavior as the older openURL call, aside from the fact that this is asynchronous and calls the completion handler rather // than returning a result. // The completion handler is called on the main queue.

核心代码:

 [[UIApplication sharedApplication] openURL:nil options:nil completionHandler:^(BOOL success) {  
}];

6.SiriKit
在 iOS 10 里面开发者可以使用 Siri SDK,毫无疑问这也是 iOS 10 最重要的 SDK。从此开发者可以使用原生API提供语音搜索、语音转文字消息甚至更多常见语音功能。

7.User Notifications
这个 API 让你可以处理本地或远程的用户通知,并且可以基于某个条件,例如时间或者地理位置。这个异常强大,貌似可以拦截并替换自己 app 发下来的 payload,并且在之前版本SDK的本地通知框架已经被废弃了,在上一篇帖子有所讲到以及代码都有展示。

8.CallKit
继2014年苹果推出VoIP证书后,这次VoIP 接口的开放,以及一个全新的 App Extension,简直是VOIP的福音,可见苹果对VOIP的重视。callkit框架 VoIP应用程序集成与iPhone的用户界面,给用户一个很棒的经历。用这个框架来让用户查看和接听电话的锁屏和VoIP管理联系人电话在手机APP的收藏夹和历史的观点。callkit还介绍了应用程序的扩展,使呼叫阻塞和来电识别。您可以创建一个应用程序扩展,可以将一个电话号码与一个名称联系起来,或者告诉系统当一个号码应该被阻止。

9.第三方键盘的改进
非常非常重要,第三方键盘一直都不能很方便的拥有长按地球键的功能,现在有了。通过 handleInputModeListFromView:withEvent: 可以弹出系统键盘列表。同时使用 documentInputMode 可以检测输入上下文中的语言,你可以对输入方式进行一些类似于对齐方式的调整。

10.iOS10 对隐私权限的管理
比如访问的摄像头、麦克风等硬件,都需要提前请求应用权限、允许后才可以使用,或者现在要提前声明,虽然以往要求不严格。在iOS10中比如遇到崩溃。

崩溃日志:

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data. 你需要在info.plist文件 添加一个NSContactsUsageDescription的Key,Value添加一个描述。 ssss.png 视频播放 需要在info.Plist中配置 >官方解释:This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSAppleMusicUsageDescription key with a string value explaining to the user how the app uses this data.
访问用户的隐私数据,并且没有向用户说明,必须在plist(info.plist)中配置这个key NSAppleMusicUsageDescription 并且向用户说明.fff.png

11.Xcode7 和Xcode 8项目中的xib兼容问题
在Xcode8上打开项目要小心,尤其是对于xib过程,在变动后可不要随意点保存,否则当你回头用Xcode7打开时时发现报错了,Xcode保存的xib在xcode7上是识别不了的!

12.ApplePay(苹果支付)
ApplePay更新内容:

  • 可用于 SFSafariViewController
  • 可用于没有UI的extensions中
  • 在 iMessage 应用中也支持 ApplePay

13.CoreData提升了并发访问性能

14.刷新控件(UIRefresh Control)
iOS系统自带的刷新控件支持所有的 UIScrollView 以及其子类,比如说 UICollectionView,UITableView。

核心代码:

#import 
@class UIRefreshControl;NS_CLASS_AVAILABLE_IOS(10_0)
@protocol UIRefreshControlHosting 
@property (nonatomic, strong, nullable) UIRefreshControl *refreshControl __TVOS_PROHIBITED;
@end

Xcode 8 新特性(正在整理..)

xcode8 常见问题:

1.更新Xcode 8 卡在“正在安装...”:
笔者连续重装了三次都无限卡在“还有5分钟...”,最终采用最粗暴直接的方式,Finder --> 应用程序,卸载Xcode 7.3 并在AppStore中直接安装Xcode 8 。

2.控制台打印多余打印
貌似是xcode自带的项目追踪信息,每次项目运行以后都会在控制台打印以下信息


控制台多余打印.png



禁用该Xcode Debug Console方法:Product --> Scheme --> Edit Scheme 在打开的窗口中选择run 在 Environment Variables中添加OS_ACTIVITY_MODE对应的value为disable后close退出即可。


控制台环境变量.png


3.注释快捷键(command + /)失效
这个是因为为了苹果解决xcode ghost,把插件屏蔽了解决方法,命令运行:
$ sudo /usr/libexec/xpccachectl
注意:然后必须重启电脑后生效