12 Replies
      Latest reply: Oct 27, 2016 5:57 PM by Macho Man Randy Savage RSS
      Macho Man Randy Savage Level 3 Level 3 (255 points)

        1) Main app and extension register for app groups.

         

        2) Main app gets a url from NSOpenPanel, makes a security scoped bookmark and saves is in NSUserdefaults with group container as the suite name.

         

        3) Run the extension, grab the bookmark data from user defaults and resolve it. The data is not nil, but fails to resolve:

         

        Error Domain=NSCocoaErrorDomain Code=259 "The file couldn’t be opened because it isn’t in the correct format."

        • Re: Share security scoped bookmark in app group?
          eskimo Apple Staff Apple Staff (5,995 points)

          The data is not nil, but fails to resolve:

          Three things:

          • Do you have the same sandbox settings on your app and your extension?  Of specific concern is com.apple.security.files.user-selected.read-only.

          • Presumably you create the bookmark with -bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error:.  What options do you pass in?

          • Have you logged the bookmark data to make sure you’re getting back the right value?  You can do that with a simple NSLog:

            NSLog(@"bookmark = %@", bookmarkData);
            

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Share security scoped bookmark in app group?
              Macho Man Randy Savage Level 3 Level 3 (255 points)

              Yes the bookmark data is the same when logging it out if I run the same code from the main app or the app extension. The difference is the main app resolves it without error but the extension fails with the error.

               

              I make the bookmark like this:

               

                NSURL *url = openPanel.URL;
              
                      NSError *error = nil;
                      NSData *bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                                                 includingResourceValuesForKeys:nil
                                                                  relativeToURL:nil
                                                                          error:&error];
                      if (bookmark != nil)
                      {
                          NSUserDefaults *userDefaults = [[NSUserDefaults alloc]initWithSuiteName:MY_GROUP_ID_HERE];
                          [userDefaults setObject:bookmark forKey:USER_DEFAULTS_BOOKMARK_KEY_HERE];
                          [userDefaults synchronize];
                     }
                    else
                    {
                            //check the error
                    }
              
              
              

               

               

              And I resolve the bookmark:

               

                   urlFromBookmark = [NSURL URLByResolvingBookmarkData:bookmarkData
                                                                 options:NSURLBookmarkResolutionWithSecurityScope
                                                           relativeToURL:nil
                                                     bookmarkDataIsStale:&isStale
                                                                   error:&error];
              
              

               

              The app and the app extension import the same file, and are using the exact same function to resolve the bookmark. Also I have com.apple.security.files.user-selected.read-write to YES in entitlements.