From 5fd626d0871d3a2511b6a221d0dd1c108cbb5330 Mon Sep 17 00:00:00 2001 From: Nicholas Sheehan Date: Sat, 24 Apr 2021 21:58:17 +0100 Subject: [PATCH] * Fixed iOS deprecation notice finally, this also means we don't have to treat how we present the iPhones and iPads differently * Some more renaming on the C# * Need to update the change list --- Adapters/IosUnityNativeSharingAdapter.cs | 4 +- Plugins/iOS/UnityNative_Sharing.h | 2 +- Plugins/iOS/UnityNative_Sharing.mm | 82 +++++++++++++++--------- 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/Adapters/IosUnityNativeSharingAdapter.cs b/Adapters/IosUnityNativeSharingAdapter.cs index bd74aeb..2820897 100644 --- a/Adapters/IosUnityNativeSharingAdapter.cs +++ b/Adapters/IosUnityNativeSharingAdapter.cs @@ -5,14 +5,14 @@ namespace UnityNative.Sharing public class IosUnityNativeSharingAdapter : IUnityNativeSharingAdapter { [System.Runtime.InteropServices.DllImport("__Internal")] - private static extern void UnityNative_Sharing_ShareTextAndScreenshot(string shareText, string filePath); + private static extern void UnityNative_Sharing_ShareTextAndFile(string shareText, string filePath); [System.Runtime.InteropServices.DllImport("__Internal")] private static extern void UnityNative_Sharing_ShareText(string shareText); public void ShareScreenshotAndText(string shareText, string filePath, bool showShareDialogBox = true, string shareDialogBoxText = "Select App To Share With", string mimeType = "image/*") { - UnityNative_Sharing_ShareTextAndScreenshot(shareText, filePath); + UnityNative_Sharing_ShareTextAndFile(shareText, filePath); } public void ShareText(string shareText, bool showShareDialogBox = true, string shareDialogBoxText = "Select App To Share With") diff --git a/Plugins/iOS/UnityNative_Sharing.h b/Plugins/iOS/UnityNative_Sharing.h index b9155ce..43b4567 100644 --- a/Plugins/iOS/UnityNative_Sharing.h +++ b/Plugins/iOS/UnityNative_Sharing.h @@ -11,7 +11,7 @@ extern UIViewController* UnityGetGLViewController(); #ifdef __cplusplus extern "C" { #endif - void UnityNative_Sharing_ShareTextAndScreenshot(const char* shareText, const char* sharePath); + void UnityNative_Sharing_ShareTextAndFile(const char* shareText, const char* filePath); void UnityNative_Sharing_ShareText(const char* shareText); #ifdef __cplusplus } diff --git a/Plugins/iOS/UnityNative_Sharing.mm b/Plugins/iOS/UnityNative_Sharing.mm index e2e9ac4..77326a2 100644 --- a/Plugins/iOS/UnityNative_Sharing.mm +++ b/Plugins/iOS/UnityNative_Sharing.mm @@ -11,56 +11,76 @@ // void UnityNative_Sharing_ShareText(const char* shareText) { - UnityNative_Sharing_ShareTextAndScreenshot(shareText, ""); + UnityNative_Sharing_ShareTextAndFile(shareText, ""); } // -// Shares Screenshot and Text +// Shares File and Text // -void UnityNative_Sharing_ShareTextAndScreenshot(const char* shareText, const char* imagePath) +void UnityNative_Sharing_ShareTextAndFile(const char* shareText, const char* filePath) { - NSString *textToShare = [NSString stringWithUTF8String:shareText]; - NSString *pathToImage = [NSString stringWithUTF8String:imagePath]; - NSMutableArray *items = [NSMutableArray new]; - //Check to see if string is empty or null - if(textToShare != NULL && textToShare.length > 0) [items addObject:textToShare]; + NSString *textToShare = [NSString stringWithUTF8String:shareText]; + if(textToShare != NULL && textToShare.length > 0) + { + [items addObject:textToShare]; + } - //Check to see if string is empty or null - if(pathToImage != NULL && pathToImage.length > 0) + NSString *pathToFile = [NSString stringWithUTF8String:filePath]; + if(pathToFile != NULL && pathToFile.length > 0) { - NSFileManager *fileMgr = [NSFileManager defaultManager]; + NSFileManager *fileManager = [NSFileManager defaultManager]; - //Check to see if the file exists - if([fileMgr fileExistsAtPath:pathToImage]) + // Check to see if the file exists + if([fileManager fileExistsAtPath:pathToFile]) { - NSURL *formattedURL = [NSURL fileURLWithPath:pathToImage]; + NSURL *formattedURL = [NSURL fileURLWithPath:pathToFile]; [items addObject:formattedURL]; } - //File not found + // File not found else { - NSString *message = [NSString stringWithFormat:@"Cannot find file %@", pathToImage]; + NSString *message = [NSString stringWithFormat:@"Cannot find file %@", pathToFile]; NSLog(@"%s", message.UTF8String); } } - UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:Nil]; - [activity setValue:@"" forKey:@"subject"]; + UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items + applicationActivities:nil]; - UIViewController *rootViewController = UnityGetGLViewController(); - - //iPhone share view - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) - { - [rootViewController presentViewController:activity animated:YES completion:nil]; - } - //iPad share view - else +// [activity setValue:@"" forKey:@"subject"]; // TODO be able to set a subject string here + + // Callback from iOS when the activity finishes + activity.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) { - //custom area to show the share popup - UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activity]; - [popup presentPopoverFromRect:CGRectMake(rootViewController.view.frame.size.width/2, rootViewController.view.frame.size.height/4, 0, 0)inView:rootViewController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; - } + NSLog(@"Shared to %s", activityType.UTF8String); + + if(activityError) + { + NSLog(@"Error: [%@]", activityError); + } + + // TODO maybe a callback into Unity here? + }; + + UIViewController *rootViewController = UnityGetGLViewController(); + + CGSize screenSize = rootViewController.view.frame.size; + + // TODO make this confirgureable from C# + CGRect rect = CGRectMake(screenSize.width / 2, // Width + screenSize.height / 4, // Height + 0, // X + 0); // Y + + activity.modalPresentationStyle = UIModalPresentationPopover; + [rootViewController presentViewController:activity + animated:YES + completion:nil]; // Completion is invoked when the popup finishes animating in + + UIPopoverPresentationController *popup = activity.popoverPresentationController; + popup.sourceView = rootViewController.view; + popup.sourceRect = rect; + popup.permittedArrowDirections = UIPopoverArrowDirectionAny; }