containerURLForSecurityApplicationGroupIdentifier returns nil on iOS Simulator
Asked Answered
G

7

22

Context

With iOS8 and extensions Apple introduced the App group containers (more info here).

Problem

We use it through NSFileManager's containerURLForSecurityApplicationGroupIdentifier: method. It works well in production on AppStore (with both iOS8 and iOS7). Problem is since our team updated to Xcode 6.1 (6A1052d), the method returns nil on Simulators.

We searched a lot but we didn't manage to find any clues. Even this question or this one are not applicable here.

TLDR: Question

Does NSFileManager's containerURLForSecurityApplicationGroupIdentifier: method works on your simulators? Is there any way to fix that?

Gaylord answered 4/11, 2014 at 16:40 Comment(2)
any solution you found?Within
I am working with WatchOS2 and iOS9, on the simulator. This same problem happens to me but intermittently. I have to reset/wipe/relaunch everything every 20 minutes. Why?Corrinecorrinne
C
17

I was able to solve this problem on my side. The documentation says

Your app must have a com.apple.security.application-groups entitlement for the specified application group. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/occ/instm/NSFileManager/containerURLForSecurityApplicationGroupIdentifier:

At some point in my app, I had removed the entitlements file from my app target/build settings/code signing/code signing entitlements . Both debug and release must include YOUR_APP.entitlements file . My debug wasn't including it so it wasn't working on simulator

Conversationalist answered 13/1, 2015 at 14:22 Comment(1)
in my case Xcode generated the entitlements file but only added it to the release profile. great answer, thank youZealand
L
22

The method containerURLForSecurityApplicationGroupIdentifier: works in both device and Simulator.

If you have setup both targets (or Target and WatchKit Extension for instance) that are going to share data to support Groups, and it keeps returning nil, you may want to check your Targets settings.

  1. Target Settings > General > Team > yourCompany signing team should be selected here. If there is a warning and a Fix This Issue button, be good and fix the issue by hitting it :)

  2. Target Settings > Capabilities > App Groups > Is On + shows your group.com.yourcompany.yourapp.sharedContainter group name ("sharedContainer" is my choice name) is selected and is on black (not red). If it's red or there is a warning and Fix This Issue button, renew the group or create a different one.

  3. Target Settings > Build Settings > Code Signing > Code Signing Entitlements: points to your entitlements files for both Debug and Release (something like yourApp/yourApp.entitlements)

  4. Target Settings > Build Settings > Code Signing > Code Signing Identity: Make sure the identity is part of the Team selected above. I use iOS Developer.

  5. Target Settings > Build Settings > Code Signing > Provisioning Profile: As above, make sure the PP you are using is of the Team required. I set it to Automatic.

Check those for both targets sharing data.

I hope it helps

Longlimbed answered 10/3, 2015 at 16:8 Comment(5)
My issue was the bundle id, I had the same bundle id for the app and the watch app. Usually we need 3 bundle ids, the app, the watch app and the extension.Bim
FWIW, I had to explicitly select the correct Provisioning Profile as it kept failing with Automatic.Distinguish
The Fix Issue button actually fixed this for me.Ethyne
Excellent!! By the time I had fixed step 2 the problem was gone.Corrinecorrinne
Sometimes, the problem returns, you will know because you will get nil values when trying to acces data on the containers. Just repeat steps 1,2, when the Capability window refreshes, the group name changes from red to black by itself. XDCorrinecorrinne
C
17

I was able to solve this problem on my side. The documentation says

Your app must have a com.apple.security.application-groups entitlement for the specified application group. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/occ/instm/NSFileManager/containerURLForSecurityApplicationGroupIdentifier:

At some point in my app, I had removed the entitlements file from my app target/build settings/code signing/code signing entitlements . Both debug and release must include YOUR_APP.entitlements file . My debug wasn't including it so it wasn't working on simulator

Conversationalist answered 13/1, 2015 at 14:22 Comment(1)
in my case Xcode generated the entitlements file but only added it to the release profile. great answer, thank youZealand
I
5

It doesn't make it clear the the docs but I found that the group identifier is case sensitive.

Ivette answered 13/4, 2015 at 11:41 Comment(0)
R
1

I find containerURLForSecurityApplicationGroupIdentifier can do work both on real device and simulator with xcode 6.2. I came across into this thread because i also got nil, but now i find the root cause is my typing error of the 'App Groups' name.

More, here is the list you need check:

  1. make sure 'app groups' been created either by xcode or web console at https://developer.apple.com
  2. make sure the 'app groups' is 'enabled' on 'Development' and 'Production' at 'web console: Identifiers->App IDs'
  3. make sure the matched 'Provisioning Profiles' are 'active'
  4. the groups name of entitlement in both containing app and extension app should match the one you created in step 1
  5. make sure the bundle identifier of all targets is the same as the one from 'web console: Identifiers->App IDs
  6. make sure the 'Team' inside of all targets matches the one from web console
  7. make sure to pick the correct group under 'Capabilities' tab on both containing app and extension app target, and no red color
  8. clean up xcode cache by cmd+shift+k
  9. rebuild and pray
Rusk answered 14/3, 2015 at 16:13 Comment(0)
U
0

The Method containerURLForSecurityApplicationGroupIdentifier: works in my Simulator. But if i delete all files in the group folder (in case a user performs a logout in my app) the method returns nil on the next RUN in Xcode 6.1. I tested this with Xcode 6.2 and iOS SDK 8.2 Beta as well but it doesn't work.

The Code runs fine on a real device.

I also tried the above solutions without success.

Uranography answered 26/11, 2014 at 10:17 Comment(0)
G
0

For me there was some mess with my iOS Team Provisioning Profile (which is not required to run in Simulator).

In order to run my app on my iPhone I had to first reconfigure the iOS Team Provisioning Profile for the Debug environment. Calling containerURLForSecurityApplicationGroupIdentifier: worked perfectly fine on the device. Afterwards I went back to the Simulator and - there you go - it worked again as well.

Gauzy answered 1/2, 2015 at 0:7 Comment(0)
W
0

I've found that the reason of this error in my case was a file ".com.apple.mobile_container_manager.metadata.plist". After I've got my file from this directory, I deleted all files in it. So this file was deleted also. And when you delete this file, app returns you a nil for containerURLForSecurityApplicationGroupIdentifier

So I changed my code to this:

    NSURL *groupPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:YGGroupIdentifier];
    NSArray *fileArray = [fileManager contentsOfDirectoryAtPath:[groupPath path] error:nil];
    for (NSString *filename in fileArray)  {
        if (![filename hasPrefix:@".com"]) {
            [fileManager removeItemAtPath:[[groupPath path] stringByAppendingPathComponent:filename] error:nil];
        }
    }

Now it works great, even in simulator.

Woolfell answered 9/10, 2015 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.