HTTP 通信ロジックをカプセル化する iOS アプリの静的クラスをまとめようとしています。サインを介して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