gpt4 book ai didi

iphone - 无法在 AFNetworking 客户端 IOS 5/6 中维护 session

转载 作者:行者123 更新时间:2023-12-03 20:40:52 24 4
gpt4 key购买 nike

您好,我在 IOS 应用程序中使用 AFNetworking,但无法维持 session 。我正在使用 AFNetworking 客户端并在对服务器的所有请求中使用它。

我已经解决了以下问题:How to manage sessions with AFNetworking ,但我不打算操纵 cookie 或 session 。我打算在应用程序的整个生命周期中实现一个 session 。

我的AFNetworking客户端的.m如下

<p></p>

<pre><code>@implementation MyApiClient


+(MyApiClient *)sharedClient {
static MyApiClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{

_sharedClient = [[MyApiClient alloc] initWithBaseURL:[GlobalParams sharedInstance].baseUrl];
});
return _sharedClient;
}

-(id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
self.parameterEncoding = AFFormURLParameterEncoding;

return self;

}
@end
</code></pre>

<p></p>

我通过调用“- (void)searchBarSearchButtonClicked:(UISearchBar *)search”向服务器发出以下请求 -->>

<p></p>

<pre><code>NSString *path = [NSString stringWithFormat:@"mobile"];
NSURLRequest *request = [[MyApiClient sharedClient] requestWithMethod:@"GET" path:path parameters:@{@"q":search.text}];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request,
NSHTTPURLResponse *response, id json) {
// code for successful return goes here
NSLog(@"search feed %@", json);
search_feed_json = [json objectForKey:@"searchFeed"];

if (search_feed_json.count > 0) {
//<#statements-if-true#>


show_feed_json = search_feed_json;


//reload table view to load the current non-empty activity feed into the table
[self.tableView reloadData];

} else {
//<#statements-if-false#>
NSLog(@"Response: %@", @"no feed");
}


} failure :^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
// code for failed request goes here
NSLog(@"nops : %@", error);
}];

[operation start];
</code></pre>

<p></p>

任何人都可以指导我或指出我错在哪里吗?任何帮助,将不胜感激。提前致谢。

编辑::

我在以下帖子中找到了答案:https://stackoverflow.com/a/14405805/1935921

我在 GlobalParams 类中初始化了答案中列出的方法,其中包含所有全局参数和方法。当应用程序登录且服务器发送 Cookie 时,我调用“saveCookies”方法。然后,每次我使用“loadCookies”方法发出任何后续请求时,都会加载这些 cookie。代码如下:

GlobalParams.h

<p></p>

<pre><code>#import <Foundation/Foundation.h>


@interface GlobalParams : NSObject

// Your property settings for your variables go here
// here's one example:

@property (nonatomic, assign) NSURL *baseUrl;


// This is the method to access this Singleton class
+ (GlobalParams *)sharedInstance;

- (void)saveCookies;
- (void)loadCookies;

@end
</code></pre>

<p></p>

GlobalParams.m

<p></p>

<pre><code> #import "GlobalParams.h"


@implementation GlobalParams

@synthesize myUserData,baseUrl;

+ (GlobalParams *)sharedInstance
{
// the instance of this class is stored here
static GlobalParams *myInstance = nil;

// check to see if an instance already exists
if (nil == dronnaInstance) {
myInstance = [[[self class] alloc] init];


myInstance.baseUrl = [NSURL URLWithString:@"http://www.test.dronna.com/"];

}
// return the instance of this class
return myInstance;
}


- (void)saveCookies{

NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: cookiesData forKey: @"sessionCookies"];
[defaults synchronize];

}

- (void)loadCookies{

NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"sessionCookies"]];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (NSHTTPCookie *cookie in cookies){
[cookieStorage setCookie: cookie];
}

}

@end
</code></pre>

<p></p>

然后,在定义 NSMutableRequest 后,我​​在所有服务器调用中调用“[[GlobalParams sharedInstance] saveCookies];”或“[[GlobalParams sharedInstance] loadCookies];”(取决于 cookie 中所需的读/写)。我希望这对某人有帮助。

最佳答案

我在以下帖子中找到了答案:https://stackoverflow.com/a/14405805/1935921

我在 GlobalParams 类中初始化了答案中列出的方法,其中包含所有全局参数和方法。当应用程序登录且服务器发送 Cookie 时,我调用“saveCookies”方法。然后,每次我使用“loadCookies”方法发出任何后续请求时,都会加载这些 cookie。代码如下:

GlobalParams.h

#import <Foundation/Foundation.h>


@interface GlobalParams : NSObject

// Your property settings for your variables go here
// here's one example:

@property (nonatomic, assign) NSURL *baseUrl;


// This is the method to access this Singleton class
+ (GlobalParams *)sharedInstance;

//saving cookies
- (void)saveCookies;
//loading cookies
- (void)loadCookies;

@end

GlobalParams.m

#import "GlobalParams.h"


@implementation GlobalParams

@synthesize myUserData,baseUrl;

+ (GlobalParams *)sharedInstance
{
// the instance of this class is stored here
static GlobalParams *myInstance = nil;

// check to see if an instance already exists
if (nil == dronnaInstance) {
myInstance = [[[self class] alloc] init];


myInstance.baseUrl = [NSURL URLWithString:@"http://www.test.dronna.com/"];

}
// return the instance of this class
return myInstance;
}


- (void)saveCookies{

NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: cookiesData forKey: @"sessionCookies"];
[defaults synchronize];

}

- (void)loadCookies{

NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"sessionCookies"]];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (NSHTTPCookie *cookie in cookies){
[cookieStorage setCookie: cookie];
}

}

关于iphone - 无法在 AFNetworking 客户端 IOS 5/6 中维护 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16985729/

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