Re: Saving Bookmark Data to XML File
Re: Saving Bookmark Data to XML File
- Subject: Re: Saving Bookmark Data to XML File
- From: Andreas Grosam <email@hidden>
- Date: Fri, 06 Jan 2012 11:25:52 +0100
On Jan 6, 2012, at 8:00 AM, Peter Zegelin wrote:
> I am trying to save a bookmark into an xml file but am having difficulty converting the bookmark to and from an NSString.
It seems from the documentation that a "bookmarkData" is an opaque representation hold by a NSData object which you can store on disk and read from and also recreate an URL from it. An arbitrary NSData object cannot be converted *directly* to a string. Think of it as binary data like an image file.
Since you cannot store binary data in XML you may have success converting the content of the NSData object to a base64 encoded string and use that string to write to your XML.
Unfortunately, there is no System API for a base64 encoder/decoder in Cocoa/CoreFoundation. There is third party software, though. If you can't find an appropriate base64 encoder/decoder I can you help out, too.
Andreas
> Below is my code. I have temporarily put it all in the NSDocument readFromURL method:
>
>
> - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError{
>
> NSData *bookmark = [absoluteURL bookmarkDataWithOptions: NSURLBookmarkCreationSuitableForBookmarkFile
> includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];
>
> NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark options: NSURLBookmarkResolutionWithoutUI relativeToURL:nil
> bookmarkDataIsStale:NULL error:NULL];
>
> [model newFile:[url path]];
>
> // The above works. The following however doesn't:
>
> NSString *str = [[NSString alloc] initWithData:bookmark encoding:NSUTF16StringEncoding];
>
> NSData *data = [str dataUsingEncoding:NSUTF16StringEncoding];
>
> url = [NSURL URLByResolvingBookmarkData:data options: NSURLBookmarkResolutionWithoutUI relativeToURL:nil
> bookmarkDataIsStale:NULL error:NULL];
>
> // url is nil here
>
> [model newFile:[url path]];
>
> return YES;
> }
>
> In the second part I am simply trying to convert the bookmark to and from a string, but it fails. I have tried several encodings - NSUTF8StringEncoding, NSUnicodeStringEncoding but no luck.
>
> Can anyone point out what I might be doing wrong?
>
> Thanks!
>
> Peter_______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden