Wonixer A
Wonixer A

Reputation: 126

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

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.

Upvotes: 3

Views: 3300

Answers (3)

HelloimDarius
HelloimDarius

Reputation: 695

Just a quick notice as I too have missed it

In order to get it working create the group on app target and then on Today's target add Group Capability as well and tick the one just created from the main app's target (it should be listed)

Upvotes: 1

user13357194
user13357194

Reputation:

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")

Upvotes: 3

Muhammad Ahmad
Muhammad Ahmad

Reputation: 398

here is a simple example of today extension in this example, I am only showing and updating the user name enter image description here

this my today extension storyboard image and today-view-Controller code is:

import UIKit
import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding {

    @IBOutlet weak var lnameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(_ animated: Bool) {
        if let name = UserDefaults.init(suiteName: "group.com.ahmad.widget")?.value(forKey: "name"){

            lnameLabel.text = name as? String
        }
        else{
            lnameLabel.text = "Wait..."
        }
    }

    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
        // Perform any setup necessary in order to update the view.

        // If an error is encountered, use NCUpdateResult.Failed
        // If there's no update required, use NCUpdateResult.NoData
        // If there's an update, use NCUpdateResult.NewData

        completionHandler(NCUpdateResult.newData)
    }

}

I create a storyboard and add a button in this than on his button action I update the user name on today extension

code of viewController is :

class ViewController: UIViewController {
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var nameTextfield: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }



    @IBAction func SetNameOnWigetAction(_ sender: Any) {

        nameLabel.text = nameTextfield.text
        UserDefaults.init(suiteName: "group.com.ahmad.widget")?.setValue(nameTextfield.text, forKey: "name")
    }

}

Upvotes: 0

Related Questions