Created
October 29, 2021 19:17
-
-
Save modosc/894da18453826c35d4b3e4e421778550 to your computer and use it in GitHub Desktop.
patch for ios custom file types in juce
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
diff --git a/modules/juce_events/messages/juce_ApplicationBase.h b/modules/juce_events/messages/juce_ApplicationBase.h | |
index efb639d27..1086efeaa 100644 | |
--- a/modules/juce_events/messages/juce_ApplicationBase.h | |
+++ b/modules/juce_events/messages/juce_ApplicationBase.h | |
@@ -205,6 +205,13 @@ public: | |
*/ | |
virtual void memoryWarningReceived() { jassertfalse; } | |
+ /** Called by the operating system when a custim file type was opened. You are expected | |
+ * to return true of you handled the URL. | |
+ | |
+ At the moment this method is only called on iOS. | |
+ */ | |
+ virtual void urlOpened(const URL&) { } | |
+ | |
//============================================================================== | |
/** This will be called when the back button on a device is pressed. The return value | |
should be used to indicate whether the back button event has been handled by | |
diff --git a/modules/juce_gui_basics/native/juce_ios_Windowing.mm b/modules/juce_gui_basics/native/juce_ios_Windowing.mm | |
index 41503d2a7..a1b3d2b16 100644 | |
--- a/modules/juce_gui_basics/native/juce_ios_Windowing.mm | |
+++ b/modules/juce_gui_basics/native/juce_ios_Windowing.mm | |
@@ -192,6 +192,66 @@ namespace juce | |
{ | |
_pushNotificationsDelegate = delegate; | |
} | |
+- (BOOL) application:(UIApplication *)application openURL:(NSURL *) | |
+ url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options | |
+{ | |
+ if(!JUCEApplicationBase::getInstance()) | |
+ { | |
+ [self applicationDidFinishLaunching:application]; | |
+ } | |
+ | |
+ // mostly stolen from didPickDocumentAtURL | |
+ NSUInteger accessOptions = NSFileCoordinatorReadingWithoutChanges; | |
+ | |
+ auto *fileAccessIntent = [NSFileAccessIntent readingIntentWithURL:url options:accessOptions]; | |
+ | |
+ NSArray<NSFileAccessIntent *> *intents = @[fileAccessIntent]; | |
+ | |
+ auto *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; | |
+ | |
+ [fileCoordinator coordinateAccessWithIntents:intents queue:[NSOperationQueue mainQueue] byAccessor:^(NSError *err) { | |
+ if (err == nil) { | |
+ [url startAccessingSecurityScopedResource]; | |
+ | |
+ NSError *error = nil; | |
+ | |
+ NSData *bookmark = [url bookmarkDataWithOptions:0 | |
+ includingResourceValuesForKeys:nil | |
+ relativeToURL:nil | |
+ error:&error]; | |
+ | |
+ [bookmark retain]; | |
+ | |
+ [url stopAccessingSecurityScopedResource]; | |
+ | |
+ URL juceUrl(nsStringToJuce([url absoluteString])); | |
+ | |
+ if (error == nil) { | |
+ setURLBookmark(juceUrl, (void *) bookmark); | |
+ } else { | |
+ auto *desc = [error localizedDescription]; | |
+ ignoreUnused(desc); | |
+ jassertfalse; | |
+ } | |
+ | |
+ if (auto *app = JUCEApplicationBase::getInstance()) | |
+ { | |
+ app->urlOpened(juceUrl); | |
+ } | |
+ else | |
+ { | |
+ jassertfalse; | |
+ } | |
+ } else { | |
+ auto *desc = [err localizedDescription]; | |
+ ignoreUnused(desc); | |
+ jassertfalse; | |
+ } | |
+ }]; | |
+ | |
+ return YES; | |
+} | |
+ | |
#if JUCE_PUSH_NOTIFICATIONS | |
- (void) application: (UIApplication*) application didRegisterUserNotificationSettings: (UIUserNotificationSettings*) notificationSettings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment