我正在使用ASIFormDataRequest发布三个值,即网址,标题和摘要。所有这些都在模拟器上工作正常,但它不在设备上工作。以下是我正在使用的代码:
- (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];
}
任何人都可以帮助我!
答案 0 :(得分:2)
首先:不再支持ASIRequest。众所周知,在某些情况下会出现问题,并且不会修复错误。
第二,关于你的问题,我看到两个可能发生错误的地方(模拟器和设备之间的差异):
您确定设备[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUsername"]
和[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultPassword"]
设置正确吗?
在iOS模拟器上,您无法访问钥匙串,因此此设置([loginRequest setUseKeychainPersistence:YES];
)也可能导致设备出现问题。
我建议检查NSUserDefaults
并更好地使用另一个框架,即RestKit
<强>编辑:强>
使用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