我的代码:
RKRequest.m:
#import "RKRequestDelegate.h"
#import "Constants.h"
@implementation RKRequestDelegate {
}
- (void)sendRequest:(UIImageView *)imageView {
NSDictionary *queryParams = [NSDictionary dictionaryWithObjectsAndKeys:@"0", @"w", @"4", @"section", @"0",
@"latitude", @"0", @"longitude", @"0", @"dt", @"0", @"di", nil];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:[Constants getSettingsName]
ofType:[Constants getSettingsFormat]];
NSDictionary *contentDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSString *addedUrl = [contentDictionary valueForKey:[Constants getBannerTag]];
//
RKRequest *request = [[RKClient sharedClient] get:addedUrl queryParameters:queryParams delegate:self];
}
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
NSLog(@"Response!!!");
}
@end
RKRequestDelegate.h:
@interface RKRequestDelegate : NSObject <RKRequestDelegate>
- (void)sendRequest:(UIImageView *)UIImageView;
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response;
@end
AppDelegate.m:
RKClient *client = [RKClient clientWithBaseURL:[HttpUtil getHost]];
启动应用程序后,它会在 5 秒后崩溃。如果我将 delegate:self
更改为 delegate:[RKRequestDelegate class]
它不会崩溃,并给出响应 200(OK),但它没有回调 didLoadResponse
!
请帮忙,谢谢。
更新:
如果我在 RKRequestDelegate.m 中使用 [request sendSynchronously];
,就会调用 response。
最佳答案
使request
成为一个类变量。
编辑:这是一些代码,没有在现实生活中检查过,但这种方式应该有效
@interface MYRequests : NSObject<RKRequestDelegate>
{
RKRequest *request;
}
@implementation MYRequests
- (void)loginRequest
{
request = [RKRequest ...];
request.method = RKRequestMethodGET;
request.delegate = self;
[request send];
}
@end
关于iphone - iOS RestKit Framework - 发送请求后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11322796/