[IOS programming] How To Get facebook notifications on iOS

Source: Internet
Author: User

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 ..

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.