1
votes

Hy, i am trying to make from Facebook IOS SDK (Octomber 2011) a class of my own in where i can make all the methods and functions that i need and just import this class in any of my application view controllers and call it. So this way i don't need to do all the initialization and delegation to the controllers in where i use the facebook. So i want to do like this, [myfacebookclass initwithappID:myappid] [myfacebookclass postmessage:@"some message"];

The thing is when i am seting authorizeWithFBAppAuth:YES safariAuth:YES flags in facebook.m ( the original class) and i do a postmessage it redirects me on Facebook Application or Safari and check's for permision, i hit ok and then redirects me in my application and doesn't post message.

WHen i am trying to post message i do like this :

This is in my own facebook class

myfacebookclass *facebook2;
+(void)initWithAppID:(NSString*) facebookAppId {
    if (facebook2 == nil) {
        facebook2 = [myfacebookclass new];
        [facebook2 setWithAppId:facebookAppId];        
    }
}

- (void) postMessage1:(NSString *)messageToPost {  
    if ([self.facebook isSessionValid]) {        
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       self.applicationID, @"api_key",
                                       messageToPost,  @"message",
                                       nil];
        [self.facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
    } else {
        NSArray * neededPermissions = [[[NSArray alloc] initWithObjects:@"user_about_me", @"publish_stream", nil] autorelease];
        [self.facebook authorize:neededPermissions];    
    }
}

+ (void)postMessage:(NSString*) message {
        [facebook2 postMessage1:message];
}

I also implemented:

  • (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [facebook handleOpenURL:url]; }

i've didn't modified facebook.m or other classes in Facebook IOS SDK.

1
This is not all my class, is just some methods, i've implemened here all the steps from linkMihai Georgescu

1 Answers

3
votes

a good practice when making a singleton class is to use shared instance. Correct singleton also has to include some memory management overridden methods. In your case it can be done like this:

@implementation FacebookManager

static FacebookManager *mySharedFacebookManager;

+ (FacebookManager*)sharedFacebookManager {
    @synchronized(self) {
        if(mySharedFacebookManager == nil) {
            mySharedFacebookManager = [[self alloc] init];
        }
    }
    return mySharedFacebookManager;
}


+ (id)allocWithZone:(NSZone *)zone {    
    @synchronized(self) {
        if(mySharedFacebookManager == nil) {
            mySharedFacebookManager = [super allocWithZone:zone];   
            return mySharedFacebookManager;
        }
    }
    return nil;
}


- (id)copyWithZone:(NSZone *)zone {
    return self;    
}


- (id)retain {  
    return self;    
}


- (unsigned)retainCount {
    return UINT_MAX;
}


- (oneway void)release {

}


- (id)autorelease {
    return self;    
}

In your case you may have Facebook type object as ivar, and some methods to wrap over FB API, for example like this:

- (id)init {
 self = [super init];
 if(self) {
    facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
// restore previous session
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    ...
 }
 return self;
}

- (void)login {
    NSArray *permissions = [NSArray arrayWithObjects: @"user_about_me", @"publish_stream", nil];
    [facebook authorize:permissions];
}

- (void)postToFeed:(NSString*)messageToPost {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   messageToPost, @"message",
                                   nil];


    [facebook requestWithGraphPath:@"me/feed"
                         andParams:params
                     andHttpMethod:@"POST"
                       andDelegate:self];
}

From other classes which have to deal with Facebook the construction is simple:

[[FacebookManager sharedFacebookManager] login];

...

[[FacebookManager sharedFacebookManager] postToFeed:@"Hello World!"];

I hope this snippets will help you.

To manage your facebook session you have to remember accessToken manually in FBSessionDelegate methods like that:

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}


- (void)fbDidLogout {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}