ASIFormDataRequest works on simulator but not on device?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 18:34:47

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/12241889/asiformdatarequest-works-on-simulator-but-not-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!