Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
    }   
}
like image 546
kev Avatar asked Oct 24 '14 03:10

kev


1 Answers

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!

like image 59
kev Avatar answered Nov 03 '22 12:11

kev