开发者

Facebook SDK for iOS, fbDidLogin never called in multi nib application

开发者 https://www.devze.com 2023-03-24 03:20 出处:网络
this is my first time posting as I usually just come by to do research for issues, but this one I cannot figure out myself.

this is my first time posting as I usually just come by to do research for issues, but this one I cannot figure out myself.

Basically my problem is similar to other developers using the facebook sdk for iOS, my fbDidLogin开发者_StackOverflow function never gets called even after I log in and can make my posts to feed successfully. I looked at the other threads here such as fbDidLogin never called (facebook-ios-sdk) and fbDidLogin not called, however I haven't been successful. My app has a main view that is loaded and upon button press it pulls in the second nib and replaces the view. That second view controller has the facebook functionality in there, and I feel like my issue might be connecting from the Appdelegate to that secondViewController. I noticed in the sample app that everything is set up in the appdelegate but that is not the way my program functions.

I'm new to objective c programming but I've been fine in terms of building some apps so far, just this facebook sdk is killing me.

I appreciate any direction people could throw at me. Thank you.

Here's some of the code:

- (void)login {
    facebook=[[Facebook alloc]initWithAppId:FB_APP_ID];
    NSLog(@"Inside login/post to wall function.");
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       @"mystuff",@"test",
                                       @"mystuff", @"link",
                                       @"mystuff", @"picture",
                                       mystuff, @"name",
                                       @"mystuff", @"caption",
                                       @"mystuff", @"description",
                                       @"mystuff",  @"message",
                                       nil];
    [facebook dialog:@"feed" andParams:params andDelegate:self];
}

/**
 * Invalidate the access token and clear the cookie.
 */
- (void)logout {
    [facebook logout:self];
}

/**
 * Called when the user has logged in successfully.
 */
-(void)fbDidLogin{
    NSLog(@"Inside fbDidLogin function.");
}

/**
 * Called when the user canceled the authorization dialog.
 */
-(void)fbDidNotLogin:(BOOL)cancelled {
    NSLog(@"User did not login.");
}


The issue here is that your delegate isn't around anymore once the authorization process completes. I would create a class that implements FBSessionDelegate and keep it as a property on your application delegate. Now, when you wire up the delegate, set it to that property. You can also wire this up using interface builder if you would like. Simply set the delegate to that property that you defined on the app delegate.

For example, you could have a class that does the following (this code covers 80% of it - hopefully it gets you on the right track):

typedef void (^completionHandler)(id);

@interface FacebookController : NSObject <FBSessionDelegate>
{
    completionHandler handler;
}

- (void)postToWallWithOptions:(NSDictionary *)options;
- (void)login:(completionHandler)handler;
- (void)fbDidLogin;

And the implementation would be:

@implementation FacebookController

- (void)postToWallWithOptions:(NSDictionary *)options
{
    completionHandler handler = ^(id data)
        {
    // If you want this class to also be the delegate dialog, it will need to implement FBDialogDelegate
            [facebook dialog:@"feed"
                   andParams:options
                 andDelegate:self];
        };
[self login:handler];
}

- (void)login:(completionHandler)handler
{
completionHandler = handler;
// Be sure to add in your permissions here
[facebook authorize:yourPermissions 
               delegate:self];
}

- (void)fbDidLogin
{
// You could pass any object you needed to here
completionHandler(nil);
}

@end

You would need to define this class as a property on your application delegate:

@property (nonatomic, retain) FacebookController *fbController;

Now, whenever you need your FacebookController, you simply can do this:

FacebookController *fbController = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] fbController];
0

精彩评论

暂无评论...
验证码 换一张
取 消