显示SLComposeViewController时,iOS应用程序崩溃

时间:2015-07-17 20:58:45

标签: ios objective-c iphone

好的,所以我有一个单一视图的iOS应用程序。在视图控制器内部,我有一个方法附加到故事板中的按钮。这是按下按钮时的方法:

- (IBAction)tweetButton:(id)sender {
  if ([SLComposeViewController isAvailableForServiceType:@"SLServiceTypeTwitter"]) {
    SLComposeViewController *tweetSheet = [[SLComposeViewController alloc] init];
    tweetSheet = [SLComposeViewController composeViewControllerForServiceType:@"SLServiceTypeTwitter"];

    [tweetSheet setInitialText:@"This is a test."];
    [self presentViewController:tweetSheet animated:YES completion:nil];
  }
  else {
    NSLog(@"Twitter not configured.");
  }
}

每当我按下应用程序中的按钮时,我都会遇到以下错误:

2015-07-17 15:57:24.110 Now Playing[425:19583] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x157e4c620>.'

我的代码几乎跟随我在网上看过的每个例子,所以我不确定是什么。

1 个答案:

答案 0 :(得分:1)

即使服用NSString,也不能难以输入服务类型,您必须使用其中一个常量:SLServiceTypeTwitterSLServiceTypeFacebook ......

此外,composeViewControllerForServiceType:已经在为您执行alloc / init步骤,因此在调用此方法之前无需分配它。

代码已修复:

- (IBAction)tweetButton:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        [tweetSheet setInitialText:@"This is a test."];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
    else {
        NSLog(@"Twitter not configured.");
    }
}