views:

212

answers:

1

Hey all..

I'm planning to convert my website into an iphone native application. I'm stuck up at the point how to achieve this. As i am new to iphone development, I'm not getting any idea on how to develop this.

Initially, i have developed the first screen i.e., LOGIN screen of my application. My server is built in JAVA. I'm able to send the login credentials to the server and able to see the request on the server. But I'm unable to receive the response from the server for the request i've sent.

A part of my code is:

NSString *post = @"username=";

post = [post stringByAppendingString:username];
post = [post stringByAppendingString:@"&password="];
post = [post stringByAppendingString:password];

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://mysite.com/login.action?"]];
[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) 
{
    receivedData = [[NSMutableData data] retain];
 NSLog(@"In \"LOGIN()\" :  receivedData = %@",receivedData);
}
  • (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [receivedData setLength:0];

    NSLog(@"In \"didReceiveResponse()\" : receivedData = %@",receivedData); }

  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data]; NSString *ReturnStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"In \"didReceiveData()\" : receivedData = %@",receivedData); NSLog(@"Return String : %@", ReturnStr);

}

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    NSString *urlData; if (nil != receivedData) { urlData = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease]; } NSLog(@"In \"connectionDidFinishLoading()\" : urlData = %@",urlData);

    [receivedData release]; }

  • (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [connection release];

    [receivedData release];

    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); }

My question in clear is..

  1. How do i develop a LOGIN application?
  2. I'm sending a request successfully to the server but unable to get the response from the server. How can i overcome this issue?

Any help is greatly appreciated..

Thanks in advance..

Venkat

+1  A: 

If your experience is with web development you may be interested in looking at NimbleKit for iPhone. It's a simple XCode plugin that allows you to use HTML and JavaScript to develop the entire native application.

Nolte Burke