How to get NSDictionary from NSURLSessionDataTask

jane
-(NSDictionary *)fetchFromUrl:(NSString *)url{
    NSURLRequest *request = [NSURLRequest  requestWithURL:[NSURL URLWithString:url]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                        completionHandler:
                              ^(NSData *data, NSURLResponse *response, NSError *error) {
                                  dataFetched = [NSJSONSerialization JSONObjectWithData:data
                                                                                           options:0
                                                                                             error:NULL];

                              }];
    [task resume];
    NSLog(@"dataFetched, %@", dataFetched);

    return dataFetched;
}

So I have tried putting the dataFetched as a global variable so I could access it around my .m file and make it accessible to other .m file but when I tried to NSLog the dataFetched from other .m file it outputs (null). Is there anyway I could make the data accessible throughout my other .m files that needed the data?

Nirav D

You need to use block with your method, instead of returning NSDictionary, So change your code like this.

First Change your method like this

-(void)fetchFromUrl:(NSString *)url withDictionary:(void (^)(NSDictionary* data))dictionary{ 
    NSURLRequest *request = [NSURLRequest  requestWithURL:[NSURL URLWithString:url]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {
                                      NSDictionary *dicData = [NSJSONSerialization JSONObjectWithData:data
                                                                                    options:0
                                                                                      error:NULL];
                                      dictionary(dicData);
                                  }];
    [task resume];          
}

Now call your method like this

[self fetchFromUrl:urlStr withDictionary:^(NSDictionary *data) {
    self.dataFetched = data;
    NSLog(@"data %@",data);
}];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get status code from NSURLSessionDataTask?

From Dev

How to get data to return from NSURLSessionDataTask

From Dev

How to get data to return from NSURLSessionDataTask in Swift

From Dev

how to get raw JSON response from AFnetworking 2.0 operation / NSURLSessioNDataTask?

From Dev

How to get key/value pair from NSDictionary?

From Dev

How to get JSON value from NSDictionary

From Dev

How to get key and value from an NSDictionary?

From Dev

how to get key of selected value from nsdictionary

From Dev

How to get the value for key @int from NSDictionary?

From Dev

How get a value from NSDictionary and move it to an NSArray

From Dev

Get a value from nsdictionary

From Dev

Get values from NSDictionary

From Dev

How to get key of NSDictionary

From Dev

How to get the asosciated id from an NSDictionary in a JSON response

From Dev

get value from NSDictionary not work

From Dev

Get NSString as NSArray from NSDictionary

From Dev

Needing to get an object from an NSDictionary

From Dev

How to use NSURLSessionDataTask in Swift

From Dev

Unable to get values from NSDictionary and JSON

From Dev

Get float value out from NSDictionary

From Dev

Want to get key value from NSDictionary

From Dev

Get a specific string from an NSMutableArray using NSDictionary

From Dev

Get object from array with NSDictionary objects

From Dev

Get float value out from NSDictionary

From Dev

iOS: Get String from NSDictionary in Swift

From Dev

Get object by key with certain value from NSDictionary

From Dev

How to filter NSDictionary by value and create new NSDictionary from that?

From Dev

how to make a new NSDictionary from a NSDictionary where key = x

From Dev

Does NSURLSessionDataTask get paused on background?

Related Related

  1. 1

    How to get status code from NSURLSessionDataTask?

  2. 2

    How to get data to return from NSURLSessionDataTask

  3. 3

    How to get data to return from NSURLSessionDataTask in Swift

  4. 4

    how to get raw JSON response from AFnetworking 2.0 operation / NSURLSessioNDataTask?

  5. 5

    How to get key/value pair from NSDictionary?

  6. 6

    How to get JSON value from NSDictionary

  7. 7

    How to get key and value from an NSDictionary?

  8. 8

    how to get key of selected value from nsdictionary

  9. 9

    How to get the value for key @int from NSDictionary?

  10. 10

    How get a value from NSDictionary and move it to an NSArray

  11. 11

    Get a value from nsdictionary

  12. 12

    Get values from NSDictionary

  13. 13

    How to get key of NSDictionary

  14. 14

    How to get the asosciated id from an NSDictionary in a JSON response

  15. 15

    get value from NSDictionary not work

  16. 16

    Get NSString as NSArray from NSDictionary

  17. 17

    Needing to get an object from an NSDictionary

  18. 18

    How to use NSURLSessionDataTask in Swift

  19. 19

    Unable to get values from NSDictionary and JSON

  20. 20

    Get float value out from NSDictionary

  21. 21

    Want to get key value from NSDictionary

  22. 22

    Get a specific string from an NSMutableArray using NSDictionary

  23. 23

    Get object from array with NSDictionary objects

  24. 24

    Get float value out from NSDictionary

  25. 25

    iOS: Get String from NSDictionary in Swift

  26. 26

    Get object by key with certain value from NSDictionary

  27. 27

    How to filter NSDictionary by value and create new NSDictionary from that?

  28. 28

    how to make a new NSDictionary from a NSDictionary where key = x

  29. 29

    Does NSURLSessionDataTask get paused on background?

HotTag

Archive