Pregunta

My app support iOS 5+. Now I want to add facebook and twitter. I added social.framework as optional and on facebook btn i check

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        NSLog(@"Facebook iOS 6 avaliable");
}
else
{
 NSLog(@"Feature not supported"); // for iOS 5 users
}

same for twitter. But iOS 6 Simulator works fine but iOS 5 simulator gives error. any help or tutorial for adding facebook and twitter share on iOS 5 and iOS 6 both.

EXC_BAD_ACCESS (code=2, address=0x0) isAvailableForServiceType:SLServiceTypeFacebook
isAvailableForServiceType:SLServiceTypeTwitter

after updating project for answer 1 go this error.

enter image description here here is prefix file enter image description here

¿Fue útil?

Solución

EXC_BAD_ACCESS (code=2, address=0x0)

So this error is a "segmentation fault", because the Social framework is an iOS6-only framework. Since you used weak linkage (that's what adding the framework as "optional" means), the SLComposeViewController class is Nil (i. e. it actually points to the invalid memory address 0x0), so any function you will try to call on it will most likely result in a segmentation fault (since dereferencing this address is not possible).

What you have to do is check this class to be a valid pointer:

if (NSClassFromString(@"SLComposeViewController") != Nil) {
    // iOS 6
} else {
    // iOS 5, Social.framework unavailable, use Twitter.framework instead
}

Otros consejos

The Social framework (SLComposeViewController) was only introduced in iOS 6. In iOS 5 the only native connection to any social network was Twitter and the TWTweetComposeViewController class. iOS 6 introduced the Social framework with pre-existing Twitter support and new Facebook and Sina Weibo integration.

So on iOS 5 you cannot actually make any reference or call to SLComposeViewController, you will need to use conditions to see which version (iOS 5 or 6) the user's device is running then make any operations/conditions.

The code if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) is used to see if the user has setup a Facebook account in the Settings application.

Yes, so you add the Twitter framework for iOS 5 compatibility, make sure Social framework is set to optional by the way.

To check what version the device is running, add this to your MyApp-Prefix.pch file:

#define SYSTEM_VERSION_EQUAL_TO(v)         ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)       ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)         ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)   ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

Then you can use it in any class because the prefix file is automatically imported to all classes:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0") && SYSTEM_VERSION_LESS_THAN(@"6.0")) {

       NSLog(@"This is called when device is running iOS 5, 5.0.1, 5.1 etc.");
}
else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {

       NSLog(@"iOS 6.0, 6.0.1, 6.0.2, 6.1 etc.");
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top