iOSNativeShare.m 6.39 KB
#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