我有一个非常简单的问题。
在FB教程https://developers.facebook.com/docs/mobile/ios/build/中,它开始在“didFinishLaunchingWithOptions”内登录 - 就在应用程序启动后。
我需要点击登录,然后等待回调并在FB墙上发送消息。 我认为Hackbook应用程序示例设计应用程序太复杂了。
实现这一目标的最简单方法是什么?
UPD:我已经关注了Hackbook示例,但ViewControllers仍然没有得到回调:(
yAppDelegate.h:
#import #import "FBConnect.h" @interface yAppDelegate : UIResponder { Facebook *facebook; } @property (strong, nonatomic) UIWindow *window; @property (nonatomic, retain) Facebook *facebook; @end
yAppDelegate.m:
#import "yAppDelegate.h" #import "yViewController.h" static NSString* kAppId = @"350435425024264"; @implementation yAppDelegate @synthesize window = _window; @synthesize facebook; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { yViewController *viewController = [[yViewController alloc] init]; facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:viewController]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) { facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; } return YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { [[self facebook] extendAccessTokenIfNeeded]; } - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [self.facebook handleOpenURL:url]; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [self.facebook handleOpenURL:url]; }
yViewController.h:
#import #import "FBConnect.h" @interface yViewController : UIViewController { NSArray *permissions; } @property (nonatomic, retain) NSArray *permissions; @end
yViewController.m
#import "yViewController.h" #import "yAppDelegate.h" #import "FBConnect.h" @interface yViewController () @end @implementation yViewController @synthesize permissions; - (IBAction)buttonPressed:(UIButton *)sender { NSLog(@"Button pressed!"); permissions = [[NSArray alloc] initWithObjects:@"offline_access", nil]; yAppDelegate *delegate = (yAppDelegate *)[UIApplication sharedApplication].delegate; if (![[delegate facebook] isSessionValid]) { [[delegate facebook] authorize:permissions]; } else { //[self showLoggedIn]; } NSLog(@"login!!"); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma mark - FBSessionDelegate Methods /** * Called when the user has logged in successfully. */ - (void)fbDidLogin { NSLog(@"did login"); } -(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt { NSLog(@"token extended"); } /** * Called when the user canceled the authorization dialog. */ -(void)fbDidNotLogin:(BOOL)cancelled { NSLog(@"fbDidNotLogin"); } /** * Called when the request logout has succeeded. */ - (void)fbDidLogout { NSLog(@"fbDidLogout"); // Remove saved authorization information if it exists and it is // ok to clear it (logout, session invalid, app unauthorized) NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults removeObjectForKey:@"FBAccessTokenKey"]; [defaults removeObjectForKey:@"FBExpirationDateKey"]; [defaults synchronize]; } /** * Called when the session has expired. */ - (void)fbSessionInvalidated { NSLog(@"fbSessionInvalidated"); [self fbDidLogout]; } #pragma mark - FBRequestDelegate Methods /** * Called when the Facebook API request has returned a response. * * This callback gives you access to the raw response. It's called before * (void)request:(FBRequest *)request didLoad:(id)result, * which is passed the parsed response object. */ - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response { //NSLog(@"received response"); } /** * Called when a request returns and its response has been parsed into * an object. * * The resulting object may be a dictionary, an array or a string, depending * on the format of the API response. If you need access to the raw response, * use: * * (void)request:(FBRequest *)request * didReceiveResponse:(NSURLResponse *)response */ - (void)request:(FBRequest *)request didLoad:(id)result { NSLog(@"-(void)request"); } /** * Called when an error prevents the Facebook API request from completing * successfully. */ - (void)request:(FBRequest *)request didFailWithError:(NSError *)error { NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]); NSLog(@"Err code: %d", [error code]); } @end
P.S。对不起我的英文:)
答案 0 :(得分:2)
以与FB教程显示的完全相同的方式执行此操作,只需将登录代码移动到按钮事件或任何您想要触发的事件。