简体   繁体   English

SLComposeViewController - 在iOS 8中“尝试显示”错误

[英]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. 为iOS 8更新了一些代码。我在iOS 7上运行了这个错误,这个应用程序在iOS 7中完美运行。我正在使用UIActionSheet来显示共享选项的选项,Facebook和Twitter正在给我带来帮助。

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 但是在iOS 8上我得到了以下内容

Warning: Attempt to present SLComposeViewController: 0x1a238760> on PostDetailViewController: 0x180c5200> which is already presenting (null) 警告:尝试在PostDetailViewController上显示SLComposeViewController:0x1a238760>:0x180c5200>已经呈现(null)

I've seen this error below and I get what its all about. 我在下面看到了这个错误,我得到了它的全部内容。 However its saying my PostDetailViewController is already presenting (null)?!? 然而它说我的PostDetailViewController已经呈现(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 . 我试图从其他VC中提供SLComposeViewController,但我不断Attempt to present error I've tried presenting from 我尝试过来

UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController , and self.navigationController to no avail. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController , and self.navigationController无济于事。 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. 我也试过从self.navigationController推送SLComposeViewController实际上有效,但是当它推空背景然后停留在那里直到SLComposeViewController关闭并且用户按下后会产生不希望的结果。

Any leads would be great! 任何线索都会很棒! Thanks in advance. 提前致谢。

EDIT 1: 编辑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. 我发现在iOS 8中,ActionSheet实际上被认为是一个ViewController(至少我不知道它是不是以前),因此出现了错误信息。 The ActionSheet is attempting to be dismissed when a button is clicked on the delegate method I was using: 当我在使用的委托方法上单击按钮时,ActionSheet正在尝试被解雇:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

This delegate gets me the button index on click however the ActionSheet is still visible. 此委托在单击时获取按钮索引,但ActionSheet仍然可见。 When a click occurs the presenting ViewController attempts to dismiss the ActionSheet and I get my error. 当发生单击时,呈现的ViewController会尝试关闭ActionSheet,然后我收到错误消息。

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! (注意: didDismiss )因此,在呈现的ViewController取消ActionSheet之前,我的共享方法不会被调用,然后可以自由地呈现共享ViewController!

Hope this helps someone else. 希望这有助于其他人。 Cheers! 干杯!

Just try following thing. 试试下面的事情。

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self presentViewController:fbViewController animated:YES completion:nil];
    }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM