如何使用 SLRequest 在 iOS 6 中集成 Facebook?
Posted
技术标签:
【中文标题】如何使用 SLRequest 在 iOS 6 中集成 Facebook?【英文标题】:How to integrate Facebook in iOS 6 using SLRequest? 【发布时间】:2012-10-06 06:46:10 【问题描述】:我一直在寻找如何使用 SLRequests 将 Facebook 集成到 ios6 中。经过一番研究,我能够做到。 这是一些代码 sn-p 显示它是如何完成的。
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = @
ACFacebookAppIdKey: @"012345678912345",
ACFacebookPermissionsKey: @[@"publish_stream", @"publish_actions"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
;
[accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *e)
if (granted)
NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
else
// Handle Failure
];
NSDictionary *parameters = @@"message": @"My first iOS 6 Facebook posting ";
NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
feedRequest.account = facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
// Handle response
];
我希望这对某人有所帮助。
【问题讨论】:
我很困惑。 requestAccessToAccountsWithType: 不是异步的吗?在使用 feedRequest 之前如何获取 facebookAccount? 【参考方案1】:我有一篇关于如何使用 SLRequest 的文章。
-(IBAction)postMessage:(id)sender
// Create the URL to the end point
NSURL *postURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];
NSString *link = @"http://developer.apple.com/library/ios/#documentation/Social/Reference/Social_Framework/_index.html%23//apple_ref/doc/uid/TP40012233";
NSString *message = @"Testing Social Framework";
NSString *picture = @"http://www.stuarticus.com/wp-content/uploads/2012/08/SDKsmall.png";
NSString *name = @"Social Framework";
NSString *caption = @"Reference Documentation";
NSString *description = @"The Social framework lets you integrate your app with supported social networking services. On iOS and OS X, this framework provides a template for creating HTTP requests. On iOS only, the Social framework provides a generalized interface for posting requests on behalf of the user.";
NSDictionary *postDict = @
@"link": link,
@"message" : message,
@"picture" : picture,
@"name" : name,
@"caption" : caption,
@"description" : description
;
SLRequest *postToMyWall = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:postURL parameters:postDict];
FacebookAccountManager* sharedManager = [FacebookAccountManager sharedAccount];
[postToMyWall setAccount:sharedManager.facebookAccount];
[postToMyWall performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
if (error)
// If there is an error we populate the error string with error
_errorString = [NSString stringWithFormat:@"%@", [error localizedDescription]];
// We then perform the UI update on the main thread. All UI updates must be completed on the main thread.
[self performSelectorOnMainThread:@selector(updateErrorString) withObject:nil waitUntilDone:NO];
else
NSLog(@"Post successful");
NSString *dataString = [[NSString alloc] initWithData:responseData encoding:NSStringEncodingConversionAllowLossy];
NSLog(@"Response Data: %@", dataString);
];
可在此处下载完整的帖子和应用程序:https://github.com/stuarticus/SocialFrameworkReference
【讨论】:
以上是关于如何使用 SLRequest 在 iOS 6 中集成 Facebook?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS Objective-C 中集成“使用 Apple 登录”流程?
使用 SLRequest 将多张图片上传到 Facebook