使用Graph API iPhone发布到Facebook [英] Cant Post To Facebook with Graph API iPhone
问题描述
我已经实现了下面的示例代码来获取与用户feed的连接。
i have implemented this sample code below to get a connection to the user feed post
- (void)viewDidLoad
{
[super viewDidLoad];
facebook = [[Facebook alloc] initWithAppId:@"197765190297119" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
此示例代码在用户登录...它现在通过按下按钮进行检查来调用,但是dosent可以做任何操作,也许我可以得到一个对话框提示
and this sample code is called when the user logs in... it calls now by a button pressed for check, but dosent do any, perhaps i can get a dialog feed insted
NSString *str=@"Your String to post";
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
str,@"message",
@"Test it!",@"name",
nil];
Facebook *fb = [[Facebook alloc] init];
[fb requestWithGraphPath:@"me/feed" // or use page ID instead of 'me'
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
推荐答案
这是一个将图像上传到Facebook的演练壁。我复制了这个旧的应用程序,目前的Facebook API工作有点不同。我想你可以看到我使用一个共享的Facebook对象的主要观点,您可以使用它来进行身份验证以及对API的请求。我拿出几件东西,使其更容易理解。苹果实际上希望您检查现有的互联网连接。我希望它有帮助。
This is a walkthrough of uploading an image to the facebook wall. I copied this out of an older application, the current facebook API works a little bit different. I think you can see my main point of using a shared facebook object, which you use to do the authentication and also the requests to the API. I took out a few things to make it easier to understand. Apple actually wants you to check for an existing internet connection. I hope it helps.
@synthesize facebook;
//login with facebook
- (IBAction) facebookButtonPressed {
if (!facebook || ![facebook isSessionValid]) {
self.facebook = [[[Facebook alloc] init] autorelease];
NSArray *perms = [NSArray arrayWithObjects: @"read_stream", @"create_note", nil];
[facebook authorize:FACEBOOK_API_KEY permissions:perms delegate:self];
}
else {
[self fbDidLogin];
}
}
//upload image once you're logged in
-(void) fbDidLogin {
[self fbUploadImage];
}
- (void) fbUploadImage
{
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
resultImage, @"picture",
nil];
[facebook requestWithMethodName: @"photos.upload"
andParams: params
andHttpMethod: @"POST"
andDelegate: self];
self.currentAlertView = [[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Facebook", @"")
message:NSLocalizedString(@"Uploading to Facebook.", @"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles: nil] autorelease];
[self.currentAlertView show];
}
//facebook error
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error
{
[self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
self.currentAlertView = nil;
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Error", @"")
message:NSLocalizedString(@"Facebook error message", @"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[myAlert show];
[myAlert release];
}
//facebook success
- (void)request:(FBRequest*)request didLoad:(id)result
{
[self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
self.currentAlertView = nil;
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Facebook", @"")
message:NSLocalizedString(@"Uploaded to Facebook message", @"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[myAlert show];
[myAlert release];
}
这篇关于使用Graph API iPhone发布到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!