Ios SLRequest处理Facebook令牌到期等
在我的应用程序中,我有一个设置视图,其中UISegmentControl设置为Yes或No,以允许使用SLRequest和Accounts框架将查看的页面自动发布到Facebook。它的流程是这样的: 它会自动设置为“否”。当用户单击“是”时,它会运行以下代码:Ios SLRequest处理Facebook令牌到期等,ios,facebook,Ios,Facebook,在我的应用程序中,我有一个设置视图,其中UISegmentControl设置为Yes或No,以允许使用SLRequest和Accounts框架将查看的页面自动发布到Facebook。它的流程是这样的: 它会自动设置为“否”。当用户单击“是”时,它会运行以下代码: -(void)facebookpost { ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *facebookAc
-(void)facebookpost {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// We will pass this dictionary in the next method. It should contain your Facebook App ID key,
// permissions and (optionally) the ACFacebookAudienceKey
NSDictionary *options = @{ACFacebookAppIdKey : @"APPID",
ACFacebookPermissionsKey : @[@"email", @"publish_stream"],
ACFacebookAudienceKey:ACFacebookAudienceFriends};
// Request access to the Facebook account.
// The user will see an alert view when you perform this method.
[accountStore requestAccessToAccountsWithType:facebookAccountType
options:options
completion:^(BOOL granted, NSError *error) {
if (granted)
{
// At this point we can assume that we have access to the Facebook account
NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
// Optionally save the account
[accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil];
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"facebook"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Failed to grant access\n%@", error);
}
}];
}
-(void)writetowall {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray *accounts = [accountStore accountsWithAccountType:accountType];
// If we don't have access to users Facebook account, the account store will return an empty array.
if (accounts.count == 0)
return;
// Since there's only one Facebook account, grab the last object
ACAccount *account = [accounts lastObject];
// Create the parameters dictionary and the URL (!use HTTPS!)
NSDictionary *parameters = @{@"message" : @"MY MESSAGE", @"link": _entry.articleUrl};
NSURL *URL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];
// Create request
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:URL
parameters:parameters];
// Since we are performing a method that requires authorization we can simply
// add the ACAccount to the SLRequest
[request setAccount:account];
// Perform request
[request performRequestWithHandler:^(NSData *respData, NSHTTPURLResponse *urlResp, NSError *error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:respData
options:kNilOptions
error:&error];
// Check for errors in the responseDictionary
}];
}
它还将facebookposting的NSUserDefault键的布尔值设置为YES
然后,在后面的代码中,当查看页面时,如果facebookposting的值为YES,则运行此代码。如果否,它将永远不会运行以下代码:
-(void)facebookpost {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// We will pass this dictionary in the next method. It should contain your Facebook App ID key,
// permissions and (optionally) the ACFacebookAudienceKey
NSDictionary *options = @{ACFacebookAppIdKey : @"APPID",
ACFacebookPermissionsKey : @[@"email", @"publish_stream"],
ACFacebookAudienceKey:ACFacebookAudienceFriends};
// Request access to the Facebook account.
// The user will see an alert view when you perform this method.
[accountStore requestAccessToAccountsWithType:facebookAccountType
options:options
completion:^(BOOL granted, NSError *error) {
if (granted)
{
// At this point we can assume that we have access to the Facebook account
NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
// Optionally save the account
[accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil];
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"facebook"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Failed to grant access\n%@", error);
}
}];
}
-(void)writetowall {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray *accounts = [accountStore accountsWithAccountType:accountType];
// If we don't have access to users Facebook account, the account store will return an empty array.
if (accounts.count == 0)
return;
// Since there's only one Facebook account, grab the last object
ACAccount *account = [accounts lastObject];
// Create the parameters dictionary and the URL (!use HTTPS!)
NSDictionary *parameters = @{@"message" : @"MY MESSAGE", @"link": _entry.articleUrl};
NSURL *URL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];
// Create request
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:URL
parameters:parameters];
// Since we are performing a method that requires authorization we can simply
// add the ACAccount to the SLRequest
[request setAccount:account];
// Perform request
[request performRequestWithHandler:^(NSData *respData, NSHTTPURLResponse *urlResp, NSError *error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:respData
options:kNilOptions
error:&error];
// Check for errors in the responseDictionary
}];
}
我想知道的是如何以及在哪里处理诸如从Facebook删除权限或更改密码等事情。是否需要在获取权限的第一组代码中处理这些事情?因为,基本上这组代码只运行一次,其余时间它只运行代码发布到墙上。我想我应该能够添加
[accountStore RenewCredentials for account:account completion:^(ACAccountCredentialRenewResult renewResult,NSError*error){//我们实际上不需要检查更新结果或错误。如果(错误){}];
在要发布到墙的代码中,但我不确定如何首先检测令牌是否已过期。或者,在处理发布的类上(不是初始授权)我是否可以简单地为已更改的帐户添加观察者,如果触发,则运行续订?当续订时,是否需要再次使用APPID?