我是StackOverflow和iOS的新手。我正在尝试获取用户信息,如团体,书籍等,我需要使用Facebook图形API。要使用图API,您必须使用访问令牌。我在Facebook开发者页面上阅读了关于这个主题的文章,我写了一些代码,但它没有用。你能帮助我吗?我的代码是正确的还是我应该用另一种方式?
我的.h文件:
#import <UIKit/UIKit.h>
#import "FBConnect.h"
@interface FirstViewController : UIViewController <FBSessionDelegate,FBLoginDialogDelegate,FBRequestDelegate>{
Facebook *face;
}
@property(nonatomic,retain)Facebook *face;
-(IBAction)facePushed:(id)sender;
-(IBAction)logoutPushed:(id)sender;
我的.m文件:
-(IBAction)facePushed:(id)sender{
NSLog(@"FacePushed Began");
face=[[Facebook alloc]initWithAppId:@"313714785358239" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
face.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
face.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![face isSessionValid]) {
[face authorize:nil ];
}
else {
NSLog(@"Session is valid");
}
NSLog(@"FacePushed End");
}
}
- (void)fbDidLogin {
NSLog(@"aslan");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[face accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[face expirationDate] forKey:@"FBExpirationDateKey"];
NSLog(@"%@", [face accessToken]);
[defaults synchronize];
}
- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate {
NSLog(@"%@",token);
}
答案 0 :(得分:1)
您必须在实施中检查以下内容:
您需要在授权应用之前获得权限
if (![face isSessionValid])
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_likes",
@"read_stream",
@"publish_stream",
nil];
[face authorize:permissions];
[permissions release];
}
您需要在应用程序委托中添加以下函数(此处的 facebook 对象是FaceBook类的实例值)。
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [facebook handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [facebook handleOpenURL:url];
}
添加到网址类型&gt;网址方案&gt;你的facebok app ID带有fb前缀(最后你的值就像这个fb313714785358239)。
祝你好运。