简体   繁体   English

应用扩展程序和主应用之间的共享文件

[英]Shared file between app extension and main app

I need your help to figure out how to make my .json file accessible by both the main app and the app extension. 我需要您的帮助,以确定如何使主应用程序和应用程序扩展程序都可以访问我的.json文件。 I have an extension that, when the post button is pressed, writes some data in a .json file. 我有一个扩展名,当按下发布按钮时,它会将一些数据写入.json文件中。 But when I try to read the file in the main app, the main app reads a blank file in a wrong path, in fact the app reads in this position: https://i.stack.imgur.com/idP3d.png and the extension writes a file in this path: https://i.stack.imgur.com/v8yhr.png . 但是,当我尝试在主应用程序中读取文件时,主应用程序以错误的路径读取了空白文件,实际上,该应用程序的读取位置为: https : //i.stack.imgur.com/idP3d.png和扩展名将在以下路径中写入文件: https : //i.stack.imgur.com/v8yhr.png I have tried to include the file in the bundle resources and set the membership of the file for both the apps, but nothing worked. 我试图将文件包含在捆绑资源中,并为两个应用程序设置文件的成员身份,但是没有任何效果。 To get the path of the files I use this method: 要获取文件的路径,请使用以下方法:

let file = Bundle.main.url(forResource: "commonfile", withExtension: "json")

You help is really appreciated 非常感谢您的帮助

You can use App Group feature to share file between apps/extensions: 您可以使用“应用程序组”功能在应用程序/扩展名之间共享文件:

  1. Activate App Group for both targets: Project Settings -> Capabilities -> Activate App Group -> add new App Groups ID (usually using group.$(YOUR_APP_BUNDLE_ID)) 为两个目标激活应用程序组:项目设置->功能->激活应用程序组->添加新的应用程序组ID(通常使用group。$(YOUR_APP_BUNDLE_ID))
  2. Files/Folders located in this url will be access by both targets: appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "YOUR_GROUP_BUNDLE_ID") 这两个目标都可以访问此url中的文件/文件夹: appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "YOUR_GROUP_BUNDLE_ID")

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 主应用程序和共享扩展名之间的iOS共享.h文件 - iOS Sharing .h file between main app and share extension 应用程序和扩展程序之间共享的单例类 - singleton class shared between app and extension 应用和扩展程序之间共享的UserDefaults无法正常工作 - Shared UserDefaults between app and extension not working correctly IOS应用程序和手表扩展之间的共享文件 - Shared files between IOS app and watch extension iOS Couchbase:应用程序和应用程序扩展之间共享的CBL管理器 - iOS Couchbase: Shared CBL manager between app and app extension 在iOS Extension和app之间使用UIApplication.shared共享CocoaPod - Sharing CocoaPod using UIApplication.shared between iOS Extension and app 在Swift中的主应用程序和Today小部件/扩展之间共享UI元素 - Sharing UI elements between main App and Today widget/extension in Swift 在通知服务扩展和主应用之间共享数据 - Sharing data between an notification service extension and main app 在ios主应用程序和扩展之间共享动态框架 - Sharing dynamic frameworks between ios main app and extension 在 iOS 8 共享扩展和主应用程序之间共享数据 - Sharing data between an iOS 8 share extension and main app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM