Skip to content

Commit

Permalink
* Fixed iOS deprecation notice finally, this also means we don't have…
Browse files Browse the repository at this point in the history
… to treat how we present the iPhones and iPads differently

* Some more renaming on the C#
* Need to update the change list
  • Loading branch information
NicholasSheehan committed Apr 24, 2021
1 parent 3c50c0e commit 5fd626d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Adapters/IosUnityNativeSharingAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion Plugins/iOS/UnityNative_Sharing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
82 changes: 51 additions & 31 deletions Plugins/iOS/UnityNative_Sharing.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 5fd626d

Please sign in to comment.