iOSNativeShare.m
6.39 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#import "iOSNativeShare.h"
#import <AssetsLibrary/ALAssetsLibrary.h>
#import <Photos/Photos.h>
@implementation iOSNativeShare{
}
#ifdef UNITY_4_0 || UNITY_5_0
#import "iPhone_View.h"
#else
extern UIViewController* UnityGetGLViewController();
#endif
static NSURL *video;
ALAssetsLibrary* defaultAssetsLibrary() {
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
+(id) withTitle:(char*)title withMessage:(char*)message{
return [[iOSNativeShare alloc] initWithTitle:title withMessage:message];
}
-(id) initWithTitle:(char*)title withMessage:(char*)message{
self = [super init];
if( !self ) return self;
ShowAlertMessage([[NSString alloc] initWithUTF8String:title], [[NSString alloc] initWithUTF8String:message]);
return self;
}
+(id) withTitle:(char*)path{
return [[iOSNativeShare alloc] initWithTitle:path];
}
-(id) initWithTitle:(char*)path {
self = [super init];
if( !self ) return self;
SendToGallery([[NSString alloc] initWithUTF8String:path]);
return self;
}
+(id) withTitleSubtitle:(char*)title withSubtitle:(char*)message{
return [[iOSNativeShare alloc] initWithTitleSubtitle: title withSubtitle:message];
}
-(id) initWithTitleSubtitle: (char*)object withSubtitle:(char*)methodName{
self = [super init];
if( !self ) return self;
AskForPermission( object,methodName);
return self;
}
void AskForPermission( char *object, char *method){
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusAuthorized) {
UnitySendMessage(object, method, "PERMITIDO");
}
else if (status == PHAuthorizationStatusDenied) {
UnitySendMessage(object, method, "NAOPERMITIDO");
}
else if (status == PHAuthorizationStatusNotDetermined) {
// Access has not been determined.
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
UnitySendMessage(object, method, "PERMITIDO");
}
else {
UnitySendMessage(object, method, "NAOPERMITIDO");
}
}];
}
else if (status == PHAuthorizationStatusRestricted) {
UnitySendMessage(object, method, "NAOPERMITIDO");
}
}
void SendToGallery(NSString *path){
// if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path)){
//UISaveVideoAtPathToSavedPhotosAlbum (path,self, nil, nil);
// UISaveVideoAtPathToSavedPhotosAlbum (path,nil, nil, nil);
//ShowAlertMessage(@"Vlibras Vídeo",@"Video salvo na galeria.");
//}
NSURL *videoURL = [NSURL fileURLWithPath:path];
ALAssetsLibrary *assetLibrary = defaultAssetsLibrary();
[assetLibrary writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:^(NSURL *assetURL, NSError *error){
if(error) {
NSLog(@"error while saving to camera roll %@",[error localizedDescription]);
UnitySendMessage("ShareIOS", "SaveGalleryResponse", "ERROR");
} else {
//For removing the back up copy from the documents directory
NSError *removeError = nil;
video = assetURL;
NSLog(@"Path: %@",assetURL);
[[NSFileManager defaultManager] removeItemAtURL:videoURL error:&removeError];
UnitySendMessage("ShareIOS", "SaveGalleryResponse", "SUCESS");
}
}];
}
void ShowAlertMessage (NSString *title, NSString *message){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
+(id) withText:(char*)text withURL:(char*)url withImage:(char*)image withSubject:(char*)subject{
return [[iOSNativeShare alloc] initWithText:text withURL:url withImage:image withSubject:subject];
}
-(id) initWithText:(char*)text withURL:(char*)url withImage:(char*)image withSubject:(char*)subject{
self = [super init];
if( !self ) return self;
//NSMutableArray *items = [NSMutableArray new];
NSArray *postItems = [NSArray new];
postItems = @[video];
UIActivityViewController *activityVc = [[UIActivityViewController alloc]initWithActivityItems:postItems applicationActivities:nil];
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && [activityVc respondsToSelector:@selector(popoverPresentationController)] ) {
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityVc];
[popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)
inView:[UIApplication sharedApplication].keyWindow.rootViewController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:activityVc animated:YES completion:nil];
return self;
}
# pragma mark - C API
iOSNativeShare* instance;
void showAlertMessage(struct ConfigStruct *confStruct) {
instance = [iOSNativeShare withTitle:confStruct->title withMessage:confStruct->message];
}
void sendToGallery(struct ConfigStruct *confStruct) {
instance = [iOSNativeShare withTitle:confStruct->title];
}
void showSocialSharing(struct SocialSharingStruct *confStruct) {
instance = [iOSNativeShare withText:confStruct->text withURL:confStruct->url withImage:confStruct->image withSubject:confStruct->subject];
}
void askForPermission(struct ConfigStruct *confStruct){
instance = [iOSNativeShare withTitleSubtitle:confStruct->title withSubtitle:confStruct->message];
}
@end