6

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];
    }   
}
kev
  • 2,306
  • 3
  • 26
  • 31
  • You need to show the whole method that the code you posted is in, so we can tell where you're using it. – rdelmar Oct 24 '14 at 03:49
  • So from the actionSheet delegate method I'm calling `[self shareWithFacebook];` – kev Oct 24 '14 at 04:14
  • I don't see anything wrong with your code. I tried a simplified version of it, and it worked ok for me. Could PostDetailViewController be presenting something else? Do you have any segues set up? – rdelmar Oct 24 '14 at 04:28
  • PostDetailViewController is not presenting anything. Thats why the error message strikes me as so odd ( presenting (null) ). The app also has a Popover VC which it also silently fails to present the Socal with the same error message. – kev Oct 24 '14 at 05:09

2 Answers2

24

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!

kev
  • 2,306
  • 3
  • 26
  • 31
  • 1
    Just ran in to this issue myself - found your solution after a lot of searching. Excellent answer - you should mark it as accepted - Thanks! – markt Dec 02 '14 at 19:52
-1

Just try following thing.

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self presentViewController:fbViewController animated:YES completion:nil];
    }];
nilkash
  • 7,408
  • 32
  • 99
  • 176
  • I will have to give that a shot. I actually found another solution, posted bellow. – kev Oct 29 '14 at 13:27