0

What i need is to login first using this function and than i'd like to e.g. call a function that show's the orders, depending on the user_id stored during the login in the $_SESSION variable on the Server using php.

Can anybody help me get this set? The login works flawlessly, but it seems like AFNetworking drops the session and doesn't persist it.

Thanks in advance.

NSURL *baseURL = [NSURL URLWithString:@"https://example.com/robot/"]; NSDictionary *parameters = @{@"eMail": eMail.text, @"pass" : password.text };

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];



AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
[manager initWithBaseURL:baseURL];



manager.responseSerializer = [AFJSONResponseSerializer serializer];


[manager POST:@"iPad.php?action=Login" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
    NSDictionary *json = responseObject;


    NSString *jsonstatus;
    jsonstatus = [json objectForKey:@"status"];


    if ([jsonstatus isEqualToString:@"100"]){
        NSLog(@"logged in");

}

NSURL *baseURL = [NSURL URLWithString:@"https://example.com/robot/"]; NSDictionary *parameters = @{@"query": @"" };

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];



AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
[manager initWithBaseURL:baseURL];


manager.responseSerializer = [AFJSONResponseSerializer serializer];


[manager POST:@"iPad.php?action=ShowOrders" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"hi");
    NSDictionary *json = responseObject;


    NSString *jsonstatus;
    jsonstatus = [json objectForKey:@"status"];
jdphenix
  • 15,022
  • 3
  • 41
  • 74
Jimmy Tschonga
  • 57
  • 1
  • 4
  • 8
  • What exactly is the problem or error you are experiencing? Your code makes a POST request to login. Do you have additional code that is submitting a GET request for the order data? – Paulw11 Mar 21 '14 at 21:14
  • Yes, I do have an additional function, which is: – Jimmy Tschonga Mar 21 '14 at 21:33
  • You can edit your question to add the additional code. Also any error message you receive – Paulw11 Mar 21 '14 at 21:34
  • The function is called, but if I make the server to display the session_id() it has changed. Therefore the showOrders function doesn't work and doesn't return any values. But the PHP functions do work, as they are also used on the Website bein triggered by javascript functions. – Jimmy Tschonga Mar 21 '14 at 21:36
  • Xcode doesn't throw out any errors. There are just no values returned as the SESSION is not persisting. – Jimmy Tschonga Mar 21 '14 at 21:37

1 Answers1

1

Your problem is caused by creating a new AFHTTPSessionManager object in your second block of code. You should create a single AFHTTPSessionManager for your application and initialise the baseURL. You can then execute a number of transactions against the same manager.

Your second block of code should just be

[manager POST:@"iPad.php?action=ShowOrders" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"hi");
NSDictionary *json = responseObject;


NSString *jsonstatus;
jsonstatus = [json objectForKey:@"status"];

UPDATE

There is another issue in your initialisation of the AFHTTPSessionManager you should address

First you call

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];

then you call

[manager initWithBaseURL:baseURL];

This second call will reinitialise your session manager without the session configuration. You should call the combined initialiser like so -

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaeURL:baseURL sessionConfiguration:sessionConfiguration];

I also set up a small test on a PHP server and the improper configuration didn't prevent the session from operating correctly, however what did reproduce your problem was allowing the requests to complete too quickly. You need to ensure that your login request has completed before you issue the next request or the second request will occur in a new session as AFHTTPSession doesn't serialise requests - they are executed asynchronously. How are you ensuring that the login task has completed before you issue the showOrders request?

You can use a queue of requests to ensure they operate sequentially - see this answer - AFNetwork 2.0 Queue with completion block?

Community
  • 1
  • 1
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • So if I have different views to call the functions from; how can I implement the manager in the app delegate and make it available on all views? How can I call it then? Thank u – Jimmy Tschonga Mar 21 '14 at 21:47
  • Shouldn't it be working smth like that: viewManager = [(AppDelegate *)[UIApplication sharedApplication].delegate manager]; – Jimmy Tschonga Mar 21 '14 at 21:57
  • Yes, a property on the appDelegate is probably the simplest approach – Paulw11 Mar 21 '14 at 22:01
  • I did include the following code in the AppDelegate.h – Jimmy Tschonga Mar 21 '14 at 22:05
  • NSURL *baseURL = [NSURL URLWithString:@"https://xxx.com/robot/"]; NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:sessionConfiguration]; [manager initWithBaseURL:baseURL]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; – Jimmy Tschonga Mar 21 '14 at 22:06
  • Can you please tell me how to refer to the manager inside the Views? – Jimmy Tschonga Mar 21 '14 at 22:06
  • Your AFHTTPSessionManager is still a local variable. You need to make it a property of your AppDelegate and assign it with self.manager . Then you can refer to it as per your previous comment – Paulw11 Mar 21 '14 at 22:07
  • I put the following in the AppDelegate.h: @property (strong, nonatomic) AFHTTPSessionManager *manager; Now i can refer to it, but sadly the function still isn't working. – Jimmy Tschonga Mar 21 '14 at 22:14
  • You need to ensure that the AFHTTPSessionManager class is keeping the cookies that your application requires to respect the login. Sometimes a login method will return a session key that you need to supply on subsequent calls - you will need to consult the web service documentation – Paulw11 Mar 21 '14 at 22:20
  • Actually this shouldn't be necessary. I am just updating an existing application from ASIHttpRequest to AFNetworking. Within ASI u could just set the session/connection to persistent and it worked perfectly. Right now the login doesn't work at all if I try to initiate it in the app delegate and just call the [delegate.manager POST:@"iPad.php?action=Login" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject)... function within the view. – Jimmy Tschonga Mar 21 '14 at 22:25
  • So why are you moving from a framework that works to one that doesn't? – Paulw11 Mar 21 '14 at 22:33