1

I have a function which invokes an azure REST API.

    - (void)authenticateUser:(NSString*)usernameString
passwordString: (NSString*)passwordString{
    self.client = [MSClient clientWithApplicationURLString:@"url" applicationKey:@"url"];

    [self.client invokeAPI:@"AuthenticateAndFetchData"
                      body:nil
                HTTPMethod:@"GET"
                parameters:@{ @"userid": usernameString, @"password" : passwordString }
                   headers:nil
                completion: ^(NSData *result, NSHTTPURLResponse *response, NSError *error) {
                    if(error) {
                        NSLog(@"ERROR %@", error);
                    } else {

                        NSLog(@"%@", result);
                    }
                }];

}

This code is inside a function. I want the JSON object received in the result object to be returned as an NSData so that I can use this object in other classes and parse the data. Can anyone help me with this?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
ash007
  • 311
  • 4
  • 24
  • Convert the json to nsdata http://stackoverflow.com/questions/26840736/converting-json-to-nsdata-and-nsdata-to-json-in-swift – Diksha Dec 28 '16 at 07:10
  • I had a different question, I want to access the result object inside completion block outside the function. – ash007 Dec 28 '16 at 07:14
  • you can make model object for that and then make golbal array with objects and can use all over the project – Diksha Dec 28 '16 at 07:15
  • Put a break point inside the completion block and check the value of *result*. It should contain the appropriate value as `NSData`. What seems to be the problem? Is it *nil*? or is the completion block never fired? Are you sure you are passing the `NSData` response value inside the completion block to where you are trying to print it? – Rikh Dec 28 '16 at 07:19
  • @AyushOjha For that you need to make completion handler with your function, check this answer to create completion handler with your function. http://stackoverflow.com/a/39138759/6433023 – Nirav D Dec 28 '16 at 07:19
  • @Rikh result is of type NSData. And i am successfully printing the json result in console. The problem is that I want to use this json string outside this completion block. So is there any way so that i can call this function and invoke the api to receive the json and parse it wherever I need to. – ash007 Dec 28 '16 at 07:30

1 Answers1

2
    - (void)authenticateUser:(NSString*)usernameString
passwordString: (NSString*)passwordString completeBlock:(void(^)(NSData *)) completeBlock
    {
        [self.client invokeAPI:@"fetchdata"
                          body:nil
                    HTTPMethod:@"GET"
                    parameters:@{ @"userid": usernameString, @"password" : passwordString }
                       headers:nil
                    completion: ^(NSData *result, NSHTTPURLResponse *response, NSError *error) {
                        if(error) {
                            NSLog(@"ERROR %@", error);
                        } else {
                            printf("%s", result);

                            if(completeBlock)
                                completeBlock(result);

                        }
                    }];
    }

call like this:

[self authenticateUser: @"username" passwordString: @"password" completeBlock:^(NSData * data){
        //do something here
    }];

Do you want this?

kai
  • 310
  • 3
  • 14
  • if(completeBlock) completeBlock(result); – ash007 Dec 28 '16 at 07:33
  • What is this part doing? Does it return the object? – ash007 Dec 28 '16 at 07:37
  • call the completeBlock and pass the object as parameter – kai Dec 28 '16 at 07:44
  • let me edit the code in the question. I am actually authenticating the user by sending his username and password through function parameters. usernameString and passwordString are the function parameters. – ash007 Dec 28 '16 at 07:51
  • You just add another completeBlock parameter. That isn't you need ? – kai Dec 28 '16 at 08:03
  • I am not exactly sure what this code does, But all I want to do is to get the NSData type object which contains the JSON, and then store that in a variable. Then I want to send that variable to another function to parse and extract the data out of it. I am a bit confused with the declaration part of your code. I am new to objective C so sorry for that. Thanks for your help though. – ash007 Dec 28 '16 at 08:15
  • input username and password ->call authenticateUser method -> requesting -> complete -> if no error , call completeBlock and pass result -> do anything as your code in completeBlock (like parse result data, send another request) – kai Dec 28 '16 at 08:34
  • i tried using this technique, But the function is not getting called, I simply printed the NSData in log but it doesn't show up. – ash007 Dec 29 '16 at 05:21