我正在开发 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。
我可能遗漏了什么问题?
最佳答案
按照以下步骤将共享扩展成功集成到您的应用中,
- Add New target in your application
- Select Share extension -> Click next
- You will receive one pop up alert -> Click on Activate button
- Check your app's bundle identifier
- Check your extension's bundle identifier
- 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 your App Group Name -> Enter it's identifier which should start with the word group.
- Create New App ID for your application
- Enter App ID name and your app's bundle identifier.
- Enable App Group -> Click Done
- Now select the App Id which you just created -> Click Edit
- Click Edit just next to App Group
Select the App Group which you create in Step 7 -> Click Done
Generate Provisioning Profile with the App ID which you created and use that in your project
Create New App ID for your application's extension
- Enter App ID name and your app's extension's bundle identifier.
Follow Step no.10 to Step no.14 again
You must be generated two(development/distribution) profiles for your App as well as for your extension
Here I am showing one example of generating development profile for your application -> Select Development under Provisioning Profiles -> Click Add
- Select iOS App Development -> Click Continue
- Select the App ID which you created for your app in Step 10 -> Click Continue -> Name it -> Download it -> Install it
- Set that profile in you project target
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.
Set All Development/Distribution Profiles in your project
Open your project -> Select app target -> Capabilities -> Enable App Group -> Select the App Group which you created in Step 7
- 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/