要引用兩個標頭檔 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哦,不然永遠都訪問不了的。。