1

我想实现以下内容:有一个自定义类(所以我可以从我的应用程序的许多部分调用/使用它),在我的帮助下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);
    }];
4

1 回答 1

1

您正在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
}];
于 2013-10-05T16:43:56.440 回答