This is the smallest code to have Facebook with iOS and AppGameKit using only obj C
Adding Frameworks
Ok as we said before you need those LIB to get all working:
- AdSupport (make it optional in build phase or will crash on old ios)
- Social (make it optional in build phase or will crash on old ios)
- Accounts (make it optional in build phase or will crash on old ios)
- Facebook Framework
Build Settings
Use the finder on the right and search for : Other Linker Flags
And add this one : -lsqlite3.0
Application Info PList
Add a row and write down this: FacebookAppID
and in the right side the ID of your application created on Facebook panel.
Now we can go to the code....
UntitledViewController.h
First of all, import the Framework (where you need, i usually import them in the template.h)
#import <FacebookSDK/FacebookSDK.h>
and after that add the delegate to your interface declaration
<FBLoginViewDelegate>
Now some code in the .h
//Facebook
- (void) FBLogin;
- (bool) FBIsLogged;
- (void) FBPostWallScore :(uint64_t)score;
- (void) PostingData :(NSString*)frase;
UntitledViewController.m
The permissions...
//*************************************************************
//This Part goes BEFORE implementation, so outside
// For example under BOOL g_bDisplayLinkReady = FALSE;
//*************************************************************
NSArray *w_permissions = [NSArray arrayWithObjects:@"publish_actions", nil];
NSArray *r_permissions = [NSArray arrayWithObjects:@"email", @"user_photos", @"friends_photos", nil];
Now the real code...
NOME_APPLICAZIONE = APP_NAME (declared in the h)
//**************************************************
// FACEBOOK
//**************************************************
- (void) FBLogin
{
if(![self FBIsLogged]){
[FBSession openActiveSessionWithReadPermissions:r_permissions allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if(session.isOpen && error == nil){
[self showAlertWithTitle:NOME_APPLICAZIONE :@"Facebook login done!"];
} else {
[self showAlertWithTitle:NOME_APPLICAZIONE :@"Facebook login error!"];
}
}];
} else {
[self showAlertWithTitle:NOME_APPLICAZIONE :@"Facebook login already done!"];
}
}
- (bool) FBIsLogged
{
return FBSession.activeSession.isOpen;
}
- (void) FBPostWallScore :(uint64_t)score
{
NSString *frase = [NSString stringWithFormat:@"%s scored %lli points!!!", Opzioni[0].nick.c_str(), score];
if([self FBIsLogged]){
if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
// if we don't already have the permission, then we request it now
[FBSession.activeSession reauthorizeWithPublishPermissions:w_permissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
[self PostingData :frase];
}
}];
} else {
[self PostingData :frase];
}
}
}
- (void) PostingData :(NSString*)frase
{
[FBRequestConnection
startWithGraphPath:@"me/feed"
parameters: [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"https://itunes.apple.com/us/app/xxxxxxxxxxxxxxx", @"link",
@"http://www.xxxxxxxxx.com/xxxxxxx.png", @"picture",
NOME_APPLICAZIONE, @"name",
@"Download it from Apple Store and get some fun!", @"caption",
frase, @"description",
nil]
HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if(error == nil) {
alertText = @"Score posted on your Facebook wall.";
}
[self showAlertWithTitle:NOME_APPLICAZIONE :alertText];
}];
}
If you have question don't hesitate to ask.
Remember the way to call obj c functions from c++ i declared in my template.
[SC FBLogin];
Long life to Steve!