Two header files, Social. h and Accounts. h, are to be referenced.
Read account information from the facebook program installed on the iPhone to access facebook and obtain the content.
- (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;}
When you see the second requestAccessToAccountsWithType, you will be confused. Why don't you read manage_configurations and emails at one time? I also tried this method here, but it always fails. Then I refer to the example above in stackoverflow. They access master permissions such as EMail first, and then access sub-permissions. It turns out that this is correct.
The next step is how to read the number of notifications:
- (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"; } }];}
The SQL-like language here is a method provided by facebook webAPI, called fql. It is similar to SQL usage. For details, refer to the facebook developer website, there are a variety of fql details, you can get friends and other things.
Finally, pay attention to Using VPN in China, otherwise it will never be accessible ..