Simple way to add Facebook/Twitter share to app? - ios

I'm looking for a drop in solution to add a Facebook and Twitter share button option to my app. I've googled this of course, and found shareKit but it's non-ARC so just throws lots of errors, which is of no use to me. Any other options? they need to be ARC compatible.
I want a SDK that I can drop in, throw in the App ID's and be done.

If you want simple sharing, the following can be used for Twitter and Facebook:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slComposeViewController setInitialText:#"Your text"];
[slComposeViewController addImage:[UIImage imageNamed:#"NameOfImage"]];
[self presentViewController:slComposeViewController animated:YES completion:nil];
} else {
//Show alert or in some way handle the fact that the device does not support this feature
}
For Facebook, simply replace SLServiceTypeTwitter with SLServiceTypeFacebook. The "Social" framework must be imported :-)

You can use UIActivityViewController:
NSArray * activityItems = activityItems = #[#"title", [NSURL URLWithString:#"urlstring"]];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
References : https://developer.apple.com/library/ios/documentation/uikit/reference/UIActivityViewController_Class/Reference/Reference.html

Yes use the native iOS sharing dialog: UIActivityViewController

Related

Facebook messanger missing in UIActivityViewController options

I am using UIActivityViewController to share some data on different social media options.
I can see Facebook but i am unable to see Facebook messenger. Does any body have any idea why i doesn't appear in the options list?
I have both Facebook and Facebook messenger installed in my iPhone.
UIActivityViewController * shareViewController = [[UIActivityViewController alloc] initWithActivityItems:#[activity] applicationActivities:nil];
if(self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular && self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular) {
shareViewController.popoverPresentationController.barButtonItem = self.shareButton;
}
[self presentViewController:shareViewController animated:YES completion:^{
}];
Any help will be great thanks

Adding iOS app to Twitter accounts in settings??

I am adding some sharing in my app to allow the use to post to FaceBook and to Twitter. I decided to use the Social Sharing framework as it does exactly what I need and works well.
However I noticed that my app doesn't ask the user for permission it assumes it has permission and does a check if there is an account setup.
The problem I see here is due to this - my app doesn't appear in the Settings - > Twitter under "Allow these apps to use your account" section.
Her is the code I use when I want to share an item - user taps on a UIButton which presents a UIActionSheet with various options"
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler completionBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled){
NSLog (#"Cancelled");
}else {
NSLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:nil];
};
controller.completionHandler = completionBlock;
NSURL *url = [NSURL URLWithString:self.item.url];
/* Adding the text to the Tweet */
[controller setInitialText:#"Online now!"];
[controller addURL:url];
[controller addImage:self.imageView.image];
[self presentViewController:controller animated:YES completion:nil];
}
else {
[self showUserAlert:#"" message:#"Please make sure your FaceBook account has been setup on this device"
delegate:self
cancelButtonTitle:#"Okay"];
}
How do I ask for access first? And add the app to the settings part of iOS so the user can turn it on or off?
I think you don't need a permission because your app can't post without user action. User have all control.
You can check if sharing possible with canSendTweet method in TWTweetComposeViewController. Look up Xcode documentation. I'm sure Apple has something similar for Facebook.
Apple documentation

iOS6 native facebook share dialog url

I have used a native post dialog and I am passing an URL to the method that actually displays the dialog.The URL actually gets posted to the Facebook as needed.But I don't want this URL to be shown in post dialog, because if the user modifies it my mistake then some wrong text gets posted.Is there anyway hiding the URL in dialog. I am using the method presentShareDialogModallyFrom:initialText:image:url:handler: to present the native post dialog.
Unfortunately, you don't show any code so we don't know how you pass the URL. You probably use the setInitialText: method, whereas you need the addURL: method.
SLComposeViewController *facebookController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookController setInitialText:self.titel];
[facebookController addURL:[NSURL URLWithString:self.guid]];
[self presentViewController:facebookController animated:YES completion:nil];
Facebook native share, you have to implements Accounts Framework in to your app
In your framwork you have to Add: Social.framework
In your File .h add:
#import "Social/Social.h"
#interface UIViewController {
SLComposeViewController *FBCompose;
}
In your file .m under a IBAction add:
- (IBAction)facebook:(id)sender {
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"imageURL"]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
//----here you can get pragmaticaly a part of yout text
NSString *mutableArry = (self.myMutableDictionay)[#"string"];
FBCompose = [[SLComposeViewController alloc]init];
FBCompose = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[FBCompose setInitialText:[NSString stringWithFormat:#"Ishare on FB %# from MyApp App", mutableArry]];
[FBCompose addImage:image];
[FBCompose addURL:[NSURL URLWithString:#"http://Link of my app or get the link by myMutableDictionay"]];
[self presentViewController:FBCompose animated:YES completion:NULL];
}
Edit my post because is incomplete sorry.
Hope this help you ;)

standard way to implement share as function

I need to implement a "share as..." function in iOS.
For example a button names "share as..." and popup a dialog which includes items like Email, SMS, Facebook, Twitter.
I wonder if there have a standard dialog do this job.
After searching, I found a way seems quite "standard" way in iOS6 by using UIActivityViewController.
Following is the description from developer.apple.com:
The UIActivityViewController class is a standard view controller that you can use to offer various services from your application. The system provides several standard services, such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS, and more. Apps can also define custom services.
And following is a dialog I managed to produce by using UIActivityViewController
And following is the source code I use:
NSArray *activityItems = [NSArray arrayWithObjects: share_text.text, share_image.image , nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
In your ViewController.m :
NSString *message = #"The Flyer http://flyerdream.tumblr.com";
UIImage *image = [UIImage imageNamed:#"flyer"];
NSArray *arrayOfActivityItems = [NSArray arrayWithObjects:message, image, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:arrayOfActivityItems applicationActivities:nil];
[self.navigationController presentViewController:activityVC animated:YES completion:nil];
After using UIActivityViewController, I believe it is a much better solution than what I've posted below. I've left my answer because the individual sharing methods may be of use to someone.
This is how I've implemented share features in my projects:
Email: Use MFMailComposeViewController. See documentation here: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html
Facebook & Twitter: Use SLComposeViewController. See documentation here: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html
Example:
// prepare and present SLComposeViewController if service is available
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *socialPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[socialPost setInitialText:#"TEXT FOR POST"];
[socialPost addImage:[UIImage imageNamed:#"yourImage.png"]];
[self presentViewController:socialPost animated:YES completion:nil];
}
// set up a completion handler
[socialPost setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultDone:
// post was completed
break;
case SLComposeViewControllerResultCancelled:
// post was cancelled
break;
default:
break;
}
}
Note: If you want to post for Twitter, replace SLServiceTypeFacebook with SLServiceTypeTwitter in the code sample above.

Settings button of iOS Facebook SDK 3.1 not working

I have done all steps that are explained here: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/3.1/
I have tested the demo projects and fail the same as on my project. (you can test it on HelloFacebookSample project).
When you don't have any Facebook account configured, you can't share something on facebook, or upload an image etc, (the same as twitter framework). So the frameworks shows you a message that tells this:
There are no Facebook accounts configured. You can add or create a Facebook account in Settings.
You click on settings but the only thing that happends is that this dialog is hided, but the framework doesn't open the Settings tab (as work on for example on the twitter framework).
Does anyone know how to solve this problem?
Thanks in advance.
Please use SLComposeViewController class for Facebook sharing, setting button work for you.
if([SLComposeViewController class])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:text];
[controller addURL:[NSURL URLWithString:encod]];
[self presentViewController:controller animated:YES completion:Nil];
//[message release];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result)
{
NSString *output= nil;
switch (result)
{
case SLComposeViewControllerResultCancelled:
output= #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output= #"Post Succesfull";
[self displayText:#"done"];
break;
default:
break;
}
};
controller.completionHandler =myBlock;
}
This code is working for me
Why dont u use UIActivityViewController ?
iOS 6 have the best support with UIActivityViewController
UIActivityViewController *ActivityView = [[UIActivityViewController alloc] initWithActivityItems:Items applicationActivities:nil];
Best Integration for Facebook, Twitter and sms Service

Resources