Monday, 2 February 2015

Objective C + Social Integration (Facebook, Twitter, Clipboard, Mail, Message, WhatsApp)

// SocialIntegration.h

#import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> #import <MessageUI/MessageUI.h> #import <Social/Social.h> #import <Twitter/Twitter.h> #import "WhatsAppKit.h" @interface HomeViewController : UIViewController<MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate> { } -(void)openFB:(id)sender; -(void)openTW:(id)sender; -(void)copyTextIntoClipboard:(id)sender; -(void)openMail:(id)sender; -(void)openMessage:(id)sender; -(void)openWhatsApp:(id)sender; @end








// SocialIntegration.m

#define kMsgNOFbLogin @"There are no Facebook accounts configured. Please set up your Facebook account in your phone's settings." #define kMsgNOTwitterLogin @"There are no Twitter accounts configured. Please set up your Twitter account in your phone's settings." #define kError_NotSupportFeature @"Your device doesn't support this feature" // Facebook Integration - (void)openFB:(id)sender { if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { SLComposeViewController *fbController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; [fbController setInitialText:@"Text Here"]; [fbController addURL:[NSURL URLWithString:@"Url String Here"]]; UIImage *image = [UIImage imageNamed:@"logo.png"]; [fbController addImage:image]; [self presentViewController:fbController animated:YES completion:nil]; [fbController setCompletionHandler:^(SLComposeViewControllerResult result) { NSString *output; switch (result) { case SLComposeViewControllerResultCancelled: output = @"Action Cancelled"; break; case SLComposeViewControllerResultDone: output = @"Message posted to your Facebook network"; break; default: break; } [self dismissViewControllerAnimated:YES completion:nil]; //check if everythink worked properly. Give out a message on the state. [self.view makeToast:output duration:3.0 position:@"center"]; }]; } else { [self.view makeToast:kMsgNOFbLogin duration:3.0 position:@"center"]; } } // Twitter Integration -(void)openTW:(id)sender { if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) { SLComposeViewController *twitterController = twitterController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; [twitterController setInitialText:@"Text Here"]; [twitterController addURL:[NSURL URLWithString:@"Url String Here"]]; [self presentViewController:twitterController animated:YES completion:nil]; UIImage *image = [UIImage imageNamed:@"logo.png"]; [twitterController addImage:image]; [twitterController setCompletionHandler:^(SLComposeViewControllerResult result) { NSString *output; switch (result) { case SLComposeViewControllerResultCancelled: output = @"Action Cancelled"; break; case SLComposeViewControllerResultDone: output = @"Message posted to your Twitter network"; break; default: break; } //check if everythink worked properly. Give out a message on the state. [self.view makeToast:output duration:3.0 position:@"center"]; [self dismissViewControllerAnimated:YES completion:nil]; }]; } else { [self.view makeToast:kMsgNOTwitterLogin duration:3.0 position:@"center"]; } } //Copy Text to Clipboard -(void)copyTextIntoClipboard:(id)sender { [self trackSocialShareWithLabel:@"Clipboard"]; NSString *clipboardText = @"Text Here"; UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = clipboardText; [self.view makeToast:@"Copied to clipboard" duration:3.0 position:@"center"]; } // Mail Integration -(void)openMail:(id)sender { if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init]; mailComposeViewController.mailComposeDelegate = self; [mailComposeViewController setSubject:@"Smart Shop"]; //NSArray *toRecipients = [NSArray arrayWithObject:@""]; [mailComposeViewController setToRecipients:nil]; NSString *emailBody = [NSString stringWithFormat:@"%@\n%@",kSharedSocialString,kSharedUrlString]; [mailComposeViewController setMessageBody:emailBody isHTML:NO]; mailComposeViewController.navigationBar.barStyle = UIBarStyleBlackOpaque; [self presentViewController:mailComposeViewController animated:YES completion:nil]; } else { [self.view makeToast:kError_NotSupportFeature duration:3.0 position:@"center"]; } } #pragma mark- MFMailComposeViewControllerDelegate Method:: - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { [self dismissViewControllerAnimated:YES completion:nil]; } // Message Integration -(void)openMessage:(id)sender { [self trackSocialShareWithLabel:@"Message"]; if(![MFMessageComposeViewController canSendText]) { [self.view makeToast:kError_NotSupportFeature duration:3.0 position:@"center"]; return; } else { MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; picker.messageComposeDelegate = self; picker.recipients = nil; NSString *msgBody = [NSString stringWithFormat:@"%@\n%@",kSharedSocialString,kSharedUrlString]; picker.body = msgBody; [self presentViewController:picker animated:YES completion:nil]; } } #pragma mark- MFMessageComposeViewControllerDelegate Method:: - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result { switch (result) { case MessageComposeResultCancelled: break; case MessageComposeResultFailed: { [self.view makeToast:@"Failed to send SMS!" duration:3.0 position:@"center"]; break; } case MessageComposeResultSent: break; default: break; } [self dismissViewControllerAnimated:YES completion:nil]; } // WhatsApp Integration
-(void)openWhatsApp:(id)sender { if ([WhatsAppKit isWhatsAppInstalled]) { [WhatsAppKit launchWhatsAppWithMessage:@""]; } else { [self.view makeToast:kError_NotSupportFeature duration:3.0 position:@"center"]; } }



No comments:

Post a Comment