简体   繁体   English

iOS ShareExtension 应用程序中的数据共享问题

[英]Data sharing issue in iOS ShareExtension app

I am working on a ShareExtension app.我正在开发 ShareExtension 应用程序。
I want to share data from Container App to ShareExtension app.我想将数据从容器应用程序共享到 ShareExtension 应用程序。

Following is the code I am using In Container App View Controller:以下是我在容器应用程序视图控制器中使用的代码:

let userDefaultx = NSUserDefaults(suiteName: "group.myapps.userConnect")
userDefaultx!.setObject("new user", forKey:"user")
userDefaultx!.synchronize()

Now I am trying to capture the data in ShareExtension View Controller:现在我试图在 ShareExtension View Controller 中捕获数据:

let userDefaults = NSUserDefaults(suiteName: "group.myapps.userConnect")
let user = userDefaults?.valueForKey("user")
print(" user \(user) ") // returns nil

I am using simulator and I have not used the default SLComposeServiceViewController .我正在使用模拟器,但我没有使用默认的SLComposeServiceViewController
Instead, I have used regular UIViewController.相反,我使用了常规的 UIViewController。

What could be the issue I am missing?我想念的问题可能是什么?

Follow below steps to successfully integrate share extension with your app,按照以下步骤成功地将共享扩展与您的应用程序集成,

  1. Add New target in your application在您的应用程序中添加新目标

在此处输入图片说明

  1. Select Share extension -> Click next选择共享扩展-> 点击下一步

在此处输入图片说明

  1. You will receive one pop up alert -> Click on Activate button您将收到一个弹出警报 -> 单击“激活”按钮

在此处输入图片说明

  1. Check your app's bundle identifier检查您的应用程序包标识符

在此处输入图片说明

  1. Check your extension's bundle identifier检查您的扩展程序包标识符

在此处输入图片说明

  1. Login to your Apple Developer Portal -> Click on Certificates, Identifiers & Profiles section -> Select App Groups under Identifiers sections -> Click on Add New App Group登录您的 Apple Developer Portal -> 点击Certificates, Identifiers & Profiles部分 -> 在 Identifiers 部分下选择App Groups -> 点击 Add New App Group

在此处输入图片说明

  1. Enter your App Group Name -> Enter it's identifier which should start with the word group .输入您的应用程序组名称 -> 输入它的标识符,该标识符应以group一词开头。

在此处输入图片说明

  1. Create New App ID for your application为您的应用程序创建新的App ID

在此处输入图片说明

  1. Enter App ID name and your app's bundle identifier.输入 App ID 名称和您的应用程序包标识符。

在此处输入图片说明

  1. Enable App Group -> Click Done启用应用程序组-> 单击完成

在此处输入图片说明

  1. Now select the App Id which you just created -> Click Edit现在选择您刚刚创建的应用程序 ID -> 单击编辑

在此处输入图片说明

  1. Click Edit just next to App Group单击应用程序组旁边的编辑

在此处输入图片说明

  1. Select the App Group which you create in Step 7 -> Click Done选择您在第 7 步中创建的应用程序组 -> 单击完成

  2. Generate Provisioning Profile with the App ID which you created and use that in your project使用您创建的 App ID 生成配置文件并在您的项目中使用它

  3. Create New App ID for your application's extension为您的应用程序扩展创建新的App ID

在此处输入图片说明

  1. Enter App ID name and your app's extension's bundle identifier.输入 App ID 名称和您应用的扩展程序包标识符。

在此处输入图片说明

  1. Follow Step no.10 to Step no.14 again再次按照第 10 步到第 14 步

  2. You must be generated two(development/distribution) profiles for your App as well as for your extension您必须为您的应用程序和扩展程序生成两个(开发/分发)配置文件

  3. Here I am showing one example of generating development profile for your application -> Select Development under Provisioning Profiles -> Click Add这里我展示了一个为您的应用程序生成开发配置文件的示例 -> 在 Provisioning Profiles 下选择 Development -> 单击 Add

在此处输入图片说明

  1. Select iOS App Development -> Click Continue选择iOS App Development -> 点击Continue

在此处输入图片说明

  1. Select the App ID which you created for your app in Step 10 -> Click Continue -> Name it -> Download it -> Install it选择您在第 10 步中为应用创建的应用 ID -> 单击继续 -> 命名 -> 下载 -> 安装

在此处输入图片说明

  1. Set that profile in you project target在您的项目目标中设置该配置文件

在此处输入图片说明

  1. Follow Step 19 to 22 for generating development provisioning profile for your extension.按照步骤 19 到 22 为您的扩展生成开发配置文件。 Here select the App ID which you created for your extension.在此处选择您为扩展程序创建的 App ID。 you need to follow these same steps for generating your distribution provisioning profile when you need to live your application on App Store.当您需要在 App Store 上运行您的应用程序时,您需要按照这些相同的步骤来生成您的分发配置文件。

  2. Set All Development/Distribution Profiles in your project在您的项目中设置所有开发/分发配置文件

  3. Open your project -> Select app target -> Capabilities -> Enable App Group -> Select the App Group which you created in Step 7打开您的项目 -> 选择应用程序目标 -> 功能 -> 启用应用程序组 -> 选择您在第 7 步中创建的应用程序组

在此处输入图片说明

  1. Select Extension -> Capabilities -> Enable App Group -> Select the App Group which you created in Step 7选择扩展 -> 功能 -> 启用应用程序组 -> 选择您在步骤 7 中创建的应用程序组

Now if you want to share data between your App and your extension, use below code,现在,如果您想在应用程序和扩展程序之间共享数据,请使用以下代码,

In your App,在您的应用程序中,

let defaults = UserDefaults(suiteName: "group.com.myapp") // Your App Group name
defaults?.set(str_user_token, forKey:"user_token")
defaults?.synchronize()

In your Share Extension File, access it using below code,在您的共享扩展文件中,使用以下代码访问它,

let defaults = UserDefaults(suiteName: "group.com.myapp") // Your App Group name
let restoredValue = defaults!.string(forKey: "user_token")

You are All set for Share Extension!您已准备好共享扩展! Happy Coding.快乐编码。 Cheers!干杯!

UPDATE:更新:

Find the Updated answer here with latest screenshots and every small detail to integrate Share Extension successfully in your iOS Application: https://igati.tech/ios-share-extension/在此处找到更新的答案,其中包含最新的屏幕截图和每个小细节,以便在您的 iOS 应用程序中成功集成共享扩展: https : //igati.tech/ios-share-extension/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM