我要实现以下目标:拥有一个自定义类(以便我可以从我的应用程序的许多部分调用/使用它),在其中借助于AFNetworking
,我向服务器请求并获得响应。
我在类中的代码有效,但是当我从应用程序的其他部分调用它时,我得到的是空值。我相信这是因为我的自定义类。无论如何,这是我的代码:
UDIDGen.h
#import <Foundation/NSString.h>
@interface NSString (UDIDGen)
+ (NSString *)getUDID;
@end
UDIDGen.m
#import "UDIDGen.h"
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
@implementation NSString (UDIDGen)
+ (NSString *)getUDID{
__strong __block NSString *holder;
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://domain.com/id.php"]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:@"http://domain.com/id.php"
parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
holder=[NSString stringWithFormat:@"%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]];
NSLog(@"SERVER RESPONSE: %@",holder);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[operation start];
return holder;
}
@end
然后,当我想从应用程序的其他部分调用它时,我
#import "UDIDGen.h"
接着
NSString *wsa=[NSString getUDID];
NSLog(@"Response %@",wsa);
出于某种原因
NSLog(@"SERVER RESPONSE: %@",holder);
可以工作,并且我能够nslog服务器响应。但是NSString *wsa=[NSString getUDID];
给了我空值。我认为这与我实现自定义类的方式有关,我做错了什么。
有什么帮助吗?
编辑:我的工作代码,以便其他人使用它:
UDIDGen.h
#import <Foundation/NSString.h>
@interface UDIDGen:NSObject{
}
- (void)getUDIDWithCompletion:(void (^)(NSString *udid))completion;
@end
UDIDGen.m
#import "UDIDGen.h"
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
@implementation UDIDGen
- (void)getUDIDWithCompletion:(void (^)(NSString *udid))completion {
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://domain.com/id.php"]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:@"http://domain.com/id.php"
parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *udid=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"SERVER RESPONSE: %@",udid);
if (completion)
{
completion(udid);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// TODO: Handle error
}];
[operation start];
}
@end
像这样使用它:
UDIDGen *getResponse = [[UDIDGen alloc] init];
[getResponse getUDIDWithCompletion:^(NSString *udid) {
NSLog(@"SERVER RESPONSE: %@",udid);
}];
最佳答案
您正在异步启动AFHTTPRequestOperation
。这意味着它在单独的非阻塞线程中运行,并且您的方法可能在请求完成之前返回。
最简单的方法就是执行此操作,但这很可能会在加载时阻塞GUI:
[operation startSynchronous];
最干净的方法是使用完成块或委派自己,并在收到响应时通知目标类:
+ (void)getUDIDWithCompletion:(void (^)(NSString *udid))completion {
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://domain.com/id.php"]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:@"http://domain.com/id.php"
parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *udid=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"SERVER RESPONSE: %@",udid);
if (completion)
{
completion(udid);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// TODO: Handle error
}];
[operation start];
}
您将这样调用此方法:
[YourCustomClass getUDIDWithCompletion:^(NSString *udid) {
// use udid / update GUI
}];