Problems with Facebook-Api publish_stream in iPhone application - iphone

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 help 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.

Related

Post comment to Wordpress blog from iPhone App

I used a code to post a comment to a wordpress blog from the following question:
Post comment to WordPress Blog from iPhone programmatically
I made a few modifications to get text from UITextFields:
- (IBAction)postComment:(id)sender {
NSString *post_url = #"http://movilarena.com/wp-comments-post.php";
NSString *post_content = #"comment_post_ID=%#&comment_parent=%#&author=%#&email=%#&comment=%#";
NSString *post_str = [NSString stringWithFormat:post_content, #"1", #"0", self.txtName.text, self.txtEmail.text, self.txtComment.text];
NSData *data = [NSData dataWithBytes:[post_str UTF8String] length:[post_str length]];
NSURL * url = [NSURL URLWithString:post_url];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:#"POST"];
[req setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[req setHTTPBody:data];
//Synchronous
NSURLResponse *response;
NSError *err;
NSData *ret = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
NSString *retString = [NSString stringWithUTF8String:[ret bytes]];
NSLog(#"%#",retString);
}
My problem is that the sendSynchronousRequest returns an error:
Execution was interrupted, reason: EXC_BAD_ACCESS (code=1,
address=0x0). The process has been returned to the state before
execution.
I will appreciate any suggestions to identify what's wrong here. I use XCode 4.5.2 and run the code on an iPhone 4S with iOS 6.0.1
Solved myself. The field comment_post_ID should be the ID of the post I am trying to reply.
NSString *post_content = #"comment_post_ID=%d&comment_parent=%#&author=%#&email=%#&comment=%#";
NSString *post_str = [NSString stringWithFormat:post_content, self.postID, #"0", self.txtName.text, self.txtEmail.text, self.txtComment.text];

How to access a SOAP webservice from my iPhone app

I am new to iPhone application development and also this stackoverflow,if I made any mistakes sorry. I have some of the questions
1) Do I need to install any other external framework into my application. I am using SDK Simulator 4.2
2) I have gone through some of the material which was posted here, I found some code and I made some modification to suits my application When I am requesting for a URL it is returning me NULL.
3) My task is to send a three parameters into database.
NSString *post =[NSString stringWithFormat:#"?FirstName=%#?Surname=%#?IDNumber=%#?DOB=%#",namestring,surnamestring,cidstring,dobstring];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSLog(#"Post Length:",[postData length]);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://www.esoft.co.za/MDS/Service1.asmx?op=InsertSuppliers"]];
[request setHTTPMethod:#"GET"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(#"response data is: %#",[responseData length] );
NSString *data = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#",data);
Thanks in Advance Guys...Please provide me any sample code or material..
I think it's because you're trying to send invalid request. Just try to check and log error message to see what's wrong.
What you're trying to do is to send POST data over GET request, which is wrong anyway. I'm also not sure about those "Post" data format, but when you'll check error message you'll find what's wrong.
NSLog(#"Request failed with error: %#", [err localizedDescription]);
best

Why this POST don't work?

this is my snippet:
NSString *post = #"from=A&name=B&email=ciccio#aaasd.com&messaggio=MESSAGE&codHidden=-1";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://www.mysite.com/page.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(#"Succeeded! Received %d bytes of data",[urlData length]);
NSString *outputdata = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSLog(#"%#", outputdata);
It call a php page that send me an email...
Is there any errors in this code?
Does not seem to work...
thanks
Is it a Synchronous/Asynchronous problem? If so, use the delegate-pattern and access the data asynchronously. Could you post any errors you see (or the server's response string)?
EDIT: Since you're not doing anything useful with the error returned, change your class to do the request asynchronously. A great answer to this was previously posted here. Once you implement didFailWithError you'll have a better picture.
Try to change NSASCIIStringEncoding to NSUTF8StringEncoding

How to get data from a json-rpc webservice : iPad /iPhone / Objective C

I'am working on a iPad project and this project needs to talk to a json-rpc webservices. The webservices is based on Drupal with module : cck and views
1)I need to push a json object into the webservice
2)I need the callback data from the webservice
I already have implemented the SBJSON api and the https://github.com/samuraisam/DeferredKit/ api to the iPad project.
The SBJSON api works fine and I understand this one
The Samuriaisam DefferedKit is new for me
My question is how to get data out of this json-rpc webservice, has someone some sample code? Or some places where I can find Objective C - json-rpc webservice documentation.
Kind Regards,
Bart Schoon
---------Update--------
I use this code now:
NSString *jsonString = #"{\"method\":\"views.get\",\"params\":{\"view_name\":\"client_list\",\"sessid\":\"xxxxxx\"},\"id\":1}";
NSString *requestString = [NSString stringWithFormat:#"%#",jsonString,nil];
NSLog(#"input: %#",jsonString);
NSData *requestData = [NSData dataWithBytes: [jsonString UTF8String] length: [jsonString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:#"http://subdomain.domain.com/services/json-rpc"]];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setHTTPMethod: #"POST"];
[request setValue:#"Content-type: application/json" 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(#"output: %#",returnString);
This will result in this message from the server:
{"error":{"name":"JSONRPCError","code":-32600,"message":"The received JSON not a valid JSON-RPC Request"},"version":"1.1"}
---------/Update--------
What is wrong? Has someone experience with this?
Kind Regards,
Bart Schoon
Read JSon file get that data.
NSDictionary *dictionary = [jsonString JSONValue];
You will get key & value pair. Store that data in your respective variable.
-(IBAction)testCall{
NSString *requestString = [NSString stringWithFormat:#"method=views.get&view_name=client_list",nil];
NSLog(requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: #"http://.xxxxxxxxx.nl/services/json"]];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setHTTPMethod: #"POST"];
[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);
NSDictionary *dict = [returnString JSONValue];
}
Just don't use a json-rpc - keep it simple and json the normal jSon method ;)

How i use Php in iphone

I am new in iphone,in my app required data from the server.
1)mysql database uploaded on the server
2)With the help of php code i fetch the data from the server
problem is that how i integrate php in my iphone please solve my problem,
give any reference.
Thanks,
I'm not sure to understand your problem, but you can't use PHP on iPhone.
If you need data from an external database, create web services to get the data in JSON, XML, or what suits you best.
Your iPhone application will connect to a specific URL, and get the data from a (PHP) script that will query the database and return the result in a specific format.
The best way in my opinion is to use JSON (see http://code.google.com/p/json-framework/).
You can make requests to your server using this code
NSError *error;
NSString *post =[NSString stringWithFormat:#"any-data=%#",
myData];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://your-server.com/your-script.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#",data);
NSDictionary* parsedData = [data JSONValue];
You then can access the data using
[parsedData valueForKeyPath:#"myVar"]
Hope this hepls,
Miguel
You could look at passing arrays of objects from the server to the iPhone in the form of a plist file. Have the server generate the plist in the appropriate format on a url and then on the device:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:#"http://www.example.com/myplist.php"];
NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:#"http://www.example.com/myplist.php"];
If your unsure what the format is for plist files create one in your xcode project and use that as a template.