0

我正在使用 ASIFormDataRequest 发布三个值,即 url、标题和摘要。所有这些都在模拟器上运行良好,但在设备上不起作用。这是我正在使用的代码:

- (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];
}

任何人都可以帮助我!

4

1 回答 1

2

首先:不再支持 ASIRequest。众所周知,在某些情况下它是错误的,并且错误不会被修复。

第二个,关于您的问题,我看到两个可能发生错误的地方(模拟器和设备之间的差异):

  1. 你确定在设备上[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUsername"]并且[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultPassword"]设置正确吗?

  2. 在 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
于 2012-09-03T06:44:01.490 回答