我正在尝试为我的 iOS 应用程序组合一个静态类,该类将封装我的 HTTP 通信逻辑。我知道如何通过+
符号在 Objective-C 中创建静态方法,但我希望我的类能够接受一个块并在它的任务完成时调用它。到目前为止我得到的类编译得很好,但是当我调用它的函数时,它只会到达某个点并且失败。这是我当前的代码:
#import <UIKit/UIKit.h>
#pragma mark - Globals
#import "Globals.h"
@interface HTTPRequest : NSObject <NSURLConnectionDelegate>
+ (void)GET:(NSMutableURLRequest *)request completionBlock:(void (^)(void))block;
+ (void)POST:(NSMutableURLRequest *)request andBody:(NSString *)requestString completionBlock:(void (^)(void))block;
@end
#import "HTTPRequest.h"
@interface HTTPRequest () {
NSInteger statusCode;
}
@end
static void (^completionBlock)(void);
@implementation HTTPRequest
+ (void)GET:(NSMutableURLRequest *)request completionBlock:(void (^)(void))block {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[request setHTTPMethod:@"GET"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (connection) {
responseData = [[NSMutableData alloc] init];
} else {
NSLog(@"NSURLConnection INSTANCE FAILED");
};
completionBlock = block;
}
+ (void)POST:(NSMutableURLRequest *)request andBody:(NSString *)requestString completionBlock:(void (^)(void))block {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSData *requestData = [NSData dataWithBytes:[requestString UTF8String]
length:[requestString length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:requestData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (connection) {
responseData = [[NSMutableData alloc] init];
} else {
NSLog(@"NSURLConnection INSTANCE FAILED");
};
completionBlock = block;
}
#pragma mark - NSURLConnectionDelegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
statusCode = [((NSHTTPURLResponse *)response) statusCode];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"CONNECTION ERROR: %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"RESPONSE STRING: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (statusCode == 200) {
completionBlock();
} else {
NSLog(@"RESPONSE ERROR: %i", statusCode);
};
}
@end
我知道 GET 和 POST 函数被调用并且它们创建了NSURLConnection
对象。此后,没有任何NSURLConnectionDelegate
函数被调用。
任何人都可以发现这个问题,或者,如果我完全错了,请告诉我。提前致谢!
更新
我应该指出,该应用程序是基于 ARC 的。我在这里阅读了有关NSURLConnectionDelegate
未调用函数的其他问题,但其中大多数似乎是因为开发人员将对象设置为autorelease
. 由于我无法控制它,我假设它不应该是一个问题。
更新 2(重写?)
我做了一些更改,从静态函数中创建了一个类的实例并使用它的实例函数。现在NSURLConnectionDelegate
函数被调用了,我知道我的onSuccessBlock
块也被调用了,所以整个类一直在执行。坦率地说,我不确定这是否是合法代码,但编译器没有抱怨,我看到了我期待的日志消息,所以我会继续使用它。这是当前的实现:
#import <UIKit/UIKit.h>
@interface HTTPRequest : NSObject <NSURLConnectionDelegate>
+ (void)requestAsGET:(NSMutableURLRequest *)request onSuccess:(void (^)(void))successBlock onFail:(void (^)(void))failBlock;
- (void)requestAsGET:(NSMutableURLRequest *)request onSuccess:(void (^)(void))successBlock onFail:(void (^)(void))failBlock;
+ (void)requestAsPOST:(NSMutableURLRequest *)request body:(NSString *)body onSuccess:(void (^)(void))successBlock onFail:(void (^)(void))failBlock;
- (void)requestAsPOST:(NSMutableURLRequest *)request body:(NSString *)body onSuccess:(void (^)(void))successBlock onFail:(void (^)(void))failBlock;
@end
#import "HTTPRequest.h"
@interface HTTPRequest () {
NSMutableData *responseData;
NSInteger statusCode;
void (^onSuccessBlock)(void);
void (^onFailBlock)(void);
}
@end
@implementation HTTPRequest
- (id)init {
self = [super init];
if (self) {
};
return self;
}
+ (void)requestAsGET:(NSMutableURLRequest *)request onSuccess:(void (^)(void))successBlock onFail:(void (^)(void))failBlock {
[[[HTTPRequest alloc] init] requestAsGET:request
onSuccess:successBlock
onFail:failBlock];
}
- (void)requestAsGET:(NSMutableURLRequest *)request onSuccess:(void (^)(void))successBlock onFail:(void (^)(void))failBlock {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[request setHTTPMethod:@"GET"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (connection) {
onSuccessBlock = successBlock;
onFailBlock = failBlock;
} else {
NSLog(@"NSURLConnection INSTANCE FAILED");
};
}
+ (void)requestAsPOST:(NSMutableURLRequest *)request body:(NSString *)body onSuccess:(void (^)(void))successBlock onFail:(void (^)(void))failBlock {
[[[HTTPRequest alloc] init] requestAsPOST:request
body:body
onSuccess:successBlock
onFail:failBlock];
}
- (void)requestAsPOST:(NSMutableURLRequest *)request body:(NSString *)body onSuccess:(void (^)(void))successBlock onFail:(void (^)(void))failBlock {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSData *requestData = [NSData dataWithBytes:[body UTF8String]
length:[body length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:requestData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (connection) {
onSuccessBlock = successBlock;
onFailBlock = failBlock;
} else {
NSLog(@"NSURLConnection INSTANCE FAILED");
};
}
#pragma mark - NSURLConnectionDelegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
statusCode = [((NSHTTPURLResponse *)response) statusCode];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"CONNECTION ERROR: %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"RESPONSE STRING: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (statusCode == 200) {
// Pass responseData into the block... How?
onSuccessBlock();
} else {
// Pass statusCode into the block?
onFailBlock();
};
}
@end