Facebook SDK – ShiVa Engine

Facebook SDK

vklymenko: “This Code starts a looped timer with 0.5 delay. It checks each time for a request from shiva/lua to share text on the user’s facebook wall. As soon as the user logs in, the app will automatically post the message on his wall. To post every time and not only once after login, you may substitute “else” for “if (![facebook isSessionValid]) {“. Alternatively, uncomment “//[facebook logout];” to log the user out and share the game on facebook only once.”

SDK Download

Download the latest SDK, for example from here: https://github.com/facebook/facebook-ios-sdk

Xcode Steps

Add the “src” folder (renamed) to your xCode project.

Code modifications

S3DEngine_AppDelegate.h

 #import "FBConnect.h"
 ...
 @interface S3DEngine_AppDelegate : NSObject 
 {
     ...
     Facebook *facebook;
 }
 ...
 @property (nonatomic, retain) Facebook *facebook;
 ...

S3DEngine_AppDelegate.m

 ...
 @synthesize facebook;
 ...
 - (void)applicationDidFinishLaunching:(UIApplication *)application
 {
     ...
     facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self];
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     if ([defaults objectForKey:@"FBAccessTokenKey"]
     && [defaults objectForKey:@"FBExpirationDateKey"]) {
         facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
         facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
     }
     [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onCheckForFB:) userInfo:nil repeats:YES];
 }
 -(void)onCheckForFB:(NSTimer *)timer
 {
     // to handle the request from lua scripts
     if (S3DEngine_WantShareFacebook())
     {
         S3DEngine_SetWantShareFacebookFalse();
         if (![facebook isSessionValid]) {
             NSArray *permissions = [[NSArray alloc] initWithObjects:
                                    @"publish_stream",
                                    nil];
             [facebook authorize:permissions];
             [permissions release];
         }
     }
 }
 // Pre iOS 4.2 support
 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url];
 }
 // For iOS 4.2+ support
 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    UIWindow- window = [UIApplication sharedApplication].keyWindow;
    return [facebook handleOpenURL:url];
 }
 - (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
    NSString *message = @"your message here!";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:message forKey:@"message"];
    [facebook requestWithMethodName:@"facebook.Stream.publish" andParams:params
                      andHttpMethod:@"POST" andDelegate:self];
    //[facebook logout];
}



Need more answers?

  • slackBanner