Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
Fixed share dialog not disappearing automatically when canceling a sh…
Browse files Browse the repository at this point in the history
…are operation on iPhones (which could lead to share callback being called multiple times)
  • Loading branch information
yasirkula committed Dec 10, 2022
1 parent 25205f4 commit 13ad5af
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Plugins/NativeShare/README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Native Share for Android & iOS (v1.4.8) =
= Native Share for Android & iOS (v1.4.9) =

Online documentation & example code available at: https://github.com/yasirkula/UnityNativeShare
E-mail: [email protected]
Expand Down
49 changes: 31 additions & 18 deletions Plugins/NativeShare/iOS/NativeShare.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,46 +85,59 @@ - (NSString *)activityViewController:(UIActivityViewController *)activityViewCon
[items addObject:[NSURL fileURLWithPath:filePath]];
}

UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
if( strlen( subject ) > 0 )
[activity setValue:[NSString stringWithUTF8String:subject] forKey:@"subject"];
else if( [items count] == 0 )
if( strlen( subject ) == 0 && [items count] == 0 )
{
NSLog( @"Share canceled because there is nothing to share..." );
UnitySendMessage( "NSShareResultCallbackiOS", "OnShareCompleted", "2" );

return;
}

if( CHECK_IOS_VERSION( @"8.0" ) )
UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
if( strlen( subject ) > 0 )
[activity setValue:[NSString stringWithUTF8String:subject] forKey:@"subject"];

void (^shareResultCallback)(UIActivityType activityType, BOOL completed, UIActivityViewController *activityReference) = ^void( UIActivityType activityType, BOOL completed, UIActivityViewController *activityReference )
{
activity.completionWithItemsHandler = ^( UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError )
NSLog( @"Shared to %@ with result: %d", activityType, completed );

if( activityReference )
{
NSLog( @"Shared to %@ with result: %d", activityType, completed );

if( activityError != nil )
NSLog( @"Share error: %@", activityError );

const char *resultMessage = [[NSString stringWithFormat:@"%d%@", completed ? 1 : 2, activityType] UTF8String];
char *result = (char*) malloc( strlen( resultMessage ) + 1 );
strcpy( result, resultMessage );

UnitySendMessage( "NSShareResultCallbackiOS", "OnShareCompleted", result );

// On iPhones, the share sheet isn't dismissed automatically when share operation is canceled, do that manually here
if( !completed && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
[activityReference dismissViewControllerAnimated:NO completion:nil];
}
else
NSLog( @"Share result callback is invoked multiple times!" );
};

if( CHECK_IOS_VERSION( @"8.0" ) )
{
__block UIActivityViewController *activityReference = activity; // About __block usage: https://gist.github.com/HawkingOuYang/b2c9783c75f929b5580c
activity.completionWithItemsHandler = ^( UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError )
{
if( activityError != nil )
NSLog( @"Share error: %@", activityError );

shareResultCallback( activityType, completed, activityReference );
activityReference = nil;
};
}
else if( CHECK_IOS_VERSION( @"6.0" ) )
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
__block UIActivityViewController *activityReference = activity;
activity.completionHandler = ^( UIActivityType activityType, BOOL completed )
{
NSLog( @"Shared to %@ with result: %d", activityType, completed );

const char *resultMessage = [[NSString stringWithFormat:@"%d%@", completed ? 1 : 2, activityType] UTF8String];
char *result = (char*) malloc( strlen( resultMessage ) + 1 );
strcpy( result, resultMessage );

UnitySendMessage( "NSShareResultCallbackiOS", "OnShareCompleted", result );
shareResultCallback( activityType, completed, activityReference );
activityReference = nil;
};
#pragma clang diagnostic pop
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.nativeshare",
"displayName": "Native Share",
"version": "1.4.8",
"version": "1.4.9",
"documentationUrl": "https://github.com/yasirkula/UnityNativeShare",
"changelogUrl": "https://github.com/yasirkula/UnityNativeShare/releases",
"licensesUrl": "https://github.com/yasirkula/UnityNativeShare/blob/master/LICENSE.txt",
Expand Down

0 comments on commit 13ad5af

Please sign in to comment.