我在将facebook集成到我的iOS应用中时遇到了问题。当我启动我的应用时,它会重定向到Facebook应用,这很好。但是auth对话框只是从屏幕上消失了(就像一个被驳回的模式视图控制器),委托方法不会触发,我留在facebook应用程序中而不是被重定向回到我的应用程序。我显然没有成功初始化会话。我遵循iOS教程中的步骤,但我一定在某处出错了。我见过的大多数问题都是关于错误或试图从auth对话中获得不同的行为。如果这是一个骗局,请直接指向我相同的问题,因为我在这里打墙。我已经按照教程中的说明设置了我的.plist。我使用从iTunes连接抓取的应用程序的包标识符。我的常量kAppID是我从Facebook Developer应用程序界面获得的AppID。仔细阅读了FB静态库的头文件和实现文件,以及FB的文档和教程,在我看来,如果我想要做的就是重定向到FB应用程序,请登录并重定向到我自己的应用程序,这些步骤加上下面的代码应该足够了。ios5中的Facebook SSO
在AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
kAppID = @"(this is where my app ID is)";
// Override point for customization after application launch.
facebook = [[Facebook alloc] initWithAppId:kAppID andDelegate:self];
[facebook setSessionDelegate:self];
return YES;
}
而在ViewController.m我抢在Facebook的同一实例的引用,然后调用如下:
- (void)loginToFacebook:(id)sender {
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
}
有一些疑难杂症的是我不知道?或者我错过了关键的一步?
哦,顺便说一句,Xcode 4.3.1,ios5SDK,iPhone 4S,ARC。
.plist的SS。
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "Facebook.h"
#import "FBConnect.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
Facebook *facebook;
NSUserDefaults *defaults;
NSString *kAppID;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) Facebook *facebook;
@property (strong, nonatomic) id delegate;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize facebook;
@synthesize delegate;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController *viewController = [[ViewController alloc] init];
kAppID = @"385380598149062";
facebook = [[Facebook alloc] initWithAppId:kAppID andDelegate:viewController];
return YES;
}
- (void)showSession {
NSLog(@"FB Session: %@", [NSNumber numberWithBool:[facebook isSessionValid]]);
}
@end
正如你所看到的,没有太多发生在这些方法。委托类是ViewController。我真正在AppDelegate中做的是init实例。关于这一点,herre是整个ViewController.m(减去一些非Facebook的相关的东西):
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize contacts, contactArray;
- (void)viewDidLoad
{
[super viewDidLoad];
defaults = [NSUserDefaults standardUserDefaults];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
facebook = delegate.facebook;
[facebook setSessionDelegate:self];
[self loginToFacebook:nil];
eventsArray = [[NSMutableArray alloc] initWithObjects:@"Event one", @"Event two", nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation = UIInterfaceOrientationPortrait);
} else {
return NO;
}
}
- (void)loginToFacebook:(id)sender {
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
NSLog(@"accessToken:%@\n expiry:%@", facebook.accessToken, facebook.expirationDate);
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSLog(@"handle open url");
return [facebook handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)request:(FBRequest *)request didLoad:(id)result {
//ok so it's a dictionary with one element (key="data"), which is an array of dictionaries, each with "name" and "id" keys
fbContacts = [(NSDictionary *)result objectForKey:@"data"];
NSLog(@"Request did load");
for (int i=0; i<[fbContacts count]; i++) {
NSDictionary *friend = [fbContacts objectAtIndex:i];
long long fbid = [[friend objectForKey:@"id"]longLongValue];
NSString *name = [friend objectForKey:@"name"];
NSLog(@"id: %lld - Name: %@", fbid, name);
}
}
- (void)fbDidLogin {
NSLog(@"FB did log in");
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
[facebook requestWithGraphPath:@"me/friends" andDelegate:self];
}
- (void)fbDidNotLogin:(BOOL)cancelled {
NSLog(@"FB login cancelled");
}
/**
* Called after the access token was extended. If your application has any
* references to the previous access token (for example, if your application
* stores the previous access token in persistent storage), your application
* should overwrite the old access token with the new one in this method.
* See extendAccessToken for more details.
*/
- (void)fbDidExtendToken:(NSString*)accessToken
expiresAt:(NSDate*)expiresAt {
NSLog(@"FB extended token");
}
/**
* Called when the user logged out.
*/
- (void)fbDidLogout {
NSLog(@"FB logged out");
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
/**
* Called when the current session has expired. This might happen when:
* - the access token expired
* - the app has been disabled
* - the user revoked the app's permissions
* - the user changed his or her password
*/
- (void)fbSessionInvalidated {
NSLog(@"FB session invalidated");
}
@end
你能说明你在.plist文件中完成了什么吗?我认为问题应该在那里... – dmirkitanov 2012-04-05 22:06:33
编辑,谢谢你看看。 – geraldWilliam 2012-04-05 22:14:41
嗨。你可以发布你的appdelegate文件吗?** – 2012-04-05 22:23:25