Problems with Facebook-Api publish_stream in iPhone application
I've added Facebook Connect to my new iPhone application. Everything perfect, no problem.
In this application, however, I need to post on user's Wall without prompt any dialog box. I've searched in Facebook Documentation and, from what I understand, if I ask user to give me the right permission (in this case, publish_stream), the dialog box should no longer appear.
But the box appears, despite of all.
I hope you can he开发者_如何学Pythonlp me.
Thank you.
P.S. Sorry for my bad English
Use the graph API after you have acquired the publish_stream permission. You make a POST to:
https://graph.facebook.com/ID/feed
This iPhone SDK does not support this natively, you will have to implement this yourself. You will need to ensure that you create the proper JSON encoded parameters and ensure they are properly escaped. A good place to start with this is here.
Thank you! So, if I understand well, I have to do something like this:
Use this framework http://code.google.com/p/json-framework/ to add JSON support.
And this code:
SBJSON *json = [SBJSON new];
json.humanReadable = YES;
NSString *service = @"NameService";
NSMutableDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"MessaggeFromMyApp",@"message",
@"http://www.sample.com",@"link",
@"nomeOfTheLink",@"name",
@"captionOfTheLink",@"caption",
@"descriptionofTheLink",@"description",
@"MyDistrict",@"value",
@"2",@"txs_Action",
nil];
//Pass it twice to escape quotes
NSString *jsonString = [NSString stringWithFormat:@"%@", [params JSONFragment], nil];
NSString *changeJSON = [NSString stringWithFormat:@"%@", [jsonString JSONFragment], nil];
NSLog(jsonString);
NSLog(changeJSON);
NSString *requestString = [NSString stringWithFormat:@"{\"id\":15,\"method\":\"%@\",\"params\":[%@]}",service,changeJSON,nil];
NSLog(requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"https://graph.facebook.com/me/feed"]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
//Data returned by WebService
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(returnString);
Thank you again.
精彩评论