【IOS編程】iOS上如何擷取facebook的通知數

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.