iosobjective-csocial-frameworkslcomposeviewcontroller

Will SLComposeViewController cause crash if SLServiceTypeFacebook used in country that doesn't support facebook?


I'm using SLComposeViewController to present a very simple share to facebook prompt on the system level. I want the device to handle login via the settings if they are not logged in, so I leave off the check +isAvailableForServiceType and just go ahead and present the SLComposeViewController.

I noticed that if I try using a service type that isn't on my device (like SLServiceTypeTencentWeibo) it causes my program to crash. Will this similarly happen in a country where facebook is not on the device, similar to how Tencent Weibo is not on my device?

The crash I get...

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

I am presenting the SLComposeViewController like so...

SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTencentWeibo];
[controller addURL:[NSURL URLWithString:@"http://www.example.com"]];
[controller addImage:sharedImage];
NSString *postString = [NSString stringWithFormat:@"A cool sharing string!"];
[controller setInitialText:postString];
[self presentViewController:controller animated:YES completion:nil];

Solution

  • I assume it will. The best way to get around this problem is avoid checking isAvailableForServiceType: and instead check if instantiated controller is nil. This way you still get the option to go to Settings to create an account in most cases and don't get random crashes when you can't.

    Here is my code for Sina Weibo (text in the alert view is equal to the text that Apple presents normally, just without the option to go to Settings):

    SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    [controller setInitialText:@"Some random text"];
    [controller addImage:_randomImage];
    
    if (!controller) {
        [[[UIAlertView alloc] initWithTitle:@"No Sina Weibo Accounts" message:@"There are no Shina Weibo accounts configured. You can add or create a Sina Weibo account in Settings." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show];
        return;
    }
    
    [self presentViewController:controller animated:YES completion:nil];