iphoneiosxcodesimulatorasiformdatarequest

ASIFormDataRequest works on simulator but not on device?


I am using ASIFormDataRequest to post three values which are a url, a title and a summary. All of them works fine on simulator but its not working on device. Here are the code that I'm using:

- (IBAction)addLinkPressed:(UIButton *)sender {

    ASIFormDataRequest *loginRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://MyWebsite.com/login.php"]];
    [loginRequest setRequestMethod:@"POST"];
    [loginRequest setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUsername"] forKey:@"username"];
    [loginRequest setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultPassword"] forKey:@"password"];
    [loginRequest setUseKeychainPersistence:YES];
    [loginRequest setDelegate:self];

    [loginRequest setDidFinishSelector:@selector(requestLoginFinished:)];
    [loginRequest setDidFailSelector:@selector(requestLoginFailed:)];
    [loginRequest startSynchronous];



    NSString *strURL = @"http://MyWebsite.com/send_link.php";
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
    [request setDelegate:self];
    [request setPostValue:self.linkField.text forKey:@"url"];
    [request setPostValue:self.linkTitleField.text forKey:@"title"];
    [request setPostValue:self.linkSummaryField.text forKey:@"summary"];

    [request setDidFinishSelector:@selector(requestDone:)];
    [request setDidFailSelector:@selector(requestWentWrong:)];
    [request startAsynchronous];
}

Anybody can help me please!


Solution

  • First of all: ASIRequest is not anymore supported. It is known to be buggy in some cases and that bugs won't be fixed.

    Second one, regarding your question, I see two places where error could occur (difference between simulator and device):

    1. Are you sure that on device [[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUsername"] and [[NSUserDefaults standardUserDefaults] objectForKey:@"defaultPassword"] are set properly?

    2. On iOS simulator you have not got access to keychain so this setting ([loginRequest setUseKeychainPersistence:YES];) could cause problems on the device too.

    I advice to check NSUserDefaults and better use another one framework, i.e. RestKit

    EDITED:

    Example of using RestKit:

    [RKClient clientWithBaseURLString:@"http://example.com"];
    RKClient *rkclient = [RKClient sharedClient];
    RKParams *params = [RKParams paramsWithDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       username, @"username",
                                                       password, @"password", nil]];
    [rkclient post:@"/profile" params:params delegate:self];
    
    // implement needed delegate methods