This repository has been archived by the owner on Mar 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
NativeShare.mm
144 lines (123 loc) · 5.31 KB
/
NativeShare.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#ifdef UNITY_4_0 || UNITY_5_0
#import "iPhone_View.h"
#else
extern UIViewController* UnityGetGLViewController();
#endif
#define CHECK_IOS_VERSION( version ) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] != NSOrderedAscending)
// Credit: https://github.com/ChrisMaire/unity-native-sharing
// Credit: https://stackoverflow.com/a/29916845/2373034
@interface UNativeShareEmailItemProvider : NSObject <UIActivityItemSource>
@property (nonatomic, strong) NSString *subject;
@property (nonatomic, strong) NSString *body;
@end
// Credit: https://stackoverflow.com/a/29916845/2373034
@implementation UNativeShareEmailItemProvider
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
return [self body];
}
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
return [self body];
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
{
return [self subject];
}
@end
extern "C" void _NativeShare_Share( const char* files[], int filesCount, const char* subject, const char* text, const char* link )
{
NSMutableArray *items = [NSMutableArray new];
// When there is a subject on iOS 7 or later, text is provided together with subject via a UNativeShareEmailItemProvider
// Credit: https://stackoverflow.com/a/29916845/2373034
if( strlen( subject ) > 0 && CHECK_IOS_VERSION( @"7.0" ) )
{
UNativeShareEmailItemProvider *emailItem = [UNativeShareEmailItemProvider new];
emailItem.subject = [NSString stringWithUTF8String:subject];
emailItem.body = [NSString stringWithUTF8String:text];
[items addObject:emailItem];
}
else if( strlen( text ) > 0 )
[items addObject:[NSString stringWithUTF8String:text]];
// Credit: https://forum.unity.com/threads/native-share-for-android-ios-open-source.519865/page-13#post-6942362
if( strlen( link ) > 0 )
{
NSString *urlRaw = [NSString stringWithUTF8String:link];
NSURL *url = [NSURL URLWithString:urlRaw];
if( url == nil )
{
// Try escaping the URL
if( CHECK_IOS_VERSION( @"9.0" ) )
{
url = [NSURL URLWithString:[urlRaw stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
if( url == nil )
url = [NSURL URLWithString:[urlRaw stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
}
else
url = [NSURL URLWithString:[urlRaw stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
if( url != nil )
[items addObject:url];
else
NSLog( @"Couldn't create a URL from link: %@", urlRaw );
}
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]];
}
UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
if( strlen( subject ) > 0 )
[activity setValue:[NSString stringWithUTF8String:subject] forKey:@"subject"];
else if( [items count] == 0 )
{
NSLog( @"Share canceled because there is nothing to share..." );
UnitySendMessage( "NSShareResultCallbackiOS", "OnShareCompleted", "2" );
return;
}
if( CHECK_IOS_VERSION( @"8.0" ) )
{
activity.completionWithItemsHandler = ^( UIActivityType activityType, BOOL completed, NSArray *returnedItems, NSError *activityError )
{
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 );
};
}
else if( CHECK_IOS_VERSION( @"6.0" ) )
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
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 );
};
#pragma clang diagnostic pop
}
else
UnitySendMessage( "NSShareResultCallbackiOS", "OnShareCompleted", "" );
UIViewController *rootViewController = UnityGetGLViewController();
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) // iPhone
{
[rootViewController presentViewController:activity animated:YES completion:nil];
}
else // iPad
{
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];
}
}