14

I have lately been trying to make a widget and wanted to share a piece of data between my widget and my app.

    static var sharedDataFileURL: URL {
        let appGroupIdentifier = "group.com.unknownstudios.yk"
        guard let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)
            else { preconditionFailure("Expected a valid app group container") }
        return url.appendingPathComponent("userID.plist")
    }

The above code works fine on my app, but when I run it on my widget it gives me the following output. I have checked that the app group is correct and is active on both the iOS app and the widget.

[unspecified] container_create_or_lookup_path_for_platform: client is not entitled
[unspecified] container_create_or_lookup_app_group_path_by_app_group_identifier: client is not entitled
Fatal error: Expected a valid app group container: file WidgetExtension/Library.swift, line 143

Edit:

I have also tried to use UserDefaults, but they don't work either.

The following is the way I use the the FileManager and UserDefaults

UserDefaults(suiteName: "group.com.unknownstudios.yk")!.set("**USERID**", forKey: "userID")

let data = Data("**USERID**".utf8)
do {
    try data.write(to: URL.sharedDataFileURL, options: .atomic)
} catch {
    print(error.localizedDescription)
}

And the following is how I try to read the data from the widget:

    var userID: String? = {
        if let userid = UserDefaults(suiteName: "group.com.unknownstudios.yk")!.string(forKey: "userID") {
            print(userid)
            return userid
        } else if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.unknownstudios.yk") {
            if let data = try? Data(contentsOf: url.appendingPathComponent("userID.plist")), let string = String(data: data, encoding: .utf8) {
                return string
            }
        }
        return nil
    }()

WidgetExtension app groups: enter image description here

Main app, app groups: enter image description here

2 Answers 2

23

I found out what my problem was. I had accidentally selected the release tab under Signing & Capabilities. Which caused my app group to be wrong when checking on release, thus making the error occur. Image of project bar I just had to select All and re-add the app groups capability which made it work again.

Sign up to request clarification or add additional context in comments.

2 Comments

You saved my day. May be it's a bug in xcode to get Release selected somehow.
It's probably a bug, i clicked on 'All' and it still created group only for release, i had to make one for 'Debug' separately!
0

You may also want to double-check for typos. $(TeamIdentifierPrefix) includes the trailing ..

I had:

// My bundle includes an entry for $(TeamIdentifierPrefix)
// See https://stackoverflow.com/a/28714850/788168
let teamIdentifierPrefix = Bundle.main.object(forInfoDictionaryKey: "TeamIdentifierPrefix") as! String
location = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.\(teamIdentifierPrefix).com.myteam.myapp")!
            .appendingPathComponent("db.sqlite")
            .path

It needed to be:

// My bundle includes an entry for $(TeamIdentifierPrefix)
// See https://stackoverflow.com/a/28714850/788168
let teamIdentifierPrefix = Bundle.main.object(forInfoDictionaryKey: "TeamIdentifierPrefix") as! String
location = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.\(teamIdentifierPrefix)com.myteam.myapp")!
            .appendingPathComponent("db.sqlite")
            .path

As in:

Wrong: "group.\(teamIdentifierPrefix).com.myteam.myapp"
Right: "group.\(teamIdentifierPrefix)com.myteam.myapp"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.