嗨,我AFNetworking
在我的IOS
应用程序中使用,我无法保持会话。我正在使用AFNetworking
客户端并在对服务器的所有请求中使用它。
我已经解决了以下问题:如何使用 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”时向服务器发出以下请求-->>
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];
谁能指导我或指出我错在哪里?任何帮助,将不胜感激。提前致谢。
编辑 ::
我在以下帖子中找到了答案:https ://stackoverflow.com/a/14405805/1935921
我在我的 GlobalParams 类中初始化了答案中列出的方法,其中包含所有全局参数和方法。当应用程序登录并且服务器发送 Cookies 时,我调用“saveCookies”方法。然后,每次我使用“loadCookies”方法发出任何后续请求时,都会加载这些 cookie。代码如下所示:
全球参数.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;
- (void)saveCookies;
- (void)loadCookies;
@end
全球参数.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];
}
}
@end
然后我在定义 NSMutableRequest 后的所有服务器调用中调用“[[GlobalParams sharedInstance] saveCookies];”或“[[GlobalParams sharedInstance] loadCookies];”(取决于 cookie 中所需的读/写)。我希望这对某人有所帮助。