App Extension(App 扩展)

苹果官方文档:App Extension Programming Guide

1.Extension(扩展)

  • Extension(扩展)是iOS8引入的一个很重要的新特性。用户可以在其他App中使用自己应用的功能而且不需要离开当前App。例如:在相册中想要分享一张图片到微信的朋友圈中,只要点击分享按钮,选择微信就可以完成,而不是离开当前相册进入微信再进行图片的分享。 iOS11中一共有9种扩展类别:Action、Audio Unit、Content Blocker、Custom Keyboard、Custom Keyboard、Document Provider、Finder Sync、Photo Editing、Share、Today;详情请看尾部官方文档)

    每一种扩展生成的文件和API都是不一样的要慎重选择,一个扩展生成一个扩展文件,一个容器App(Containing App)可以有多个扩展;官方提示:

  • Extension(扩展)是一个独立的二进制文件,独立于容器App(Containing App)运行的。发布扩展时是提交包含扩展的容器App(Containing App)。当用户安装App时也会安装App包含的Extension。
  • Extension不是一个App,所以生命周期也不同于App的生命周期。官方示意图:
  • Extension和宿主App(Host App)和容器App(Containing App)官方通信关系图如下:(Extension和Containing App没有直接的通信;一般来说Extension运行时Containing App甚至不运行;而Host App和Containing App从头到尾根本不会通信)三者完整官方通信图:

    • Open URL 即为Extension唤醒Containing App

      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. // 暂时只有Today widget扩展类型才可以使用这个方法

    • Read/Write Extension和Containing App 之间的数据共享是通过APP Groups实现的

      As indicated by the Read/Write arrows in Figure 2-3, any app extension and its containing app can access shared data in a privately defined shared container. // 任何扩展都可以

  • 有一些API不能在Extension中使用

    • Access a sharedApplication object, and so cannot use any of the methods on that object
    • Use any API marked in header files with the NS_EXTENSION_UNAVAILABLE macro, or similar unavailability macro, or any API in an unavailable framework(有NS_EXTENSION_UNAVAILABLE符号的都不能用)
    • Access the camera or microphone on an iOS device (an iMessage app, unlike other app extensions, does have access to these resources, as long as it correctly configures the NSCameraUsageDescription and NSMicrophoneUsageDescription Info.plist keys)(想要访问相册之类的只要配置好,如果Containing App已配置好其Extension可以不用配置了)
    • Perform long-running background tasks The specifics of this limitation vary by platform, as described in the extension point chapters in this document. (An app extension can initiate uploads or downloads using an NSURLSession object, with results of those operations reported to the containing app.)(扩展可用NSURLSessoin上传下载数据)
    • Receive data using AirDrop (An app extension can send data using AirDrop in the same way an app does: by employing the UIActivityViewController class.)(扩展可以像应用程序一样使用AirDrop发送数据:使用UIActivityViewController类。)

results matching ""

    No results matching ""