How to get only action button share facebook from UIActivityViewController - ios

I have a case follows:
In app, i create a UIButton, when click will show a popup share facebook like UIActivityViewController. But i don't known get popup share facebook from UIActivityViewController. I need help everybody.
THANK YOU SO MUCH !!!

You need to create a UIActivityViewController and specify the items you wish to share:
- (IBAction)buttonPressed:(id)sender
{
NSString *textToShare = #"Put text here";
UIImage *imageToShare = _img;
NSArray *itemsToShare = #[textToShare, imageToShare];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
[self presentViewController:activityViewController animated:TRUE completion:nil];
}
If you wish to exclude any activity types you can do so by adding:
// add an array of activity types to exclude
activityViewController.excludedActivityTypes = #[UIActivityTypeMail, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];
before presenting the activityViewController.
Also, to see the "share on Facebook" option, you need to be logged in to Facebook on the device, as stated in this answer.
///
Option 2 - without UIActivityController
If you do not want to use the Activity Controller, but instead just directly open the iOS social sharing dialog, you can do that by importing the Social framework and then using the Social Compose View Controller (SLComposeViewController)
// import Social framework
#import <Social/Social.h>
// check if there is an account for Facebook
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *fbController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
// set up a completion handler (optional)
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
break;
case SLComposeViewControllerResultDone:
break;
}};
[fbController addImage:imageToPost];
[fbController setInitialText:textToPost];
[fbController addURL:urlToPost];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}

Related

Trying to dismiss View Controller that presents SLComposeViewController after user posts to FB in iOS

I am presenting an SLComposeViewController to post to Facebook in my app. The user is able to dismiss this View Controller in one of two ways: either by posting their post to Facebook, or by pressing "cancel". When the user presses "cancel", the SLComposeViewController is dismissed, and the user is returned to the presenting View Controller that is behind it.
However, what I would like to do is if the user presses "post", then I want the presenting View Controller to ALSO be dismissed after the SLComposeViewController is dismissed (i.e. in the SLComposeViewControllerResultDone case). My problem is that I am not sure how to do this. I realize that I would use the completion handler for this, but I am stuck here. Here is the code that I have which presents the SLComposeViewController:
SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbSheet setInitialText:initialText];
[fbSheet addImage:myImage];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result) {
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
}
break;
}
};
[fbSheet setCompletionHandler:completionHandler];
[self presentViewController:fbSheet animated:YES completion:nil];
With the completion handler above, I get the NSLog outputs as expected. However,
Can anyone see what it is I'm doing wrong? As I've pointed out, I need the dismissal of the presenting View Controller to occur ONLY if the user "posts" to Facebook, but NOT when they cancel.
You could simply dismiss the presenting view controller in SLComposeViewControllerResultDone part of completionHandler as below:
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
If you are supporting iOS 6, then you need to dismiss the SLComposeViewController first.
you don't need to dismiss ViewController in the completion handler, it will be handled when you press cancel button
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController * fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:initialText];
[fbSheet addImage:myImage];
[self presentViewController:controller animated:YES completion:Nil];
}

ActivityItems in UIActivityViewController don't show up

I'm developing an app in iOS 7 and in it I need to show an activity controller. Below is my code,
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[UIActivityTypeSaveToCameraRoll, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePrint] applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
But when its presented, this is how it looks.
None of the activity types I added in the array shows up. Why is this happening? Am I missing something? I'd like some help to get this working. I especially need the Facebook, twitter sharing and the saving to the local storage options here.
Thank you.
Edit: I checked on a real device and the Facebook and Twitter sharing options show up. However UIActivityTypeSaveToCameraRoll, UIActivityTypePrint still aren't showing up.
The Items will show up only if you have integrated them to your Device.
If you have integrated facebook and Logged into facebook in the settings then Facebook will appear along the Items. That means your device must be Synchronised with the native Facebook, Twitter, etc Apps for them to show up in the UIActivityViewController
Edit:
Try using like this
UIActivityViewController *ActivityView;
ActivityView =
[[UIActivityViewController alloc]
initWithActivityItems:Items applicationActivities:nil];
[self presentViewController:ActivityView animated:YES completion:nil];

Recreate/Use the default iPhone Photo app save menu

I want to save a photo with the same options given by the default iPhone photo app as seen in this picture:
You can do this using UIActivityViewController. You can create a UIActivityViewController, pass it an image, or the URL to an image, and optionally tell it to exclude certain activities that you don't want to appear. Then, present it, as you would any other viewController.
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[ myImage ] applicationActivities:nil];
activityViewController.excludedActivityTypes = #[ UIActivityTypeAssignToContact ];
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed){
// handle completion
};
[self presentViewController:activityViewController animated:YES completion:nil];
Check out the documentation for more info.

iOS Button that performs multiple actions

I have a button at the end of a guide that says Done!I need it to bring up Facebook posting dialogue and also segue back to the menu screen
At the moment I have it opening a Facebook posting screen, which pops up and goes back down when you click post or cancel. I would like it to return to the menu after the facebook process has completed. I have followed a guide on how segue programatically and have added the code to my current postToFacebook IBAction but it didn't work. I then tried to create it's own IBAction and link it to the button but it didnt work either.
Does anyone know how I can get it to segue after the Facebook dialogue
Heres my code for facebook posting in case you need it
- (IBAction)PostToFacebook:(id)sender {
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:#"I just followed a guide on this App - Get it on the App Store http://"];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
And heres the code for the programatic segue
-(IBAction)nextView{
[self performSegueWithIdentifier:#"next" sender:self]; }
You can put all code in a single -(IBAction) and use a boolean value that changes it's value after executing facebook action.
Something like this,
- (IBAction)PostToFacebook:(id)sender {
if(valueOfBoolean==TRUE){
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:#"I just followed a guide on this App - Get it on the App Store http://"];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
valueOfBoolean=FALSE;
}
else{
[self performSegueWithIdentifier:#"next" sender:self];
}
}
Set default value for valueOfBoolean as TRUE in viewDidLoad method.
EDITED :
- (IBAction)facebook:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebook = [[SLComposeViewController alloc]init];
facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebook setInitialText:[NSString stringWithFormat:#"your string"]];
[self presentViewController:facebook animated:YES completion:nil];
[facebook setCompletionHandler:^(SLComposeViewControllerResult result){
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
// here you can handle your seque, or your custom action what you want
break;
case SLComposeViewControllerResultDone:
// here you can handle your seque, or your custom action what you want
break;
default:
break;
}
// [self dismissViewControllerAnimated:YES completion:nil];
}];
}
else{
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:#"No Facebook Account" message:#"There are no Facebook accounts configured. You can configure or create accounts in settings ." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alertView show];
}
}
I hope it helps !

UIActivityViewController: PostToTwitter: text can´t be changed in presented modal View

i have a problem with iOS 6´s UIActivityViewController. I want to share an image and a text.
When i share it via Facebook, the text is editable in the modal view. In Twitter, i can't change the text. is there a way to change it for Twitter, so that the text is editable ?
This is my code:
NSArray *activityItems = #[#"Test Text",resultImage];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeMessage,UIActivityTypePostToWeibo];
[self presentViewController:activityVC animated:YES completion:nil];

Resources