嗨,我在我的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”->>向服务器发出以下请求



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类中答案中列出的方法,该类包含所有Global参数和方法。
当应用程序执行登录并且服务器发送Cookies时,我将调用“ 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;

- (void)saveCookies;
- (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];
        }

    }

@end




然后在定义NSMutableRequest之后,在我所有的服务器调用中都将“ [[GlobalParams sharedInstance] saveCookies]”或“ [[GlobalParams sharedInstance] loadCookies]”(取决于cookie中的读/写)称为“ [[GlobalParams sharedInstance] saveCookies]”。
希望对您有所帮助。

最佳答案

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

我初始化了GlobalParams类中答案中列出的方法,该类包含所有Global参数和方法。当应用程序执行登录并且服务器发送Cookies时,我将调用“ 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/

10-10 17:35