Created
June 22, 2020 21:47
-
-
Save adamski/24332cebb76e27405ddbfc4ba0bcf8f9 to your computer and use it in GitHub Desktop.
JUCE NSURL Utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <JuceHeader.h> | |
#if JUCE_IOS | |
class NSURLUtils | |
{ | |
public: | |
static URL URLfromBookmark (void* bookmark); | |
static void* bookmarkFromURL (URL& url); | |
static String lastKnownPathFromBookmark (void* bookmark); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#include "NSURLUtils.h" | |
#define NSURLPathKey @"_NSURLPathKey" | |
URL NSURLUtils::URLfromBookmark (void* bookmarkData) | |
{ | |
NSData* bookmark = static_cast<NSData*> (bookmarkData); | |
BOOL isBookmarkStale = false; | |
NSError* error = nil; | |
auto nsURL = [NSURL URLByResolvingBookmarkData:bookmark | |
options:0 | |
relativeToURL:nil | |
bookmarkDataIsStale:&isBookmarkStale | |
error:&error]; | |
if (error == nil) | |
{ | |
auto urlString = String([[nsURL absoluteString] UTF8String]); | |
URL url (urlString); | |
return url; | |
} | |
auto desc = [error localizedDescription]; | |
ignoreUnused(desc); | |
DBG ([desc UTF8String]); | |
//jassertfalse; | |
return URL(); | |
} | |
void* NSURLUtils::bookmarkFromURL (URL& url) | |
{ | |
if (NSData* bookmark = (NSData*) getURLBookmark (url)) | |
{ | |
BOOL isBookmarkStale = false; | |
NSError* error = nil; | |
auto nsURL = [NSURL URLByResolvingBookmarkData:bookmark | |
options:0 | |
relativeToURL:nil | |
bookmarkDataIsStale:&isBookmarkStale | |
error:&error]; | |
if (error == nil) | |
{ | |
bool securityAccessSucceeded = [nsURL startAccessingSecurityScopedResource]; | |
NSError* error = nil; | |
NSData* updatedBookmark = [nsURL bookmarkDataWithOptions:NSURLBookmarkCreationSuitableForBookmarkFile | |
includingResourceValuesForKeys:nil | |
relativeToURL:nil | |
error:&error]; | |
if (securityAccessSucceeded) | |
[nsURL stopAccessingSecurityScopedResource]; | |
if (error == nil) | |
return updatedBookmark; | |
else | |
jassertfalse; | |
} | |
} | |
} | |
String NSURLUtils::lastKnownPathFromBookmark (void* bookmarkData) | |
{ | |
NSDictionary *values = [NSURL resourceValuesForKeys:@[NSURLPathKey] | |
fromBookmarkData:static_cast<NSData*> (bookmarkData)]; | |
NSString *path = [values objectForKey:NSURLPathKey]; | |
return String([path UTF8String]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment