gpt4 book ai didi

iphone - 使用 Web 服务的 AFNetworking 身份验证

转载 作者:可可西里 更新时间:2023-11-01 06:16:39 34 4
gpt4 key购买 nike

有一个问题更多的是设计考虑而不是代码问题。

我的 iOS 应用程序与 json 网络服务交互。我正在使用 AFNetworking,我的问题基本上是我需要 init 函数(它验证 AFHTTPClient 并检索 token )在我发出任何其他请求(需要所述 token )之前完全完成。

从下面的代码中,我有兴趣听听实现此目的的设计方法,我更愿意将所有请求保持异步,另一种解决方案是使 initWithHost:port:user:pass 中的请求同步(不使用 AFNetworking ) 我知道这是不好的做法,希望避免。

DCWebServiceManager.h

#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"

@interface DCWebServiceManager : NSObject
{
NSString *hostServer;
NSString *hostPort;
NSString *hostUser;
NSString *hostPass;
NSString *hostToken;
AFHTTPClient *httpClient;
}

// Designated Initialiser
- (id)initWithHost:(NSString *)host port:(NSString *)port user:(NSString *)user pass:(NSString *)pass;

// Instance Methods
- (void)getFileList;
@end

DCWebServiceManager.m

#import "DCWebServiceManager.h"
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
#import "AFJSONRequestOperation.h"

@implementation DCWebServiceManager

- (id)initWithHost:(NSString *)host port:(NSString *)port user:(NSString *)user pass:(NSString *)pass
{
self = [super init];
if (self)
{
hostServer = host;
hostPort = port;
hostUser = user;
hostPass = pass;

NSString *apiPath = [NSString stringWithFormat:@"http://%@:%@/", hostServer, hostPort];

httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:apiPath]];
[httpClient setAuthorizationHeaderWithUsername:hostUser password:hostPass];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"authenticate.php" parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){

// Do operations to parse request token to be used in
// all requests going forward...
// ...
// ...
// Results in setting: hostToken = '<PARSED_TOKEN>'
NSLog(@"HostToken: >>%@<<", hostToken);

} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"ERROR: %@", operation.responseString);
}];

[operation start];
}

return self;
}

- (void)getFileList
{
// *************************
// The issue is here, getFileList gets called before the hostToken is retrieved..
// Make the authenticate request in initWithHost:port:user:pass a synchronous request perhaps??
// *************************
NSLog(@"IN GETFILELIST: %@", hostToken); // Results in host token being nil!!!

NSString *queryString = [NSString stringWithFormat:@"?list&token=%s", hostToken];
NSMutableURLRequest *listRequest = [httpClient requestWithMethod:@"GET" path:queryString parameters:nil];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:listRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(@"SUCCESS!");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
NSLog(@"ERROR!: %@", error);
}];

[operation start];
}
@end

ViewController.m

....
DCWebServiceManager *manager = [[DCWebServiceManager alloc] initWithHost:@"localhost" port:@"23312" user:@"FOO" pass:@"BAR"];
[manager getFileList];

// OUTPUTS
IN GETFILELIST: (nil)
HostToken: >>sdf5fdsfs46a6cawca6<<
....
...

最佳答案

我建议子类化 AFHTTPClient 并为 token 添加 +sharedInstance 和属性。

+ (MyGClient *)sharedInstanceWithHost:(NSString *)host port:(NSString *)port user:(NSString *)user pass:(NSString *)pass {
static MyClient *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[... your code from the init ...]
});
return sharedInstance;
}

然后您可以重写 enqueueHTTPRequestOperationWithRequest:success:failure 以在进一步操作入队之前检查 token 。

此外,您可以通过覆盖属性的 setter 来收集操作并在设置 token 后立即将它们排入队列。

关于iphone - 使用 Web 服务的 AFNetworking 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11468869/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com