Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
An Objective-C class that simplifies using the iOS 6 Native social posting dialogs for Facebook and Twitter, plus SMS messaging and E-mail
//
// SimpleSocial.h
//
// Copyright (c) 2013 Erik Ralston
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <Foundation/Foundation.h>
///
/// A simple interface for using the iOS 6 native Facebook and Twitter, plus SMS and E-Mail messaging functionality
///
@interface SimpleSocial : NSObject
+ (BOOL)isTwitterAvailable;
- (void)showTwitterDialog:(NSString *)promptText withViewController:(UIViewController *)viewController;
+ (BOOL)isFacebookAvailable;
- (void)showFacebookDialog:(NSString *)promptText withViewController:(UIViewController *)viewController;
+ (BOOL)isSmsAvailable;
- (void)showSmsDialog:(NSString *)promptText withViewController:(UIViewController *)viewController;
+ (BOOL)isEmailAvailable;
- (void)showEmailDialogWithSubject:(NSString *)subject andBody:(NSString *)body withViewController:(UIViewController *)viewController;
@end
//
// SimpleSocial.m
//
// Copyright (c) 2013 Erik Ralston
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "SimpleSocial.h"
#warning Be sure to include the following frameworks in the project:
// Twitter.Framework
// Social.Framework
// Accounts.Framework
// MessageUi.Framework
#import <Social/Social.h>
#import <MessageUI/MessageUI.h>
@interface SimpleSocial() <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
{
SLComposeViewController* cvc;
}
@end
@implementation SimpleSocial
#pragma mark - Common Social Features
///
/// Returns YES if the given service is available; otherwise, returns NO
///
+ (BOOL)isServiceAvailable:(NSString *)serviceName
{
if( NSClassFromString(@"SLComposeViewController") != nil ) {
return [SLComposeViewController isAvailableForServiceType:serviceName];
}
return NO;
}
///
/// Shows the post dialog for the given service with the given initial text
///
- (void)showDialogForService:(NSString *)serviceName withText:(NSString *)text andViewController:vc
{
if ( [SimpleSocial isServiceAvailable:serviceName] == NO ) {
// Tell the developer that the service is not supported then quietly abort
NSLog(@"%@ is not available.",serviceName);
return;
}
cvc = [SLComposeViewController composeViewControllerForServiceType:serviceName];
__weak UIViewController *controller = cvc;
cvc.completionHandler = ^(SLComposeViewControllerResult result) {
// This can be called on ANY thread, so be save and sync with UI thread if you need to access UI elements
if( result == SLComposeViewControllerResultDone ) {
NSLog(@"%@ Success",serviceName);
} else {
NSLog(@"%@ Cancelled",serviceName);
}
[controller dismissViewControllerAnimated:YES completion:nil];
};
[cvc setInitialText:text];
// Present the social ViewController using the given parent
[vc presentViewController:cvc animated:YES completion:nil];
}
#pragma mark - Implementation for specific social services
///
/// Returns YES if Facebook is available; otherwise, returns NO
/// NOTE: The user has to be logged into their account under "Settings" in iOS for this to return YES
///
+ (BOOL)isFacebookAvailable
{
return [SimpleSocial isServiceAvailable:SLServiceTypeFacebook];
}
///
/// Shows the Facebook dialog with the given initial prompt text
///
- (void)showFacebookDialog:(NSString *)promptText withViewController:(UIViewController *)viewController
{
[self showDialogForService:SLServiceTypeFacebook withText:promptText andViewController:viewController];
}
///
/// Returns YES if Twitter is available; otherwise, returns NO
/// NOTE: The user has to be logged into their account under "Settings" in iOS for this to return YES
///
+ (BOOL)isTwitterAvailable
{
return [SimpleSocial isServiceAvailable:SLServiceTypeTwitter];
}
///
/// Shows the Twitter dialog with the given initial prompt text
///
- (void)showTwitterDialog:(NSString *)promptText withViewController:(UIViewController *)viewController
{
[self showDialogForService:SLServiceTypeTwitter withText:promptText andViewController:viewController];
}
#pragma mark - SMS Methods
///
/// Returns YES if SMS is available on the device; otherwise, returns NO
///
+ (BOOL)isSmsAvailable
{
return [MFMessageComposeViewController canSendText];
}
///
/// Shows the SMS dialog to the current user
///
- (void)showSmsDialog:(NSString *)promptText withViewController:(UIViewController *)viewController
{
// Use the root controller of the app
MFMessageComposeViewController *messageViewController = [[MFMessageComposeViewController alloc] init];
messageViewController.body = promptText;
messageViewController.messageComposeDelegate = self;
[viewController presentViewController:messageViewController animated:YES completion:nil];
}
///
/// Callback for SMS dialog
///
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[controller becomeFirstResponder];
[controller dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - E-Mail Methods
///
/// Returns YES if e-mail is available on the device; otherwise, returns NO
///
+ (BOOL)isEmailAvailable
{
return [MFMailComposeViewController canSendMail];
}
///
/// Shows the E-mail dialog to the current user
///
- (void)showEmailDialogWithSubject:(NSString *)subject andBody:(NSString *)body withViewController:(UIViewController *)viewController
{
// From within your active view controller
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
[mailCont setSubject:subject];
[mailCont setMessageBody:body isHTML:NO];
[mailCont setMailComposeDelegate:self];
[viewController presentViewController:mailCont animated:YES completion:nil];
}
///
/// Callback for mail compose controller
///
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[controller becomeFirstResponder];
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment