要引用两个头文件 Social.h 和 Accounts.h
通过从iPhone上面安装的facebook程序中读取账户信息来访问facebook,并且获取内容。
- (id)init
{
self = [super init];
if (self) {
self.isRequesting = NO;
self.accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSMutableDictionary *optionsDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"376722425766807",ACFacebookAppIdKey, @[@"email"], ACFacebookPermissionsKey,nil];
、
[self.accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:optionsDict
completion:^(BOOL granted, NSError *error)
{
if (granted) {
[optionsDict setObject:@[@"manage_notifications"/*,@"read_mailbox"*/] forKey:ACFacebookPermissionsKey];
[self.accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:optionsDict
completion:^(BOOL granted, NSError *error)
{
if (granted) {
self.isConnect = YES;
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookTypeAccount];
self.account = [accounts lastObject];
} else {
NSLog(@"Facebook Error:%@", [error localizedDescription]);
self.isConnect = NO;
facebookCount = @"x";
}
}];
} else {
//fail
NSLog(@"Facebook Error:%@", [error localizedDescription]);
self.isConnect = NO;
facebookCount = @"x";
}
}];
}
return self;
}
看到第二个requestAccessToAccountsWithType,大家肯定会感到疑惑,为什么不在上面一次性读取manage_notifications,email呢?这里我也尝试过这种做法,但是总是失败,然后参考了stackoverflow上面的例子,他们都是先访问一下EMail这类主权限,然后再去访问副权限的。事实证明这样做是对的。
接下去就是如何读取通知数了:
- (void)requestForNotifcation
{
if (self.account == nil || self.isRequesting) {
return;
}
self.isRequesting = YES;
NSString *urlStr = @"https://graph.facebook.com/fql";
NSURL *requestUrl = [NSURL URLWithString:urlStr];
NSDictionary *fql = [NSDictionary dictionaryWithObject:@"SELECT title_text FROM notification WHERE recipient_id=me() AND is_unread =1" forKey:@"q"];
//Token:read_mailbox
//SELECT unread_count FROM mailbox_folder WHERE folder_id=0
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestUrl
parameters:fql];
request.account = self.account;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
//process
//NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]);
self.isRequesting = NO;
if (responseData != nil) {
if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
id json = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:nil];
if (json != nil) {
NSDictionary *mainDict = json;
NSDictionary *unreadNotifications = [mainDict objectForKey:@"data"];
NSInteger count = [unreadNotifications count];
if ([self.delegate respondsToSelector:@selector(didGetNotificationCount:)]) {
[self.delegate didGetNotificationCount:count];
}
}
}
} else {
facebookCount = @"x";
}
}];
}
这里的类似SQL的语言是facebook webAPI提供的一种方式,叫做fql,和SQL的用法有点像,具体可以查看facebook的developer网站,里面有比较详细的各种fql,可以获取好友之类更多的东西。
最后,国内的注意用VPN哦,不然永远都访问不了的。。