user3587825
user3587825

Reputation: 121

SLComposeViewController for Twitter now crashing in iOS 8.3

I have a Tweetsheet share option in my app. Just updated to iOS8.3 and now SLComposeViewController is throwing an error when I try to present the Tweetsheet:

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target"

SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@"testing!"];
[self presentViewController:tweetSheet animated:YES completion:nil];

I'm checking beforehand whether Twitter is available. Anyone else have this problem now?

Upvotes: 3

Views: 1822

Answers (2)

Theo
Theo

Reputation: 3995

I have the same problem when using SLServiceTypeTwitter on an iPhone running iOS 8.3: Although

[SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter] 

returns YES, a subsequent call to

[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]

returns nil. However, it returns a view controller if I'm running the app on an iPad, or on the iPhone Simulator, or if I change the service type to SLServiceTypeFacebook.

Now I'm using this workaround: To check Twitter availability, I use

+(BOOL)twitterAvailable {
    return([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter] &&
           [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter] != nil);
}

I further add an additional check before presenting the SLComposeViewController:

if(composeViewController != nil) {
   [viewController presentViewController:composeViewController animated:YES completion:nil];
}

This should be future proof for a bug fix in iOS 8.4.

Upvotes: 2

Alex Cio
Alex Cio

Reputation: 6052

Before using one of the services you always should check if the object can be accessed:

if( [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter] ){

This is really important so

  1. your app is not crashing and
  2. you can tell the user the service is not available.

You should do the same for the other services which can be used from iOS, like for SMS or Mail:

if(![MFMessageComposeViewController canSendText]) {

OR

if( ![MFMailComposeViewController canSendMail] ){

If you want to be sure, a specific app is available on the phone, you could also try to use canOpenURL::

- (BOOL)isAppAvailable:(NSString *)appURL{
   return [[UIApplication sharedApplication]  
                canOpenURL:[NSURL URLWithString:appURL]];
}

I think the call isAvailableForServiceType:SLServiceTypeTwitter is executing something similar.

Upvotes: 0

Related Questions