我对 Objective C 和 RestKit 有一个大问题。 在应用程序启动时,我想单击一个按钮。 但是,如果我在启动后立即单击按钮,我的应用程序就会崩溃。 如果我等待几秒钟,一切都会正常。
有人有想法吗? 这是我的代码
- (void)sendRequest {
// Perform a simple HTTP GET and call me back with the results
[[RKClient sharedClient] get:@"/user" delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
if ([response isSuccessful]) {
NSLog(@"HTTP status code: %d", response.statusCode);
NSLog(@"HTTP status message: %@", [response localizedStatusCodeString]);
}
}
启动后客户端会在“AppDelegate”中创建
RKClient *client = [RKClient clientWithBaseURLString:@"http://192.168.0.6:8080"];
[client setPassword:@"user"];
[client setUsername:@"user"];
[client setAuthenticationType:RKRequestAuthenticationTypeHTTPBasic];
client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
按下按钮后将调用“sendRequest”方法。
抱歉,我的英语不是很好。 我希望你理解我的问题。
最佳答案
我也有同样的问题.. 能够用 block 解决这个问题
NSURL *baseURL = [[NSURL alloc] initWithString:@"http://your.api.com"];
RKClient *client = [RKClient clientWithBaseURL:baseURL];
RKRequest *request = [client get:@"/method/name" queryParameters:nil delegate:nil];
request.onDidLoadResponse = ^(RKResponse *response) {
NSLog(@"%@", [response bodyAsString]);
};
关于objective-c - RestKit iOS 应用程序根据请求崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182181/