Demo1_iOS9网络适配_改用更安全的HTTPS
iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL。采用TLS 1.2 协议,目的是 强制增强数据访问安全,而且 系统 Foundation 框架下的相关网络请求,将不再默认使用 Http 等不安全的网络协议,而默认采用 TLS 1.2。服务器因此需要更新,以解析相关数据。如不更新,可通过在 Info.plist 中声明,倒退回不安全的网络请求。
在讨论之前,跟往常一样,先说下iOS程序猿们最关心的问题:
跟我有毛关系?需要我加班吗?!
首先咱们来看下业内对Apple这一做法的评论:

这是某社交App上讨论,看来业内还是吐槽声和肯定声同在。
结论是:
跟你很有关系,加班吧,少年!
书归正传【严肃脸】,我们正式讨论下 WHAT,WHY,HOW:
WHAT(什么是SSL/TLS?跟HTTP和HTTPS有什么关系)
WHY(以前的HTTP不是也能用吗?为什么要用SSL/TLS,闲得慌?!Apple是不是又在反人类?)
HOW(如何适配?---弱弱地问下:加班要多久?)
WHAT(什么是SSL/TLS?跟HTTP和HTTPS有什么关系)
跟往常一样,先说结论:
1
HTTP+SSL/TLS+TCP = HTTPS
TLS 是 SSL 新的别称。举个例子:
“TLS1.0”之于“SSL3.1”,犹“公元2015”之于“民国104”,或者是“一千克”之于“一公斤”,或者是“半斤”之于“八两”:称呼不同,但意思相同。
SSL 3.0版本之后的迭代版本被重新命名为TLS 1.0,
也就是说:
1
TLS 1.0 = SSL 3.1
所以他们是一个东西,我们平常也经常简单见到 “SSL/TLS” 这种说法。
常用的是下面这些:
SSL 2.0
SSL 3.0
TLS 1.0 (SSL 3.1)
TLS 1.1 (SSL 3.1)
TLS 1.2 (SSL 3.1)
那为什么标题是“使用HTTPS”而没有提及SSL和TLS什么事? 要理解这个,要看下一个公式:
1
HTTP+SSL/TLS+TCP = HTTPS

打个比方:如果原来的 HTTP 是塑料水管,容易被戳破;那么如今新设计的 HTTPS 就像是在原有的塑料水管之外,再包一层金属水管。一来,原有的塑料水管照样运行;二来,用金属加固了之后,不容易被戳破。
目前,应用最广泛的是TLS 1.0,接下来是SSL 3.0。但是,主流浏览器都已经实现了TLS 1.2的支持。
Apple让你的HTTP采用SSL/TLS协议,就是让你从HTTP转到HTTPS
WHY(以前的HTTP不是也能用吗?为什么要用SSL/TLS,闲得慌?!Apple是不是又在反人类?)
不使用SSL/TLS的HTTP通信,就是不加密的通信!
所有信息明文传播,带来了三大风险:
窃听风险(eavesdropping):第三方可以获知通信内容。
篡改风险(tampering):第三方可以修改通信内容。
冒充风险(pretending):第三方可以冒充他人身份参与通信。
SSL/TLS协议是为了解决这三大风险而设计的,希望达到:
所有信息都是加密传播,第三方无法窃听。
具有校验机制,一旦被篡改,通信双方会立刻发现。
配备身份证书,防止身份被冒充。
HOW(如何适配?---弱弱地问下:加班要多久?)
正如文章开头所说:
TLS 1.2 协议 强制增强数据访问安全 系统 Foundation 框架下的相关网络请求,将不再默认使用 Http 等不安全的网络协议,而默认采用 TLS 1.2。服务器因此需要更新,以解析相关数据。如不更新,可通过在 Info.plist 中声明,倒退回不安全的网络请求。
方案一:立即让公司的服务端升级使用TLS 1.2
方案二:虽Apple不建议,但可通过在 Info.plist 中声明,倒退回不安全的网络请求依然能让App访问指定http,甚至任意的http,
具体做法见gif图,示例Demo见 Demo1

正如 Apple官方文档 所说 :



Info.plist 配置中的XML源码如下所示:

上面是比较严谨的做法,指定了能访问哪些特定的HTTP。当然也有暴力的做法: 彻底倒退回不安全的HTTP网络请求,能任意进行HTTP请求,比如你在开发一款浏览器App,或者你想偷懒,或者后台想偷懒,或者公司不给你升级服务器。。。

声明:目前Apple的官方文档并未提及如何在 Info.plist 配置,我将密切关注官方文档,如有提及,再来更新本文 .
Demo2_iOS9新特性_更灵活的后台定位

Demo:GitHub地址
【iOS9在定位的问题上,有一个坏消息一个好消息】坏消息:如果不适配iOS9,就不能偷偷在后台定位(不带蓝条,见图)!好消息:将允许出现这种场景:同一App中的多个location manager:一些只能在前台定位,另一些可在后台定位,并可随时开启或者关闭特定location manager的后台定位。
如果没有请求后台定位的权限,也是可以在后台定位的,不过会带蓝条:

如何偷偷在后台定位:请求后台定位权限:
12345678910111213141516171819
// 1. 实例化定位管理器
_locationManager = [[CLLocationManager alloc] init];
// 2. 设置代理
_locationManager.delegate = self;
// 3. 定位精度
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// 4.请求用户权限:分为:?只在前台开启定位?在后台也可定位,
//注意:建议只请求?和?中的一个,如果两个权限都需要,只请求?即可,
//??这样的顺序,将导致bug:第一次启动程序后,系统将只请求?的权限,?的权限系统不会请求,只会在下一次启动应用时请求?
if
([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
//[_locationManager requestWhenInUseAuthorization];//?只在前台开启定位
[_locationManager requestAlwaysAuthorization];
//?在后台也可定位
}
// 5.iOS9新特性:将允许出现这种场景:同一app中多个location manager:一些只能在前台定位,另一些可在后台定位(并可随时禁止其后台定位)。
if
([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
_locationManager.allowsBackgroundLocationUpdates = YES;
}
// 6. 更新用户位置
[_locationManager startUpdatingLocation];
但是如果照着这种方式尝试,而没有配置Info.plist,100%你的程序会崩溃掉,并报错:
1
*** Assertion failure
in
-[CLLocationManager setAllowsBackgroundLocationUpdates:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-1808.1.5/Framework/CoreLocation/CLLocationManager.m:593
要将 Info.plist 配置如下:

对应的 Info.plist 的XML源码是:

Bitcode(通俗解释:在线版安卓ART模式)
未来Watch应用须包含Bitcode,iOS不强制,但Xcode7默认会开启Bitcode。
如何适配?
方法一:更新library使包含Bitcode,否则会出现以下中的警告;
1
(
null
): URGENT: all bitcode will be dropped because
'/Users/myname/Library/Mobile Documents/com~apple~CloudDocs/foldername/appname/GoogleMobileAds.framework/GoogleMobileAds(GADSlot+AdEvents.o)'
was built without bitcode. You must rebuild it
with
bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode
for
this
target. Note: This will be an error
in
the future.
方法二:关闭Bitcode,方法见下图

更多信息,请移步 bitcode 苹果官方文档
,和 WWDC 2015 Session 102: "Platforms State of the Union"

企业级分发
iOS9之前,企业级分发十分方便:点击App出现“信任按钮”,

iOS9以后,企业级分发ipa包将遭到与Mac上dmg安装包一样的待遇:默认不能安装,也不再出现“信任按钮”

必须让用户进行gif图中的设置(相关Demo:https://github.com/ChenYilong/iOS9AdaptationTips/ )

URL scheme
在iOS9中,如果使用URL scheme必须在"Info.plist"中将你要在外部调用的URL scheme列为白名单,否则不能使用。key叫做LSApplicationQueriesSchemes ,键值内容是
1
LSApplicationQueriesSchemes urlscheme urlscheme2 urlscheme3 urlscheme4
推荐一篇博客: http://awkwardhare.com/post/121196006730/quick-take-on-ios-9-url-scheme-changes
其中最关键的是以下部分:
12
If you call the “canOpenURL” method on a URL that is not
in
your whitelist, it will
return
“NO”, even
if
there is an app installed that has registered to handle
this
scheme. A “This app is not allowed to query
for
scheme xxx” syslog entry will appear.
If you call the “openURL” method on a URL that is not
in
your whitelist, it will fail silently. A “This app is not allowed to query
for
scheme xxx” syslog entry will appear.
更多信息请移步:WWDC 2015 Session 703: "Privacy and Your App" 时间在30:18左右

iPad适配Slide Over 和 Split View

【iPad适配Slide Over 和 Split View】 若想适配multi tasking特性,唯一的建议:弃纯代码,改用storyboard、xib,纵观苹果WWDC所有Demo均是如此:
本文转自Cocoa China http://www.cocoachina.com/ios/20150703/12392.html
WHAT(什么是SSL/TLS?跟HTTP和HTTPS有什么关系)
WHY(以前的HTTP不是也能用吗?为什么要用SSL/TLS,闲得慌?!Apple是不是又在反人类?)
HOW(如何适配?---弱弱地问下:加班要多久?)
HTTP+SSL/TLS+TCP = HTTPS
TLS 1.0 = SSL 3.1
SSL 2.0
SSL 3.0
TLS 1.0 (SSL 3.1)
TLS 1.1 (SSL 3.1)
TLS 1.2 (SSL 3.1)
HTTP+SSL/TLS+TCP = HTTPS
窃听风险(eavesdropping):第三方可以获知通信内容。
篡改风险(tampering):第三方可以修改通信内容。
冒充风险(pretending):第三方可以冒充他人身份参与通信。
所有信息都是加密传播,第三方无法窃听。
具有校验机制,一旦被篡改,通信双方会立刻发现。
配备身份证书,防止身份被冒充。
// 1. 实例化定位管理器
_locationManager = [[CLLocationManager alloc] init];
// 2. 设置代理
_locationManager.delegate = self;
// 3. 定位精度
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// 4.请求用户权限:分为:?只在前台开启定位?在后台也可定位,
//注意:建议只请求?和?中的一个,如果两个权限都需要,只请求?即可,
//??这样的顺序,将导致bug:第一次启动程序后,系统将只请求?的权限,?的权限系统不会请求,只会在下一次启动应用时请求?
if
([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
//[_locationManager requestWhenInUseAuthorization];//?只在前台开启定位
[_locationManager requestAlwaysAuthorization];
//?在后台也可定位
}
// 5.iOS9新特性:将允许出现这种场景:同一app中多个location manager:一些只能在前台定位,另一些可在后台定位(并可随时禁止其后台定位)。
if
([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
_locationManager.allowsBackgroundLocationUpdates = YES;
}
// 6. 更新用户位置
[_locationManager startUpdatingLocation];
*** Assertion failure
in
-[CLLocationManager setAllowsBackgroundLocationUpdates:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-1808.1.5/Framework/CoreLocation/CLLocationManager.m:593
(
null
): URGENT: all bitcode will be dropped because
'/Users/myname/Library/Mobile Documents/com~apple~CloudDocs/foldername/appname/GoogleMobileAds.framework/GoogleMobileAds(GADSlot+AdEvents.o)'
was built without bitcode. You must rebuild it
with
bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode
for
this
target. Note: This will be an error
in
the future.
LSApplicationQueriesSchemes urlscheme urlscheme2 urlscheme3 urlscheme4
If you call the “canOpenURL” method on a URL that is not
in
your whitelist, it will
return
“NO”, even
if
there is an app installed that has registered to handle
this
scheme. A “This app is not allowed to query
for
scheme xxx” syslog entry will appear.
If you call the “openURL” method on a URL that is not
in
your whitelist, it will fail silently. A “This app is not allowed to query
for
scheme xxx” syslog entry will appear.