newby IOS: unable to redirect url

97 Views Asked by At

This is my first IOS project. Having a tough time getting login to work. The site has a meta_refresh to another url. I've tried to send another request to the url in the meta_refresh but then the app just hangs. I'm sure that I'm doing something wrong but I'm using XCode 4.4 so a lot of the NSURLConnection delegate methods have been deprecated.

Here's what I'm doing:

NSString *urlAsString = baseURL;
urlAsString = [urlAsString stringByAppendingString:@"?user[login]=username"];
urlAsString = [urlAsString stringByAppendingString:@"&user[password]=password"];
urlAsString = [urlAsString stringByAppendingString:@"&origin=splash"];

NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
NSURLResponse *response = nil;
NSError *error = nil;

NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];    

if([data length] > 0 && error == nil){
    NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"HTML = %@", html);
    NSRange obj = [html rangeOfString:@"http-equiv=\"refresh\""];
    NSInteger len = obj.length;
} else if([data length] == 0 && error == nil) {
    NSLog(@"No data was returned.");

} else {
    NSLog(@"Error happened = %@", error);
}

Here's what I'm getting:

Another question is that I've read that I should use asynchronous connection. The trouble is that I need the data scraped from the site to display on the screen. If I display the screen before the data is returned, then I won't have any data -- or am I not understanding how this works?

Thanks. --Tony

1

There are 1 best solutions below

0
estemendoza On

I think that maybe you're a mixing stuff. You're setting a string url with GET values and then you set the HTTPMethod to POST and I don't know if that is going to work fine.

Here's a code that I have to send values with POST:

NSString *post = [NSString stringWithFormat:@"&user_login=%@&user_password=%@&origin=%@,username,password, splash];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:baseURL]]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];