security-scoped bookmarks

Hello!


New macOS app trying to allow user access to files they opened with NSOpenPanel at a later date | sessioin.


I can successfully use the code below to write a bookmark at the same location that behaves as expected.

I can do this AS LONG AS I do NOT use 'NSURLBookmarkCreationWithSecurityScope'.

Yes I have: com.apple.security.app-sandbox==true in my entitlements file


How do I go about understanding what is wrong?

           NSURLBookmarkCreationOptions bookmarkCreationOptions = (NSURLBookmarkCreationOptions)NSURLBookmarkCreationWithSecurityScope;
            NSURLBookmarkResolutionOptions bookmarkReadingOptions = (NSURLBookmarkResolutionOptions)(NSURLBookmarkResolutionWithoutUI|NSURLBookmarkResolutionWithoutMounting|NSURLBookmarkResolutionWithSecurityScope);
            self.attributedRTFString=[[NSMutableAttributedString alloc] initWithString:kMacEnactEmptyString];
            if ([self.attributedRTFString readFromURL:self.documentCurrentURL options:tDictDocDefaultRTFDocAttrs documentAttributes:&myDictionary error:&myError]==YES) {
                NSLog(@"readFromURL SUCCESS errorDesc:%@",[myError debugDescription]);
               if(myDictionary!=nil) NSLog(@"usedAttibutes: %@",myDictionary);
                tString_ = self.attributedRTFString.string;
                [[NSUserDefaults standardUserDefaults] setURL:self.documentCurrentURL forKey:@"rtfURL"];
                myError=nil;
                currentDocumentExtendedAttrs = [self.documentCurrentURL extendedAttributesWithError:&myError];
                if(myError!=nil) NSLog(@"extendedAttributedRTFString  ERROR: %@",[myError debugDescription]);
                myError=nil;
                NSLog(@"ready to: bookmarkDataWithOptions bookmarkCreationOptions:%ld",bookmarkCreationOptions);
                myData = [self.documentCurrentURL bookmarkDataWithOptions:bookmarkCreationOptions
                                        includingResourceValuesForKeys:nil
                                                         relativeToURL:nil
                                                                 error:&myError];
                if(myError!=nil) NSLog(@"bookmarkDataWithOptions ERROR  errorDesc:%@",[myError debugDescription]);
                else NSLog(@"bookmarkDataWithOptions SUCCESS  errorDesc:%@",[myError debugDescription]);
                myError=nil;
                NSLog(@"ready to: writeBookmarkData bookmarkCreationOptions:%ld",bookmarkCreationOptions);
               myBool= [NSURL writeBookmarkData:myData toURL:[self bookmarkURLForType:myType] options:bookmarkCreationOptions error:&myError];


                if((myBool==NO)&&(myError==nil)) NSLog(@"writeBookmarkData SUCCESS STALE:%hhd errorDesc:%@",myBool,[myError debugDescription]);
                else if((myBool==YES)||(myError!=nil)) NSLog(@"writeBookmarkData ERROR STALE:%hhd  errorDesc:%@",myBool,[myError debugDescription]);
            } else {
                if(myError!=nil) NSLog(@"readFromURL ERROR errorDesc:%@",[myError debugDescription]);
                tString_ = kMacEnactEmptyString;
                self.attributedRTFString = [[NSMutableAttributedString alloc] initWithString:tString_];
                [[NSUserDefaults standardUserDefaults] setURL:nil forKey:@"rtfURL"];
            }
            NSLog(@"attributedRTFString: %@",self.attributedRTFString);
-------
2016-12-14 08:44:51:654 macEnact[13368:6889260] rURL: file:///Users/steve/Documents/Untitled.rtf
2016-12-14 08:44:51.654555 macEnact[13368:6889260] documentRTFURL: file:///Users/steve/Documents/Untitled.rtf
2016-12-14 08:44:51.655558 macEnact[13368:6889260] readFromURL SUCCESS errorDesc:(null)
2016-12-14 08:44:51.655697 macEnact[13368:6889260] usedAttibutes: {
    BottomMargin = 72;
    CocoaRTFVersion = 1504;
    DefaultTabInterval = 0;
    DocumentType = NSRTF;
    HyphenationFactor = 0;
    LeftMargin = 90;
    PaperSize = "NSSize: {612, 792}";
    RightMargin = 90;
    TopMargin = 72;
    UTI = "public.rtf";
    UsesScreenFonts = 0;
}
2016-12-14 08:44:51.656058 macEnact[13368:6889260] ready to: bookmarkDataWithOptions bookmarkCreationOptions:2048
2016-12-14 08:44:51.661549 macEnact[13368:6889260] bookmarkDataWithOptions SUCCESS  errorDesc:(null)
2016-12-14 08:44:51.661588 macEnact[13368:6889260] ready to: writeBookmarkData bookmarkCreationOptions:2048
2016-12-14 08:44:51.662415 macEnact[13368:6889260] writeBookmarkData ERROR STALE:0  errorDesc:Error Domain=NSCocoaErrorDomain Code=512 "The file couldn’t be saved."
2016-12-14 08:44:51.662880 macEnact[13368:6889260] attributedRTFString: tMutAttrString_{
    NSFont = "\"Geneva 39.20 pt. P [] (0x600000249bd0) fobj=0x100d41930, spc=13.07\"";
…

Aw jeez I meant to say:

<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>

In the hopes of helping someone else - here's a better statement of the problem:


-(BOOL) writeBookmarkForURL:(NSURL*)thisURL ofType:(NSUInteger)thisType asReadOnly:(BOOL)readOnly; {
    BOOL didWriteURL = NO;
    NSError *thisError = nil;
    if (readOnly) {
        bookmarkCreationOptions = (NSURLBookmarkCreationOptions)(NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess);
    } else {
        bookmarkCreationOptions = (NSURLBookmarkCreationOptions)(NSURLBookmarkCreationWithSecurityScope);
    }
    NSLog(@"ready to: bookmarkDataWithOptions bookmarkCreationOptions:%ld",bookmarkCreationOptions);
    NSData*myData = [thisURL bookmarkDataWithOptions:bookmarkCreationOptions includingResourceValuesForKeys:nil relativeToURL:nil error:&thisError];
    if(thisError!=nil) NSLog(@"bookmarkDataWithOptions ERROR  errorDesc:%@",[thisError debugDescription]);
    else if(myData!=nil)NSLog(@"bookmarkDataWithOptions SUCCESS  errorDesc:%@",[thisError debugDescription]);
    if(myData!=nil){
        thisError=nil;
        NSURL*bookmarkURL = [self bookmarkURLForType:(int)thisType];
        NSLog(@"ready to: writeBookmarkData bookmarkCreationOptions:%ld",bookmarkCreationOptions);
        NSLog(@"ready to: writeBookmarkData[%lu] to URL:%@",myData.length,bookmarkURL);
        didWriteURL= [NSURL writeBookmarkData:myData toURL:bookmarkURL options:bookmarkCreationOptions error:&thisError];
        if(thisError!=nil) NSLog(@"writeBookmarkData ERROR  errorDesc:%@",[thisError debugDescription]);
    } else {
        NSLog(@"bookmarkDataWithOptions ERROR ** NO DATA RETURNED ** errorDesc:%@",[thisError debugDescription]);
    }
   return didWriteURL;
}


2016-12-14 12:57:24.525621 macXYZZ[14748:7731477] ready to: bookmarkDataWithOptions bookmarkCreationOptions:4096

2016-12-14 12:57:30.326348 macXYZZ[14748:7731477] bookmarkDataWithOptions SUCCESS errorDesc:(null)

2016-12-14 12:57:43.551668 macXYZZ[14748:7731477] ready to: writeBookmarkData bookmarkCreationOptions:4096

2016-12-14 12:57:43.551786 macXYZZ[14748:7731477] ready to: writeBookmarkData[892] to URL:file:/Users/steve/Library/Containers/com.mac-chi.macxyzz/Data/Library/Application%20Support/macXYZZ/aliases/rtfAlias

2016-12-14 12:57:43.552932 macXYZZ[14748:7731477] writeBookmarkData ERROR errorDesc:Error Domain=NSCocoaErrorDomain Code=512 "The file couldn’t be saved."

So can I ask why the post has been in moderation for 2 days?

security-scoped bookmarks
 
 
Q