iPhone Application button with URL POST method - iphone

I would like to ask about the button and the url post method in iPhone application.
In my program, I want the user to click a button, and then a url will be called by POST method. For the url, it may need to redirect to somewhere (302 or 303) etc and final is 200.
I have complete the button and the success page, however, I don't know how to use the objective-C library. I found lots of reference of this forum, but I don't understand what the code means. Can anyone help me?
The following is a question which I believe related to the question.
Invoking a http post URL from iphone using .net web service
Thank you very much.

If you're okay with blocking the thread you're making the request on, this is just about as simple as it gets.
NSString *postBody = [NSString stringWithFormat:#"param1=%#&param2", param1value, param2value];
NSData *postData = [postBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:targetURL];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
A few caveats:
This will perform a synchronous (blocking) request, so either do it on a background thread or look into using the NSURLConnection delegate methods.
POST data must be URL-encoded, so you may need to do some preprocessing of your parameter values.
Redirects will occur automatically until a 200 OK or an error is encountered.

Related

iPhone follow a user on twitter programmatically

I want to follow a twitter user on a button click. I used the share kit follow me method using oauth. But always I am getting this error.
{"error":"Could not authenticate with
OAuth.","request":"/1/friendships/create/priyankav89.json"}
Then I tried to implement it myself
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1/friendships/create.json"];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:#"871252998" forKey:#"user_id"];
[dict setObject:#"true" forKey:#"follow"];
[dict setObject:#"KXL0jFLJY6RI4lszOz8r5w" forKey:#"consumer"];
NSString *jsonRepresentation=[dict JSONString];
NSLog(#"jsonRepresentation = %#",jsonRepresentation);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3];
NSData *postData=[NSData dataWithBytes:jsonRepresentation.UTF8String length:[jsonRepresentation length]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [postData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error=nil;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"respon = %s",responseData.bytes);
still I am getting the same error.
I searched a lot. Still no use. I don't want to load the url in a UIWebView or in safari. I want to support the app from ios4. So ios5 built-in twitter app cannot be used.
I found the problem. In share kit follow me method they are not calling the authorization method before following. So we want to call authorize before login and save the access token.
Assuming you know how to tweet using the twitter integrated to the iOS, you can simply tweet "follow "

Objective-C equivalent of curl request

I'm trying to manipulate this curl request in Objective-C:
curl -u username:password "http://www.example.com/myapi/getdata"
I've implemented the following and I'm getting a data get error Domain=kCFErrorDomainCFNetwork Code=303 with NSErrorFailingURLKey=http://www.example.com/myapi/getdata:
// Make a call to the API to pull out the categories
NSURL *url = [NSURL URLWithString:#"http://www.example.com/myapi/getdata"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// Create the username:password string for the request
NSString *loginString = [NSString stringWithFormat:#"%#:%#", API_USERNAME, API_PASSWORD];
// Create the authorisation string from the username password string
NSData *postData = [loginString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
[request setURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
I was hoping that someone could spot where I am going wrong with my attempt to manipulate the curl request and point me in the correct direction. Is there something obvious I'm missing out? The data returned from the API is in a JSON format.
I found that the best thing to do was to not attempt the authentication within the code and to put it straight in the URL itself. The working code looks as follows:
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:#"http://%#:%##www.example.com/myapi/getdata", API_USERNAME, API_PASSWORD]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
This guide seems to do what you're looking for:
http://deusty.blogspot.co.uk/2006/11/sending-http-get-and-post-from-cocoa.html
Just as an FYI, a lot of classes accept initWithData, and NSData has a method dataWithContentsOfURL, if you want to avoid setting up NSURLConnections yourself this could be an easier way of achieving what you're looking for.

How to post multiple data to web server

I have below code for posting data to the web server but it gives me null response, I have multiple data and they are:-
date,
rig,
driller,
offsider-1,
offsider-2,
shift
,
project,
supervisoremail,
geologistemail,
comments,
plants[0][name-id],
plants[0][start-usage],
plants[0][end-usage],
consumables[0][category],
consumables[0][name-id],
consumables[0][used],
consumables[0][code],
consumables[0][description],
I have to post this data and I used this code for posting but it didn't work for me. Please help me out.
NSURL *url=[NSURL URLWithString:#"http://www.mydor.com.au/staff/dor/idorInsert"];
NSString *post =[[NSString alloc]initWithFormat:#"date=15/08/2012&rig=53&driller=207&offsider-1=131&offsider- 2=236&shift=day&project=24&supervisoremail=24&geologistemail=24&comments=&plants[0][name- id]=286&plants[0][start-usage]=1.0&plants[0][end-usage]=0.0"];
NSLog(post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"here u re");
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"here also called");
NSLog(#"%#",data)
Thanks in advance.
Here is an article which demonstrates how to consume .NET Web service using iOS application. You can use the techniques discussed in the article:
http://www.highoncoding.com/Articles/809_Consuming__NET_Web_Services_in_an_iOS_Application.aspx
Almost all the work I do involves .NET with iOS. Have a look here for the network class I wrote and use all the time (AFNetworking based). I prefer to use a regular aspx form instead of an asmx web-service. I do this because I don't want to expose any web-methods to snooping visitors and I use an API key check before returning anything in the aspx form. I can send you an example .NET code (written in VB.NET) if you need me to.

How to send the updated data using json to server in iphone?

am using web services (JSON). from json am getting data this data loading into tableview am trying to edit this data but after edit the data how to send this updated data to server.
please any one help me?
try this will help you.. this is the post method for updating data in WS .
NSString *post =[NSString stringWithFormat:#"uid=%#&firstname=%#&lastname=%#&phone=%#&bday=%#&about_me=%#&image=%#&image_code=%#&contact_number=%#",LoginID,fname,lname,cn,bday,abtme,strimage11,c11,cn];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"YOUR LINK"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *uData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:uData encoding:NSUTF8StringEncoding];
//
NSMutableDictionary *temp = [data JSONValue];
//
NSLog(#"%#",temp);
I am assuming you have your edited data, then
Form your json. There are several libraries out there which can help you.
Know the hostname of your server.
Know which API to hit on your server.
then pass this json as POST (GET is also ok, but POST is preferred).
Process this received json on your server.
Hope this helps. Nothing much to it actually.

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