Skip to main content

应用扩展

应用扩展允许你在主应用之外提供自定义功能和内容。iOS 上有不同类型的应用扩展,应用扩展编程指南 中都涵盖了它们。在本指南中,我们将简要介绍如何利用 iOS 上的应用扩展。

¥App extensions let you provide custom functionality and content outside of your main app. There are different types of app extensions on iOS, and they are all covered in the App Extension Programming Guide. In this guide, we'll briefly cover how you may take advantage of app extensions on iOS.

扩展中的内存使用

¥Memory use in extensions

由于这些扩展是在常规应用沙箱之外加载的,因此很可能会同时加载其中几个应用扩展。正如你所料,这些扩展的内存使用限制较小。开发应用扩展时请记住这些。始终强烈建议在实际设备上测试你的应用,在开发应用扩展时更是如此:开发者经常发现他们的扩展在 iOS 模拟器中运行良好,却收到用户报告说他们的扩展未在实际设备上加载。

¥As these extensions are loaded outside of the regular app sandbox, it's highly likely that several of these app extensions will be loaded simultaneously. As you might expect, these extensions have small memory usage limits. Keep these in mind when developing your app extensions. It's always highly recommended to test your application on an actual device, and more so when developing app extensions: too frequently, developers find that their extension works fine in the iOS Simulator, only to get user reports that their extension is not loading on actual devices.

我们强烈建议你监视 Conrad Kramer 在 扩展中的内存使用 上的演讲,以了解有关此主题的更多信息。

¥We highly recommend that you watch Conrad Kramer's talk on Memory Use in Extensions to learn more about this topic.

今日小部件

¥Today widget

Today 小部件的内存限制为 16 MB。碰巧的是,使用 React Native 的 Today 小部件实现可能工作不可靠,因为内存使用率往往太高。如果你的 Today 小部件生成消息 '无法加载',你就可以判断它是否超出了内存限制:

¥The memory limit of a Today widget is 16 MB. As it happens, Today widget implementations using React Native may work unreliably because the memory usage tends to be too high. You can tell if your Today widget is exceeding the memory limit if it yields the message 'Unable to Load':

始终确保在真实设备中测试你的应用扩展,但请注意这可能还不够,尤其是在处理“今日”小部件时。调试配置的构建更有可能超出内存限制,而发布配置的构建不会立即失败。我们强烈建议你使用 Xcode 的仪器 来分析你的实际内存使用情况,因为你的发布配置构建很可能非常接近 16 MB 限制。在此类情况下,你可以通过执行常见操作(例如从 API 获取数据)来快速超过 16 MB 限制。

¥Always make sure to test your app extensions in a real device, but be aware that this may not be sufficient, especially when dealing with Today widgets. Debug-configured builds are more likely to exceed the memory limits, while release-configured builds don't fail right away. We highly recommend that you use Xcode's Instruments to analyze your real world memory usage, as it's very likely that your release-configured build is very close to the 16 MB limit. In situations like these, you can quickly go over the 16 MB limit by performing common operations, such as fetching data from an API.

要试验 React Native Today 小部件实现的限制,请尝试扩展 react-native-today-widget 中的示例项目。

¥To experiment with the limits of React Native Today widget implementations, try extending the example project in react-native-today-widget.

其他应用扩展

¥Other app extensions

其他类型的应用扩展具有比“今日”小组件更大的内存限制。例如,自定义键盘扩展限制为 48 MB,共享扩展限制为 120 MB。使用 React Native 实现此类应用扩展更为可行。概念验证示例之一是 react-native-ios-share-extension

¥Other types of app extensions have greater memory limits than the Today widget. For instance, Custom Keyboard extensions are limited to 48 MB, and Share extensions are limited to 120 MB. Implementing such app extensions with React Native is more viable. One proof of concept example is react-native-ios-share-extension.