在 iOS 上使用 Facebook Graph API
这是我在 StackOverflow 上的第一篇文章,但我使用它很长一段时间...
我的问题基本上很简单,我想在用户墙上分享 UITextView(mShareText)
的内容,而不提示任何内容对话。基本上,用户必须在 UITextView
上填写消息,然后单击“post”UIButton。很简单不是吗?
我已经下载了 Facebook iOS SDK 并将其复制到我的项目中。我已在控制器的头文件中包含“FBConnect.h
”和“Facebook.h
”,并创建了一个 var : Facebook* facebook
; ,控制器还实现了以下委托:FBSessionDelegate、FBRequestDelegate。
在我的控制器的实现文件中,当用户单击“post”UIButton 时,我会触发 IBAction。
- (IBAction)onShareByFBPressed:(id)sender {
NSLog(@"onShareByFBPressed");
// Facebook settings
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithCapacity:3];
[params setObject:mShareText.text forKey:@"message"];
[params setObject:@"http://www.example.com" forKey:@"link"];
[params setObject:@"https://www.example.com/myImg.jpg" forKey:@"picture"];
[facebook requestWithGraphPath:@"me/feed"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
[params release];
}
我已经在我的 viewdidload 上实现了这个:
- (void)viewDidLoad
{
NSLog(@"viewDidLoad");
[super viewDidLoad];
(...)
// Test facebook
facebook = [[Facebook alloc] initWithAppId:@"XXXXXXXXXXX" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
NSLog(@"AccessToken: %@ ExpirationDate: %@", facebook.accessToken, facebook.expirationDate);
}else{
NSLog(@"No AccessToken or ExpirationDate");
}
if (![facebook isSessionValid]) {
[facebook authorize:[NSArray arrayWithObjects:@"publish_stream", nil]];
}
}
由于委托,我必须实现以下不同的方法:
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSLog(@"fbDidLogin");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
- (void)fbSessionInvalidated{
NSLog(@"fbSessionInvalidated");
}
- (void)fbDidLogout{
NSLog(@"fbDidLogout");
}
- (void)fbDidExtendToken:(NSString*)accessToken expiresAt:(NSDate*)expiresAt{
NSLog(@"fbDidExtendToken");
}
- (void)fbDidNotLogin:(BOOL)cancelled{
NSLog(@"fbDidNotLogin");
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response{
NSLog(@"didReceiveResponse: %@", response);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error{
NSLog(@"didFailWithError: %@", [error description]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Share on Facebook"
message:@"An error occured"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
不幸的是,每次我尝试触发我的“onShareByFBPressed”方法时,我都会在日志中收到以下错误:
2012-02-17 13:02:06.401 Mixtapes[935:707] didFailWithError: Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0x1f6f60 {error=<CFBasicHash 0x1f6660 [0x3ec6d630]>{type = mutable dict, count = 3,
entries =>
2 : <CFString 0x1f64a0 [0x3ec6d630]>{contents = "type"} = <CFString 0x1f6b10 [0x3ec6d630]>{contents = "OAuthException"}
3 : <CFString 0x1f6ab0 [0x3ec6d630]>{contents = "message"} = <CFString 0x1f6a10 [0x3ec6d630]>{contents = "An active access token must be used to query information about the current user."}
6 : <CFString 0x1f6e60 [0x3ec6d630]>{contents = "code"} = 2500
}
}
任何人都可以告诉我出了什么问题吗?与我的实施?
多谢。
此致。
编辑: @Adil
感谢您的建议。它有时确实有效。
有时我会收到以下错误:
didFailWithError: Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0xa17e0d0 {error=<CFBasicHash 0xa171e10 [0x3ec6d630]>{type = mutable dict, count = 3,
entries =>
2 : <CFString 0xa179d10 [0x3ec6d630]>{contents = "type"} = <CFString 0xa17a280 [0x3ec6d630]>{contents = "OAuthException"}
3 : <CFString 0x6127710 [0x3ec6d630]>{contents = "message"} = <CFString 0xa178db0 [0x3ec6d630]>{contents = "Error validating access token: Session is invalid. This could be because the application was uninstalled after the session was created."}
6 : <CFString 0xa17e020 [0x3ec6d630]>{contents = "code"} = 190
}
}
感谢您的帮助
This is my first post on StackOverflow but i use this for a long time...
My problem is basically simple, I want to share the content of a UITextView(mShareText)
on the user's wall without prompting any Dialog. Basically, the user has to fill its message on the UITextView
and then click on a "post" UIButton. Simple isn't it ?
I've downloaded the Facebook iOS SDK and copied it into my project. I've included "FBConnect.h
" and "Facebook.h
" on my controller's header file and created a var : Facebook* facebook
;, the controller also implements the following delegates : FBSessionDelegate, FBRequestDelegate.
On my controller's implementation file, I've a IBAction triggered when the user clicks on the "post" UIButton.
- (IBAction)onShareByFBPressed:(id)sender {
NSLog(@"onShareByFBPressed");
// Facebook settings
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithCapacity:3];
[params setObject:mShareText.text forKey:@"message"];
[params setObject:@"http://www.example.com" forKey:@"link"];
[params setObject:@"https://www.example.com/myImg.jpg" forKey:@"picture"];
[facebook requestWithGraphPath:@"me/feed"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
[params release];
}
I've implemented this on my viewdidload:
- (void)viewDidLoad
{
NSLog(@"viewDidLoad");
[super viewDidLoad];
(...)
// Test facebook
facebook = [[Facebook alloc] initWithAppId:@"XXXXXXXXXXX" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
NSLog(@"AccessToken: %@ ExpirationDate: %@", facebook.accessToken, facebook.expirationDate);
}else{
NSLog(@"No AccessToken or ExpirationDate");
}
if (![facebook isSessionValid]) {
[facebook authorize:[NSArray arrayWithObjects:@"publish_stream", nil]];
}
}
Here are different methods I had to implement due to the delegates :
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSLog(@"fbDidLogin");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
- (void)fbSessionInvalidated{
NSLog(@"fbSessionInvalidated");
}
- (void)fbDidLogout{
NSLog(@"fbDidLogout");
}
- (void)fbDidExtendToken:(NSString*)accessToken expiresAt:(NSDate*)expiresAt{
NSLog(@"fbDidExtendToken");
}
- (void)fbDidNotLogin:(BOOL)cancelled{
NSLog(@"fbDidNotLogin");
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response{
NSLog(@"didReceiveResponse: %@", response);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error{
NSLog(@"didFailWithError: %@", [error description]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Share on Facebook"
message:@"An error occured"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Unfortunatelly, everytime I try to trigger my "onShareByFBPressed" method, I got the following error on the log:
2012-02-17 13:02:06.401 Mixtapes[935:707] didFailWithError: Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0x1f6f60 {error=<CFBasicHash 0x1f6660 [0x3ec6d630]>{type = mutable dict, count = 3,
entries =>
2 : <CFString 0x1f64a0 [0x3ec6d630]>{contents = "type"} = <CFString 0x1f6b10 [0x3ec6d630]>{contents = "OAuthException"}
3 : <CFString 0x1f6ab0 [0x3ec6d630]>{contents = "message"} = <CFString 0x1f6a10 [0x3ec6d630]>{contents = "An active access token must be used to query information about the current user."}
6 : <CFString 0x1f6e60 [0x3ec6d630]>{contents = "code"} = 2500
}
}
Can anyone tell me what's wrong with my implementation ??
Thanks a lot.
Best regards.
EDIT:
@Adil
Thanks for your proposal. It actually works somethimes.
Sometimes i got the following error :
didFailWithError: Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0xa17e0d0 {error=<CFBasicHash 0xa171e10 [0x3ec6d630]>{type = mutable dict, count = 3,
entries =>
2 : <CFString 0xa179d10 [0x3ec6d630]>{contents = "type"} = <CFString 0xa17a280 [0x3ec6d630]>{contents = "OAuthException"}
3 : <CFString 0x6127710 [0x3ec6d630]>{contents = "message"} = <CFString 0xa178db0 [0x3ec6d630]>{contents = "Error validating access token: Session is invalid. This could be because the application was uninstalled after the session was created."}
6 : <CFString 0xa17e020 [0x3ec6d630]>{contents = "code"} = 190
}
}
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是:
被授权的facebook是
AppDelegate
的facebook对象,而不是你的ViewController
的facebook。解决方案:
而是为每个
ViewController
或您需要的地方分配新的Facebook
对象。您应该为您的应用程序创建一个属性
delegate
。像这样:在 appDelegate.h 中
并在 appDelegate.m 中综合它,现在将 Facebook 的初始化放在
现在你想使用它的地方,编写如下内容:
The problem here is:
The facebook which is authororized is
AppDelegate
's facebook object, not yourViewController
's facebook.Solution:
Instead allocating new
Facebook
object for everyViewController
or where you needed.you should make a property of your application
delegate
. like this:in appDelegate.h
and synthesize it in appDelegate.m and now put initialization of Facebook in
now where do you want to use it, write something like this:
尝试取消对您的应用程序的授权,然后再次授权
Try to deauthorize your application and then authorize it again