gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:03:22 31 4
gpt4 key购买 nike

我正在开发 ShareExtension 应用程序。
我想将数据从容器应用程序共享到 ShareExtension 应用程序。

以下是我在容器应用程序 View Controller 中使用的代码:

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

现在我正尝试在 ShareExtension View Controller 中捕获数据:

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

我正在使用模拟器,我没有使用默认的 SLComposeServiceViewController
相反,我使用了常规的 UIViewController。

我可能遗漏了什么问题?

最佳答案

按照以下步骤将共享扩展成功集成到您的应用中,

  1. Add New target in your application

enter image description here

  1. Select Share extension -> Click next

enter image description here

  1. You will receive one pop up alert -> Click on Activate button

enter image description here

  1. Check your app's bundle identifier

enter image description here

  1. Check your extension's bundle identifier

enter image description here

  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

enter image description here

  1. Enter your App Group Name -> Enter it's identifier which should start with the word group.

enter image description here

  1. Create New App ID for your application

enter image description here

  1. Enter App ID name and your app's bundle identifier.

enter image description here

  1. Enable App Group -> Click Done

enter image description here

  1. Now select the App Id which you just created -> Click Edit

enter image description here

  1. Click Edit just next to App Group

enter image description here

  1. Select the App Group which you create in Step 7 -> Click Done

  2. Generate Provisioning Profile with the App ID which you created and use that in your project

  3. Create New App ID for your application's extension

enter image description here

  1. Enter App ID name and your app's extension's bundle identifier.

enter image description here

  1. Follow Step no.10 to Step no.14 again

  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

enter image description here

  1. Select iOS App Development -> Click Continue

enter image description here

  1. Select the App ID which you created for your app in Step 10 -> Click Continue -> Name it -> Download it -> Install it

enter image description here

  1. Set that profile in you project target

enter image description here

  1. Follow Step 19 to 22 for generating development provisioning profile for your extension. Here select the App ID which you created for your extension. you need to follow these same steps for generating your distribution provisioning profile when you need to live your application on 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

enter image description here

  1. Select Extension -> Capabilities -> Enable App Group -> Select the App Group which you created in Step 7

现在如果你想在你的应用程序和你的扩展之间共享数据,使用下面的代码,

在您的应用中,

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

在您的共享扩展文件中,使用以下代码访问它,

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

您已准备好共享扩展!快乐编码。干杯!

UPDATE:

在此处找到包含最新屏幕截图和每个小细节的更新答案,以便在您的 iOS 应用程序中成功集成 Share Extension:https://igati.tech/ios-share-extension/

关于ios - iOS ShareExtension 应用程序中的数据共享问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824360/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com