我对Objective C和iOS开发非常陌生(比如5小时新:-)。我有一些代码调用API对用户进行身份验证,并返回一个简单的OK或FAIL。我可以将结果写入控制台,但我需要做的是将该结果作为IBAction的一部分进行获取。
下面是IBAction代码:
- (IBAction) authenticateUser
{
[txtEmail resignFirstResponder];
[txtPassword resignFirstResponder];
[self performAuthentication];
if (authResult == @"OK")我需要的是让authResult成为JSON结果(OK或FAIL)。下面是获得结果的代码:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", responseString);
[responseData release];
NSMutableDictionary *jsonResult = [responseString JSONValue];
if (jsonResult != nil)
{
NSString *jsonResponse = [jsonResult objectForKey:@"Result"];
NSLog(@"%@", jsonResponse);
}
}非常感谢你的帮助,如果我遗漏了一些明显的东西,我很抱歉!
发布于 2011-03-09 11:48:24
我有点困惑这是怎么回事...看起来您的-performAuthentication方法必须通过NSURLConnection启动一个异步网络请求,并且您的连接委托的-connectionDidFinishLoading: gets来确定请求的结果。到目前为止还好吗?但是您的-authenticateUser方法期望在-performAuthentication返回后立即确定authResult。如果网络请求是异步的,这是不会发生的。如果我在关注你,我认为你需要做以下事情:
if ([authResult isEqualToString:@"OK") { }
https://stackoverflow.com/questions/5240952
复制相似问题