Janosch Hübner
Janosch Hübner

Reputation: 1694

iOS ASIFormDataRequest

finally I got ASI-xx stuff working so I dont get ARC compiling errors nor anything else missing.

Now I want to try ASIFormDataRequest to be able to login on a website right from the application.

This is what I alreadxy got, but it doesnt seem to work right:

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];

    NSString *str = @"http://mysite.com/ucp.php?module=login";
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"username" forKey:@"username"];
    [request setPostValue:@"password" forKey:@"password"];
    [request setTimeOutSeconds:120];
    [request setDelegate:self];
    [request startAsynchronous];
    NSLog(@"%@",[request responseString]);
    NSLog(@"%d",[request responseStatusCode]);
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://mysite.com"]]];
    [self.view addSubview:webView];
}

If I remove the webview nothing happens on the screen.

Here are the results of the NSLog:

(null)
0

The textfield(of the website) where I want to POST data to has the following code:

<input tabindex="1" name="username" id="username" size="25" value="" class="inputbox autowidth" type="text">
<input tabindex="2" name="password" id="password" size="25" value="" class="inputbox autowidth" type="text">

Any help appreciated :)

Upvotes: 1

Views: 985

Answers (2)

zeeawan
zeeawan

Reputation: 6895

Your code looks OK but make sure that your web service is working fine using Chrome Advanced Rest Client.

Upvotes: 0

app_
app_

Reputation: 697

Your calling the method [request startAsynchronous]; This handle the request on separate from the main thread so the [request responseString]; will still be empty when it's there.

There are two things you can do. [request startSynchronous] but this will cause your UI to stall.

The best option is:

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

You already set the delegate so this should work.

A sidenote: ASIHTTPRequest is an old framework and they stopt developing for it. A good replacement is AFNetworking.

Upvotes: 1

Related Questions