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

Add url support, handle null strings on iOS, and fix deprecated calls on iOS. #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Plugins/NativeShare/NativeShare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ private static AndroidJavaObject Context
}
#elif !UNITY_EDITOR && UNITY_IOS
[System.Runtime.InteropServices.DllImport( "__Internal" )]
private static extern void _NativeShare_Share( string[] files, int filesCount, string subject, string text );
private static extern void _NativeShare_Share( string[] files, int filesCount, string subject, string text, string url, bool prioritizeFile );
#endif

private string subject;
private string url;
private string text;
private string title;

Expand Down Expand Up @@ -70,6 +71,14 @@ public NativeShare SetSubject( string subject )
return this;
}

public NativeShare SetUrl(string url)
{
if (url != null)
this.url = url;

return this;
}

public NativeShare SetText( string text )
{
if( text != null )
Expand Down Expand Up @@ -114,7 +123,7 @@ public NativeShare AddFile( string filePath, string mime = null )

public void Share()
{
if( files.Count == 0 && subject.Length == 0 && text.Length == 0 )
if( files.Count == 0 && string.IsNullOrEmpty(subject) && string.IsNullOrEmpty(text) && string.IsNullOrEmpty(url) )
{
Debug.LogWarning( "Share Error: attempting to share nothing!" );
return;
Expand All @@ -123,15 +132,16 @@ public void Share()
#if UNITY_EDITOR
Debug.Log( "Shared!" );
#elif UNITY_ANDROID
AJC.CallStatic( "Share", Context, targetPackage, targetClass, files.ToArray(), mimes.ToArray(), subject, text, title );
string contextText = !string.IsNullOrEmpty(text) ? (text + " " + url) : url;
AJC.CallStatic( "Share", Context, targetPackage, targetClass, files.ToArray(), mimes.ToArray(), subject, contextText, title );
#elif UNITY_IOS
_NativeShare_Share( files.ToArray(), files.Count, subject, text );
_NativeShare_Share( files.ToArray(), files.Count, subject, text, url, true );
#else
Debug.Log( "No sharing set up for this platform." );
#endif
}

#region Utility Functions
#region Utility Functions
public static bool TargetExists( string androidPackageName, string androidClassName = null )
{
#if !UNITY_EDITOR && UNITY_ANDROID
Expand Down Expand Up @@ -175,6 +185,6 @@ public static bool FindTarget( out string androidPackageName, out string android
return false;
#endif
}
#endregion
#endregion
}
#pragma warning restore 0414
149 changes: 141 additions & 8 deletions Plugins/NativeShare/iOS/NativeShare.mm
Original file line number Diff line number Diff line change
@@ -1,31 +1,164 @@
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#ifdef UNITY_4_0 || UNITY_5_0
#import "iPhone_View.h"
#else
extern UIViewController* UnityGetGLViewController();
#endif

@interface PlatformDependentMediaProvider:UIActivityItemProvider <UIActivityItemSource>
@property (nonatomic, copy) NSString *filePath;
- (PlatformDependentMediaProvider *)initWithFilePath:(NSString *)filePath;
@end
@implementation PlatformDependentMediaProvider
@synthesize filePath;

- (PlatformDependentMediaProvider *)initWithFilePath:(NSString *)filePath {
if (self = [super initWithPlaceholderItem:filePath]) {
self.filePath = filePath;
}
return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
return [[UIImage alloc] init];
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType {
if ([activityType isEqualToString:@"com.facebook.Messenger.ShareExtension"]) {
return filePath.length != 0 ? [[UIImage alloc] initWithContentsOfFile:filePath] : nil;
}

return filePath.length != 0 ? [NSURL fileURLWithPath:filePath] : nil;
}
@end

@interface OptionalPlatformDependentMediaProvider:UIActivityItemProvider <UIActivityItemSource>
@property (nonatomic, copy) NSString *filePath;
- (OptionalPlatformDependentMediaProvider *)initWithFilePath:(NSString *)filePath;
@end
@implementation OptionalPlatformDependentMediaProvider
@synthesize filePath;

- (OptionalPlatformDependentMediaProvider *)initWithFilePath:(NSString *)filePath {
if (self = [super initWithPlaceholderItem:filePath]) {
self.filePath = filePath;
}
return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
return [[UIImage alloc] init];
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType {
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
return nil;
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
return nil;
} else if ([activityType isEqualToString:@"com.facebook.Messenger.ShareExtension"]) {
return filePath.length != 0 ? [[UIImage alloc] initWithContentsOfFile:filePath] : nil;
}

return filePath.length != 0 ? [NSURL fileURLWithPath:filePath] : nil;
}
@end

@interface OptionalPlatformDependentImageProvider:UIActivityItemProvider <UIActivityItemSource>
@property (nonatomic, copy) UIImage *image;
- (OptionalPlatformDependentImageProvider *)initWithImage:(UIImage *)image;
@end
@implementation OptionalPlatformDependentImageProvider
@synthesize image;

- (OptionalPlatformDependentImageProvider *)initWithImage:(UIImage *)image {
if (self = [super initWithPlaceholderItem:image]) {
self.image = image;
}
return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
return image;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType {
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
return nil;
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
return nil;
} else if ([activityType isEqualToString:@"com.facebook.Messenger.ShareExtension"]) {
return image;
}

return image;
}
@end

@interface OptionalPlatformDependentUrlProvider:UIActivityItemProvider <UIActivityItemSource>
@property (nonatomic, copy) NSString *urlPath;
- (OptionalPlatformDependentUrlProvider *)initWithUrlPath:(NSString *)urlPath;
@end
@implementation OptionalPlatformDependentUrlProvider
@synthesize urlPath;

- (OptionalPlatformDependentUrlProvider *)initWithUrlPath:(NSString *)urlPath {
if (self = [super initWithPlaceholderItem:urlPath]) {
self.urlPath = urlPath;
}
return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
return urlPath.length != 0 ? [[NSURL alloc] initWithString:urlPath] : [[NSURL alloc] initWithString:@""];
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType {
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
return nil;
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
return nil;
} else if ([activityType isEqualToString:@"com.facebook.Messenger.ShareExtension"]) {
return nil;
}

return urlPath.length != 0 ? [[NSURL alloc] initWithString:urlPath] : nil;
}
@end

// Credit: https://github.com/ChrisMaire/unity-native-sharing

extern "C" void _NativeShare_Share( const char* files[], int filesCount, char* subject, const char* text )
extern "C" void _NativeShare_Share( const char* files[], int filesCount, char* subject, const char* text, const char* url, bool prioritizeFile )
{
NSMutableArray *items = [NSMutableArray new];
if(filesCount == 0 || !prioritizeFile)
[items addObject:[[NSURL alloc] initWithString: [NSString stringWithUTF8String:url]]];
else if( url && strlen( url ) > 0 )
[items addObject:[[OptionalPlatformDependentUrlProvider alloc] initWithUrlPath: [NSString stringWithUTF8String:url]]];

if( strlen( text ) > 0 )
if( text && strlen( text ) > 0 )
[items addObject:[NSString stringWithUTF8String:text]];

// Credit: https://answers.unity.com/answers/862224/view.html
for( int i = 0; i < filesCount; i++ )
{
NSString *filePath = [NSString stringWithUTF8String:files[i]];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
if( image != nil )
[items addObject:image];
else
[items addObject:[NSURL fileURLWithPath:filePath]];
if(prioritizeFile) {
if( image )
[items addObject:image];
else
[items addObject:[[PlatformDependentMediaProvider alloc] initWithFilePath:filePath]];
} else {
if( image )
[items addObject:[[OptionalPlatformDependentImageProvider alloc] initWithImage:image]];
else
[items addObject:[[OptionalPlatformDependentMediaProvider alloc] initWithFilePath:filePath]];
}
}

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

UIViewController *rootViewController = UnityGetGLViewController();
Expand All @@ -38,4 +171,4 @@
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activity];
[popup presentPopoverFromRect:CGRectMake( rootViewController.view.frame.size.width / 2, rootViewController.view.frame.size.height / 2, 1, 1 ) inView:rootViewController.view permittedArrowDirections:0 animated:YES];
}
}
}