iOS开发小技巧

iOS App Extension 开发

2016-12-07  本文已影响291人  RogerHXJ

1.什么是Extension?

扩展 (Extension) 是 iOS 8 和 OSX 10.10 加入的一个非常大的功能点,开发者可以通过系统提供给我们的扩展接入点 (Extension point) 来为系统特定的服务提供某些附加的功能。

例如1:安装了同花顺应用后,打开手机的Widget界面,我们可以看到同花顺提供的自选股实时行情,这个功能就是一个Extension,称为Today Extension。

同花顺提供的Extension
例如2: 安装了搜狗输入法之后,在手机-设置-键盘-添加新键盘后,就可以用搜狗输入法替代原生输入法,这是Custom Keyboard Extension。

目前Xcode8.1提供的所有的扩展类型如下:

Xcode8.1所有可用扩展类型

2.Extension 存在的形式

3. Extension 生命周期详解

Extension 不是一个app,所以生命周期和运行环境和app不同。在多数情况下,extension是由用户在某一个app的界面或者某一个活动的控制器中启动,这个启动extension的app被称为host app。Host app 提供extension运行所需的上下文并通过发送一个request的方式开启extension的生命周期,extension在完成host app请求的任务后结束运行。

The basic life cycle of an app extension

苹果开发者文档中原文如下:

Because an app extension is not an app, its life cycle and environment are different. In most cases, an extension launches when a user chooses it from an app’s UI or from a presented activity view controller. An app that a user employs to choose an app extension is called a host app. A host app defines the context provided to the extension and kicks off the extension life cycle when it sends a request in response to a user action. An extension typically terminates soon after it completes the request it received from the host app.

4.Extension的通信

@interface UIViewController(NSExtensionAdditions)  

// Returns the extension context. Also acts as a convenience method for a view controller to check if it participating in an extension request. 
@property (nonatomic,readonly,retain) NSExtensionContext *extensionContext NS_AVAILABLE_IOS(8_0); 
 
@end 

A Today widget (and no other app extension type) can ask the system to open its containing app by calling the openURL:completionHandler: method of the NSExtensionContext class.

An app extension can communicate indirectly with its containing app
上一篇 下一篇

猜你喜欢

热点阅读