我创建了一个名为“LoginFacebook”的单例类,旨在将用户连接到 Facebook 并执行不同的请求。问题是我收到有关 access_token 的错误。这里是 :
3 : <CFString 0x4c552f0 [0xe50400]>{contents = "message"} = <CFString 0x4c55250 [0xe50400]>{contents = "An active access token must be used to query information about the current user."}
首先,我通过从另一个 View Controller 发出以下请求连接到 Facebook:
[[LoginFacebook loginFacebook] launchFacebook:self]
然后我从同一个其他视图控制器但从另一种方法发出第二个请求:
[[LoginFacebook loginFacebook] requireName:self]
这是我的单身课程“LoginFacebook”:
LoginFacebook.h :
#import <UIKit/UIKit.h>
#import "LoginFacebook.h"
@interface FirstViewController : UIViewController {
}
-(IBAction)performConnect:(id)sender;
-(IBAction)performName:(id)sender;
@end
登录Facebook.m:
#import "LoginFacebook.h"
static LoginFacebook *loginFacebook = nil;
@implementation LoginFacebook
@synthesize name;
@synthesize facebook;
-(void)launchFacebook:(id)sender
{
permissions = [[NSArray arrayWithObjects:
@"read_stream", @"publish_stream", @"offline_access",nil] retain];
Facebook* facebookbis = [[Facebook alloc] initWithAppId:@"168938499825684"];
facebook = facebookbis;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil delegate:self];
}
}
-(NSString *)requireName:(id)sender
{
NSLog(@"requireName asked");
[facebook requestWithGraphPath:@"me" andDelegate:self];
return name;
NSLog(@"%@",[facebook accessToken]);
}
+ (LoginFacebook *)loginFacebook
{
if (loginFacebook == nil) {
loginFacebook = [[super allocWithZone:NULL] init];
}
return loginFacebook;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self loginFacebook] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax; //denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
// FBRequestDelegate
/**
* 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, a string,
* or a number, 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 {
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
name = [result objectForKey:@"name"];
NSLog(@"request didLoad");
};
/**
* Called when an error prevents the Facebook API request from completing
* successfully.
*/
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
name = [error localizedDescription];
NSLog(@"----request didFailWithError");
NSLog(@"%@", [error localizedDescription]);
NSLog(@"%@", [error description]);
};
////////////////////////////////////////////////////////////////////////////////
// FBDialogDelegate
/**
* Called when a UIServer Dialog successfully return.
*/
- (void)dialogDidComplete:(FBDialog *)dialog {
name = @"publish successfully";
}
@end
另请注意,我在我的 App Delegate 中添加了以下方法(在 .h 中有相应的 FacebookLogin *facebook):
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
你们当中有人知道那里出了什么问题吗?我已经在代码上苦苦挣扎了 2 天了...