您好我在AFNetworking
应用中使用IOS
,我无法维持会话。我正在使用AFNetworking
客户端并在服务器的所有请求中使用它。
我已经完成了以下问题:How to manage sessions with AFNetworking,但我不打算操纵Cookie或会话。我打算在应用程序的整个生命周期中实现一个会话。
我的AFNetworking客户端的.m如下
@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
我在调用“ - (void)searchBarSearchButtonClicked:(UISearchBar *)search” - >>时向服务器发出以下请求:
@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
有人可以指导我或指出我错在哪里吗? 任何帮助,将不胜感激。 提前致谢。
编辑::
我在以下帖子中找到了答案:https://stackoverflow.com/a/14405805/1935921
我初始化了GlobalParams类中答案中列出的方法,该类包含所有全局参数和方法。 当应用程序执行登录并且服务器发送Cookie时,我调用“saveCookies”方法。 然后,每次使用“loadCookies”方法发出任何后续请求时,都会加载这些cookie。该守则如下:
GlobalParams.h
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];
GlobalParams.m
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];
然后我在定义NSMutableRequest之后调用所有服务器调用中的“[[GlobalParams sharedInstance] saveCookies];”或“[[GlobalParams sharedInstance] loadCookies];”(取决于cookie中所需的读/写)。 我希望这有助于某人。
答案 0 :(得分:4)
我在以下帖子中找到了答案: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];
}
}