iOS Button that performs multiple actions - ios

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 !

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];
}

How to get only action button share facebook from UIActivityViewController

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];
}

SLComposeViewController - "Attempt to present" errors in iOS 8

Updating some code for iOS 8. I ran into this error on an iPad only app which worked flawlessly in iOS 7. I'm using a UIActionSheet to present options of share options, Facebook and Twitter are giving me heck.
SLComposeViewController *facebookShare = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
//I add an image if there is one
//I set some initial text
[self presentViewController:facebookShare animated:YES completion:nil];
Pretty straight forward stuff. But on iOS 8 I get the below
Warning: Attempt to present SLComposeViewController: 0x1a238760> on
PostDetailViewController: 0x180c5200> which is already presenting
(null)
I've seen this error below and I get what its all about. However its saying my PostDetailViewController is already presenting (null)?!? So basically its already busy with nothing?
I've attempted to present the SLComposeViewController from other VC's in the hearty but I keep getting the Attempt to present error. I've tried presenting from
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController , and self.navigationController to no avail. I've also tried pushing the SLComposeViewController from self.navigationController which actually works but gives undesired results as it pushes an empty background then stays there until SLComposeViewController is closed and the user presses back.
Any leads would be great! Thanks in advance.
EDIT 1:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)[self shareWithTwitter];
if (buttonIndex == 1)[self shareWithFacebook];
if (buttonIndex == 2)[self shareWithiMessage];
if (buttonIndex == 3)[self shareWithEmail];
if (buttonIndex == 4)[self SaveImageToCameraRoll];
}
-(void)shareWithFacebook{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
//add animal URL
SLComposeViewController *facebookShare = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookShare addURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.somelink.com/detail.aspx?id=%#",theAnimal.AnimalID]]];
//add image if there is one
if (theAnimal.PhotoUrl) {
[facebookShare addImage:imageView1.image];
}
//set initial text
if ([theAnimal.Sex isEqualToString:#"Male"]) {
[facebookShare setInitialText:[NSString stringWithFormat:NSLocalizedString(#"pop_his_fb_share", nil),theAnimal.Name]];
}else [facebookShare setInitialText:[NSString stringWithFormat:NSLocalizedString(#"pop_her_fb_share", nil),theAnimal.Name]];
[self presentViewController:facebookShare animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:NSLocalizedString(#"alert", nil)] message:[NSString stringWithFormat:NSLocalizedString(#"details_no_fb_account", nil)] delegate:nil cancelButtonTitle:[NSString stringWithFormat:NSLocalizedString(#"dismiss", nil)] otherButtonTitles:nil, nil];
[alert show];
}
}
I found out that in iOS 8 ActionSheet is actually considered a ViewController (at least I didn't know if it was before), hence the error message. The ActionSheet is attempting to be dismissed when a button is clicked on the delegate method I was using:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
This delegate gets me the button index on click however the ActionSheet is still visible. When a click occurs the presenting ViewController attempts to dismiss the ActionSheet and I get my error.
I switched to:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
(Note: didDismiss) So my sharing methods are not called until the presenting ViewController has dismissed the ActionSheet, then it is free to present the share ViewController!
Hope this helps someone else. Cheers!
Just try following thing.
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:fbViewController animated:YES completion:nil];
}];

E-mail account information with QLPreviewController

I am using QLPreviewController for showing the pdf. And I am sending pdf by tapping the share button and then tap on email in the QLPreviewController.
But i don't know how to get that share button method in QLPreviewController to validate that the email account is available or not. Here is the screenshot:
Please let me know about this.
Thanks in advance.
if you have UInavigationController then you can add a bar button for sharing the content. Or you can add a share button in your viewcontroler.
Use MessageUI. Add the delegates MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate.
- (IBAction)contactBtn:(id)sender {
if ([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[controller setSubject:#"Subject"];
[controller setMessageBody:#" " isHTML:NO];
[controller setToRecipients:[NSArray arrayWithObjects:#"a#wa.com",nil]];
controller.mailComposeDelegate = self;
[self presentViewController:controller animated:YES completion:NULL];
}
else{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Failed!" message:#"Mail can not be sent. Check your email settings." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil] ;
[alert show];
}
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:NULL];
}
The QL previewer is working on separate process. Something like a different app. So there is no way to customize the share button. see details here
This may help you. See this answer.

How to add url for SLComposeViewController in iOS7?

In my app i'm using SLComposeViewController to send tweets. I'm also calling its method addURL: like this:
[tweetSheet addURL:[NSURL URLWithString:#"http://itunes.com/apps/MyAppName"]];
and it works fine in iOS 6, but in iOS 7 it opens iTunesStore right after being presented on the screen. How do i fix it?
UPDATE:
if ([SLComposeViewController isAvailableForServiceType:network])
{
AppController *appController = (AppController *)[[UIApplication sharedApplication] delegate];
MyNavigationController *navController = appController.navController;
UIViewController *currentController = [[navController viewControllers] lastObject];
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:network];
[tweetSheet setInitialText:text];
[tweetSheet addImage:[UIImage imageNamed:temp_character]];
[tweetSheet addURL:[NSURL URLWithString:#"http://itunes.com/apps/MyAppName"]];
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result){
[currentController dismissViewControllerAnimated:YES completion:nil];
if (result == SLComposeViewControllerResultDone)
[[NSNotificationCenter defaultCenter] postNotificationName:HC_TWEET_SENT_NOTIFICATION object:nil];
};
[currentController presentViewController:tweetSheet animated:YES completion:nil];
}
Your custom socialIntegration class has a bug in it. It only returns a SLComposeViewController if Facebook is available on the device. If it isn't, it returns nothing.
However, you don't test for this when you actually call it:
SLComposeViewController * faceSheet=[self.socialIntegration showFacebook:#"text" andImage:nil andLink:#"link" andView:self];
dispatch_sync(dispatch_get_main_queue(), ^{
//[self netConnectionTrue:cell Connected:answer];
//[tempAlertView show];
[self presentViewController:faceSheet animated:YES completion:NO];
});
...you're not checking to see if faceSheet is nil. So if there's no Facebook account you call presentViewController with a nil object, which triggers the error you're seeing.
The reason you are seeing this on iOS 7 is your linked FB accounts probably got reset, but it was probably a source of crashes for your users on iOS 6 as well.

Resources