2.插件的局限性WWDC14除发布了OS X v10.10和switf外,iOS8.0也开始变得更加开放了。说到开放,固然要数利用扩大(App Extension)了。顾名思义,利用扩大允许开发者扩大利用的自定义功能和内容,能够让用户在使用其他利用程序时使用该项功能,从而实现各个利用程序间的功能和资源同享。可以将扩大理解为1个轻量级(nimble and lightweight)的分身。
以下为常见的3类插件:
Target Type
Extension point identifier
Scenarios
Today Extension
com.apple.widget-extension
系统通知栏下拉显示
Share Extension
com.apple.share-services
Host App(照片、Safari、邮件、语音等)分享菜单第1行
Action Extension(UI)
com.apple.ui-services
Host App(照片、Safari、邮件、语音等)分享菜单第2行
下图为iPhone/iOS8中的【照片】分享:
例如【微信】最多支持分享9张(NSExtensionActivationSupportsImageWithMaxCount=9)照片给好友或到朋友圈。
以下文字节选自《App Extension Programming Guide》,主要罗列了插件的局限性,以知其可为不可为。
--------------------------------------------------------------------------------
1.Design a Streamlined UI
- An extension`s UI should be simple, restrained, and focused on facilitating a single task.
- To improve performance and the user`s experience, avoid including extraneous UI that doesn`t support your extension`s main task.
2.Optimize Efficiency and Performance
(1)App extensions should feel nimble and lightweight to users.
- Design your app extension to launch quickly, aiming for well under one second.
- An extension that launches too slowly is terminated by the system.
(2)Memory limits for running app extensions are significantly lower than the memory limits imposed on a foreground app.
- On both platforms, the system may aggressively terminate extensions because users want to return to their main goal in the host app.
- Some extensions may have lower memory limits than others.
(3)Your app extension doesn`t own the main run loop, so it`s crucial that you follow the established rules for good behavior in main runloops.
- For example, if your extension blocks the main runloop, it can create a bad user experience in another extension or app.
(4)Keep in mind that theGPUis a shared resource in the system.
- App extensions do not get top priority for shared resources; for example, a Today widget that runs a graphics-intensive game might give users a bad experience. The system is likely to terminate such an extension because of memory pressure.
- Functionality that makes heavy use of system resources is appropriate for an app, not an app extension.
--------------------------------------------------------------------------------
因而可知,iOS系统对插件要求简洁至上:UI启动要快、内存消耗要少、runloop履行耗时要短。
iOS系统对插件的限制决定了开发的插件必须轻量,发点Twitter/微博分享、小图片文件分享、URL跳转还是可以的;奢望丰富绚丽的UI或用来传大文件等大动作是不适合的。
固然,如果希望扩大(即便退出)履行长时间任务(比如上传/下载),可使用NSURLSession来创建1个上传/下载session,并初始化1个后台上传/下载任务。
注意:
Apple也限制了扩大在API使用方面的权限,在扩大中禁用的API原型声明被标上了NS_EXTENSION_UNAVAILABLE宏。例如:
+ (UIApplication*)sharedApplication NS_EXTENSION_UNAVAILABLE_IOS;
3.插件工作机制对sharedApplication的限制实际上就是不让插件直接获得访问宿主利用(Host App的UIApplication)对象。
4.插件与Containing App的App Group证书配置1.插件只能与Host App通过上下文直接通讯
2.插件可通过同享资源区与Containing App间接通讯
3.HostApp-Extension-Containing App工作流程
- Host App通过点击系统分享菜单中的插件图标调起扩大程序――Share/ActionExtension (*.appex)。
- iOS系统(Host App)通过扩大上下文(NSExtensionContext)向Share/ActionExtension传递欲分享的数据。
- Share/Action Extension提取数据并序列化到以AppGroup ID标识的同享资源区NSUserDefaults/AppGroup Container(containerURLForSecurityApplicationGroupIdentifier)中。
- Share/Action Extension通过URL Scheme呼起ContainingApp,同时插件通过上下文向iOS系统(HostApp)发出request completion通知,以便返回到Host App(iOS系统会dismiss插件UIViewController)。
- Containing App通过App Group ID从NSUserDefaults/containerURL中读取分享过来的数据,并对分享数据进行后续处理。
因而可知,扩大插件将Host App与Containing App勾结起来,而App Group Container则架起了数据交互的鹊桥。
这里需要注意的是,在iOS 8.0中,只有Today Extension才支持直接调用NSExtensionContext的openURL:completionHandler:打开URL链接;Share/Action Extension要想实现URL Scheme,只能创建1个Sink UIWebVew对URL进行loadRequest实现曲线救国(所谓“Sink”是指隐而不显,例如frame=CGRectZero)。
4.插件的UI形态
插件在UI上以UIViewController模式存在,被parentViewController(Host App)以模态窗口情势弹出(present as modal viewController)。
插件工程在Info.plist的NSExtension中通过NSExtensionMainStoryboard指定UI视图入口。固然,如果不想使用storyboard,也能够使用NSExtensionPrincipalClass指定自定义UIViewController子类名(也能够封装到UINavigationController)。
注意:
- 新建Extension Target后(Deployment Target≥8.0),需在Build Settings|Architectures|Valid Architectures中增加arm64!
- 初始安装Containing App时,扩大插件并未使能,需要到【更多】中打开开关。
参考:Containing App及其Extension是通过以App Group ID标识的同享资源区―App Group Container来实现数据同享的。
Containing App及其Extension的App ID必须是Explicit,且Extension App ID必须以Containing App ID为Prefix/Seed,并且配置到同1App Group下。
App Group会被配置到各自【Xcode Target|Build Settings|Code Signing|Code Signing Entitlements】的*.entitlements文件中的键com.apple.security.application-groups下。
证书和Provisioning Profile的申请和Code Signing配置流程同之前普通的App,详情请参考《iOS开发证书要点详解》。
《App Extension Programming Guide》/《App Extension编程指南》
《App Extensions for iOS 8 in Depth》
《iOS8 Extensions》《iOS8通知中心扩大制作入门》