Web service in iphone - iphone

Hi everyone i am new to iphone development.I am working on an application which required to check and register the user name and password using soap web service.could anyone help me how to do that with that web service.i have two text fields for login.and button to check if the credentials are correct or not.

Maybe this is helpful for you: http://devmylife.com/?p=111
The website disappeared, you can find it here: http://web.archive.org/web/20100704000223/http://devmylife.com/?p=111
ASIHTTP SOAP Request
First of all down http://www.soapui.org/ or any packet sniffing app. and try requesting your SOAP-service. When you get the SOAP-packet, you have to exactly generate the same packet using ASIHTTP. You have to add headers yourself.
Download latest version of ASIHTTP
NSString *urlString = URL_HOST;
NSString *soapMessage = [NSString stringWithFormat:#"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<SOAP-ENV:Body>"
"<m:getInboxPosts xmlns:m=\"urn:finditnearwsdl\">"
"<input xsi:type=\"tns:InboxPostsRequest\">"
"<session_id xsi:type=\"xsd:string\">%#</session_id>"
"<page_size xsi:type=\"xsd:int\"></page_size>"
"<offset xsi:type=\"xsd:int\"></offset>"
"</input>"
"</m:getInboxPosts>"
"</SOAP-ENV:Body>"
"</SOAP-ENV:Envelope>" ,adBo.sessionId];
NSLog(soapMessage);
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlString];
[request appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding];
[request addRequestHeader:#"SOAPAction:" value:SOAP_ACTION];
[request startSynchronous];

Related

Changes coming in Version 1.1 of the Twitter API in iPhone

I am using twitter API version V1.0 for my new iphone application .I am successfully able to do this using twitters v1.0 of the API and all works perfectly. Simply making a request to http://api.twitter.com/1/statuses/user_timeline.json?screen_name=userid retrieves all the information that I require.
since v1.0 has been deprecated and V1.1 requires authentication for each request, I get a bad authorization error (HTTP response status: 400)using this API.
What are the Changes i need to do in my appication .how to generate OAuth request headers,Do i need register my application ?How can i get authentication for new version?
I hope the above makes sense and any help would be appreciated.
Thanks in advance.
Visit https://dev.twitter.com/docs/ios/making-api-requests-slrequest
https://dev.twitter.com/docs/oauth/xauth
u can get the authentication using following method:
NSString *base64 = #"Basic NFRLWGlZY3l1aHJ4OVJaUWI5RW5BOmdVMXlvcVV6YzBNZUhUQmFXdVRZU2NtZHlIWDFOZXhwZmxqRE16bm01aw==";
//oauth_consumer_secret,oauth_consumer_key
NSURL *urlAPI = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.twitter.com/oauth2/token"]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlAPI];
[request setDelegate:self];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Authorization" value:base64];
[request addRequestHeader:#"Content-Type" value:#"application/x-www-form-urlencoded;charset=UTF-8"];
[request setPostValue:#"client_credentials" forKey:#"grant_type"];
In response u will get the authtoken for your application like this
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAMddRQAAAAAAXRP6Axur1RS%2Fv9YFtJ7TijyPAGo%3DpmNowwOGf3dZTHDiH2gnCcv7qNIyGZcyV2IW5YFTBs"}

How to store the values in web server from i phone application

I am developing view based application.In my view i have register page in that page i have
some fields like Firstname, lastname,e mail ID. When we click save button after entering the values these all field should be store in in webserver.In webserver i have application that application was developed using .net MVC Architecture and database is MYSQL .How i can store these values in webserver.
thanks for your response i have written this code is it right way to store values in webserver
-(IBAction)buttonClick:(id)sender
{
NSString* firstname = nameInput.text;
NSString* lastname = passInput.text;
NSString* bname = lastInput.text;
NSString *post =
[[NSString alloc] initWithFormat:#"fname=%#&lname=%#&email=%#",firstname,lastname,bname];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php?%#",post]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) NSLog(#"Connection Successful");
}
#end
You have two options:
1) Develop a proper API that your iOS app can call using libraries like ASIHTTPRequest or AFNetworking
2) Have a .NET form processor in place, just like you would to process an HTML form, and then use ASIHTTPRequest, AFNetworking, or something similar to submit the request to this processor using the POST method and with the parameters you would like to store added to the request. This simulates an HTML form that has been filled and submitted, and then you can do whatever you wish with the data .NET receives. Both ASI and AFNetworking have pretty extensive documentation on how these types of requests are implemented on iOS with their respective libraries. Unfortunately, ASI is no longer being maintained, so I would recommend going with AFNetworking if possible.
RESPONSE TO UPDATE:
I only gave it a quick look over, but everything looks good to me. The only thing I would change is this:
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php?%#",post]]];
to this:
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php"]]];
What you will need now is code on the web side in that file.php that can process the request. It would use the typical $_POST['var_name_here'] to grab the data that is passed to it.
You have to implement a webapplication in your preferred language and deploy it to some webserver or hosting service. To get started, you should choose a language PHP, Java, Python, C# or whatever and google how you could implement a webservice within that language.
I would do this in Java and implement a RESTful wbeservice that sends and receives JSON or XML.

How to use post manner to transmit username and password in order to log in a website on iphone or ipad platform?

How to use post manner to transmit username and password in order to log in a website on iphone or ipad platform?
Some one has suggest me that use ASIHTTPRequest,but I don't know how to use it.
Can somebody help me ?Thank you ........
ASIHTTPRequest has one of the best how to use pages of any library I have ever encountered. It is located here: http://allseeing-i.com/ASIHTTPRequest/How-to-use
If you need to post to a web form you could do something like this:
#define kURLString #"https://yourwebsite.com"
NSURL *url = [NSURL urlWithString:kURLString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"myname" forKey:#"username"];
[request setPostValue:#"l33td00d" forKey:#"password"];
[request setDelegate:self];
[request startSynchronous];
Although in real life you may wish to run the request asynchronous. The details on how to do that are on the page I linked. It is defiantly worth reading.

Twitter profile image upload in objective-c

I want to upload an image to my twitter profile using objective-c. I saw in the twitter API that I need to send a HTML post to http://twitter.com/account/update_profile_image.format and send the picture as a parameter. I am done with the authentication. I am stuck with the uploading. Maybe somebody can help me with sending the picture as a parameter?
You should be using NSURLRequests and NSURLConnection to perform the API requests. If this is true, all you need to do is create an NSMutableURLRequest, set it's URL to the Twitter image upload API URL, set the method to POST.
Then you'll need to create an NSData object to represent your image, which you can do using
NSData *myImageData = [[NSData alloc] initWithData:[myImage CGImage]];
I don't know what the parameter name is for Twitter's upload API, so for arguments sake, lets call it "image". The next thing you need to do is set the image data as the request's body for the "image" parameter, like this
NSString *bodyString = [NSString stringWithFormat:#"image=%#", [[[NSString alloc] initWithData:myImageData encoding:NSStringUTF8Encoding] autorelease]];
[myRequest setBody:bodyString];
Then you can just start your NSURLConnection with the request and it should upload.
If you’ve managed to get started, then this post on CocoaDev should help you set the uploading up. There’s a sample linked at the top too.
I recommend using ASIHTTPRequest
What is ASIHTTPRequest?
ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and iPhone applications.
It is suitable performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE). The included ASIFormDataRequest subclass makes it easy to submit POST data and files using multipart/form-data.
See this blog post for an example
Somthing like this
// See http://groups.google.com/group/twitter-development-talk/browse_thread/thread/df7102654c3077be/163abfbdcd24b8bf
NSString *postUrl = #"http://api.twitter.com/1/account/update_profile_image.json";
ASIFormDataRequest *req = [[ASIFormDataRequest alloc] initWithURL:[NSURL
URLWithString:postUrl]];
[req addRequestHeader:#"Authorization" value:[oAuth oAuthHeaderForMethod:#"POST"
andUrl:postUrl andParams:nil]];
[req setData:UIImageJPEGRepresentation(imageView.image, 0.8)
withFileName:#"myProfileImage.jpg"
andContentType:#"image/jpeg" forKey:#"image"];
[req startSynchronous];
NSLog(#"Got HTTP status code from Twitter after posting profile image: %d", [req
responseStatusCode]);
NSLog(#"Response string: %#", [req responseString]);
[req release];

Can I log in to my site from my iPhone app?

I'm trying to make a log in or sign up feature for my web site in my iPhone app. My website is a content management system, and like any other CMS, it has log in and registration features. It also has permmissions, dependent on the user account. I think I would have to use UIWebView for this.
Are there any examples or tutorials I can examine?
Check out the documentation for NSURLRequest (and NSMutableURLRequest): you can use it to make a POST request to your login and registration pages, just like a web browser. You can write the form UI in Cocoa/Objective-C and then send the data to the server.
As far as displaying the result to the user, you'll have to figure out a way to either parse the returned HTML (bad idea) or modify your CMS to return JSON or XML to iPhone requests (better idea).
Edit: Here's some sample code, taken from an app I'm working on (it submits data to Last.fm using POST):
NSURL *url = [NSURL URLWithString:#"http://example.com/"];
NSString *str = #"This is my example data!";
// everything below here is directly from my app:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:kLastFMClientUserAgent forHTTPHeaderField:#"User-Agent"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setHTTPShouldHandleCookies:NO];
*connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];