Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I share data between iOS app and today view extension

Tags:

xcode

ios

swift

I'm trying to show the persons name in the today view extension; but I can't seem to do this. I've watched so many YouTube video's but they never helped. So if you can, I want you to answer this.

How the app works: You type your name inside the app. And the it will show in the today view extension.

About the app: I have a text field and button. The textfield is the persons name and the button is the save the name.

I want to show the name in the today extension.

Thank you.

like image 329
Wonixer A Avatar asked Dec 10 '25 20:12

Wonixer A


1 Answers

Add the group to the entitlements/capabilities.

  1. Go to the capabilities tab of the app's target
  2. Enable App Groups
  3. Create a new app group, entitled something appropriate. It must start with group..
  4. Let Xcode go through the process of creating this group for you.

Save data to NSUserDefaults with group ID and use it in your extension.

From Apple's App Extension Guide :

https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

In the main app, save person name:

let defaults = UserDefaults(suiteName: "your group ID")
defaults!.set("person name", forKey: "key for person name")
defaults!.synchronize()

In the extension, you can use saved person name:

let defaults = UserDefaults(suiteName: "your group ID")
let savedPersonName = defaults!.string(forKey: "key for person name")

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!