我试图使用Facebook SDK从我的应用程序登录到FB。我虔诚地遵循这个教程https://developers.facebook.com/docs/mobile/ios/build/#implementsso!Facebook iOS SDK:isSessionValid返回否,但实际登录时
我的问题 这是我做的(我没有安装在我的iPhone上的Facebook应用程序) 1.我第一次从我的应用程序登录的“iPhone”到Facebook上,它打开Safari浏览器。我登录&授予我的应用程序的权限。 2.我从iPhone中删除了我的应用程序&重新启动了我的iPhone。 3.现在,当我尝试从我的App再次登录到FB时,isSessionValid调用返回'no',我试着打印访问令牌&它返回null。这可以。但是,在执行isSessionValid时,App会调用(如在教程中) [thisAppDelegate.facebook authorize:nil]; 它在iPhone中打开Safari并带我进入一个Facebook页面,该页面显示已登录(怎么会,当isSessionValid返回null?好吧,假设我已经登录该应用程序与FB的会话了),并且此应用程序已经被授权。我在下面的屏幕中点击“好的”,应用程序崩溃。
我得到一个EXC_BAD_ACCESS(代码1)fbLogin调用完成后。这似乎在FBURLConnection.m是发生在2号线(“for”循环)这种方法:
- (BOOL)isCDNURL:(NSURL *)url
{
NSString* urlHost = url.host;
for (NSString* host in _cdnHosts) {
if ([urlHost hasSuffix:host]) {
return YES;
}
}
return NO;
}
打印的“urlHost”的值给我的价值“graph.facebook.com”。
有人可以帮忙吗?
代码片段
AppDelegate.h
#import <UIKit/UIKit.h>
#import "FBConnect.h"
@interface com_AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate> {
Facebook *facebook;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) Facebook *facebook;
@end
我AppDelegate.m
...
@synthesize facebook;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
facebook = [[Facebook alloc] initWithAppId:kFBAppId andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSLog(@"AppDelegate.handleOpenURL");
return [facebook handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(@"AppDelegate.openURL");
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSLog(@"AppDelegate.fbDidLogin");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
NSLog(@"Set the facebook accessToken to '%@' & expiry to '%@'", [facebook accessToken], [facebook expirationDate]);
}
...
我有一个类FBTalker这将打开一个FB连接。
从FBTalker.h
... @interface ViewController_FBTalker:{的UIViewController ID 的appDelegate; } @property(nonatomic)com_AppDelegate * thisAppDelegate; ...
FBTalker.m
...
-(void) initializeView {
appDelegate = [[UIApplication sharedApplication] delegate];
thisAppDelegate = (com_AppDelegate *) appDelegate;
/*
Check for a valid session and if it is not valid call the authorize method which will both log the user in and prompt the user to authorize the app
*/
NSLog(@"thisAppDelegate.facebook: access token - %@", [thisAppDelegate.facebook accessToken]);
NSLog(@"thisAppDelegate.facebook: exp date - %@", [thisAppDelegate.facebook expirationDate]);
if (![thisAppDelegate.facebook isSessionValid]) {
[thisAppDelegate.facebook authorize:nil];
NSLog(@"No valid FB Session yet");
// Checks if the App has permission to publish to the user stream
/* NSArray *permissions = [[NSArray alloc] initWithObjects:
@"publish_stream",
nil];
[thisAppDelegate.facebook authorize:permissions];
*/
} else {
NSLog(@"Valid FB Session exists");
}
...
环境 的XCode 4.3.3 的Facebook的iOS SDK(最新从GitHub)https://github.com/facebook/facebook-ios-sdk/ 的iOS 5 iPhone 4(未安装Facebook应用)
+1非常感谢,Ananth!让我试试看,如果能解决问题,请接受你的答案! :) – Jean 2012-08-15 05:03:42