Monday, 14 December 2015

Share Image & Video on Social Media

Share Image & Video on Social Media


1) Import Following Framework :

#import <Social/Social.h>
#import <MessageUI/MessageUI.h>

&

set :-

 <MFMailComposeViewControllerDelegate,UIDocumentInteractionControllerDelegate>



@property (retain) UIDocumentInteractionController * documentInteractionController;


2) Write This Source code in info.plist  :


<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
<string>whatsapp</string>
</array>


3) Write This code For Share Image & Video  :


************** Image & Video Share on Whatsapp **********

-(void)whatsappshare
{
    //new code
    if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]])
    {
        
        if ([SelectedMediaType isEqualToString:@"image"])
        {
            UIImage     * iconImage = self.fullCard.image;
            NSString    * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];
        
            [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
        
            _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
            _documentInteractionController.UTI = @"net.whatsapp.image";
            _documentInteractionController.delegate = self;
        
            [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
        }
        else if ([SelectedMediaType isEqualToString:@"video"])
        {
            NSString    * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wam"];
            
            savePath = strVideoURL ;
            
            _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
            _documentInteractionController.UTI = @"net.whatsapp.movie";
            _documentInteractionController.delegate = (id)self;
            
            [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
            
        }
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}


************** Image & Video Share on Instagram **********

-(void)instashare
{
    if ([SelectedMediaType isEqualToString:@"image"])
    {
        NSString* imagePath = [NSString stringWithFormat:@"%@/instagramShare.igo", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
        [[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
        
        UIImage *instagramImage = self.fullCard.image;
        [UIImagePNGRepresentation(instagramImage) writeToFile:imagePath atomically:YES];
        // NSLog(@"Image Size >>> %@", NSStringFromCGSize(instagramImage.size));
        
        self.documentInteractionController=[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
        self.documentInteractionController.delegate = self;
        self.documentInteractionController.UTI = @"com.instagram.exclusivegram";
        [self.documentInteractionController presentOpenInMenuFromRect: self.view.frame inView:self.view animated:YES ];

    }
    else if ([SelectedMediaType isEqualToString:@"video"])
    {
        NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
        if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
        {
            UISaveVideoAtPathToSavedPhotosAlbum(strVideoURL, nil, nil, nil);
            
            NSURL *videoFilePath = [NSURL URLWithString:strVideoURL];
            NSString *caption = @"Greetings Video";
            
            NSString* escapedString =
            [videoFilePath.absoluteString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
                
            NSString* escapedCaption =
            [caption stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
                
            NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",escapedString,escapedCaption]];
            if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
            {
                    [[UIApplication sharedApplication] openURL:instagramURL];
            }
        }
    }
}

************** Image Share on facebook **********

- (void)postByFb
{
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        
        [controller addImage:self.fullCard.image];

        [self presentViewController:controller animated:YES completion:Nil];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Facebook not installed." message:@"Your device has no Facebook installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

    }
}

************** Image Share on Twitter **********

- (void)postByTwitter
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet addImage:self.fullCard.image];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Twitter not installed." message:@"Your device has no Twitter installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        

    }
}

************** Image & Video Share on Mail **********

- (void)mailRqst
{
    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;
    // [mailComposer setToRecipients:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@" , emailID_str],nil]];
    if ([MFMailComposeViewController canSendMail])
    {
        
        [mailComposer setValue:@"Greetingz - Surprise Inside!" forKey:@"subject"];
        
        if ([SelectedMediaType isEqualToString:@"image"])
        {
        
            NSData *imageData = UIImagePNGRepresentation(self.fullCard.image);
            [mailComposer addAttachmentData:imageData mimeType:@"image/png" fileName:@"Greetingz"];
        
        }
        else if ([SelectedMediaType isEqualToString:@"video"])
        {
            [mailComposer addAttachmentData:[NSData dataWithContentsOfURL:videoURL] mimeType:@"video/MOV" fileName:@"Video.MOV"];
        
            // [mailComposer setSubject:@"Greetingz - Surprise Inside!"];
       
        }
         
        [self presentViewController:mailComposer animated:YES completion:nil];
    }
    
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            //NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            //NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            //NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            // NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error !" message:[NSString stringWithFormat:@"Error %@",[error localizedDescription]] delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil];
            [alert show];
        }
            break;
        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

No comments:

Post a Comment