1

And it says use [NSURLSession sharedsession] dataTaskwithRequest:request completionHandler:]

So here my code:

 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

I changed above code to like this:

 NSData *returnData = [[NSURLSession  sharedSession] dataTaskWithRequest:request completionHandler:nil];

I got 2 warning:

  1. Null passed to a callee that requirs a non-null argument

  2. Incomplete pointer type initializing 'NSData' with a expression of type 'NSURLSession'

Help me out.Please do explain me with code that will helpfull to understand. I am new to ios

My actual code:

   -(void)getdata {

    NSString *userName = @“user@yahoo”;
    NSString *password = @“passr”;
    NSData *plainData = [password dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];
    base64String=[self sha256HashFor: base64String];

    NSString *urlString = @"https://api.eaxmpleurl/files";

    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"GET"];



    NSString *authStr = [NSString stringWithFormat:@"%@:%@", userName, base64String];
    NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];


    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];

       [request setValue:authValue forHTTPHeaderField:@"Authorization"];










    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]
                                      dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        // Use the data here







        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


        NSError * error1;

        self->arrayPDFName = [[NSMutableArray alloc]init];
        NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];


        NSDictionary *dictOriginal = jsonResults[@"dark”];
    [titleArray addObject:[NSString stringWithFormat:@" Dark(%@)”, dictOriginal[@"count"]]];


    NSDictionary *dictOriginal2 = jsonResults[@"opey”];
    [titleArray addObject:[NSString stringWithFormat:@" Opey(%@)”, dictOriginal2[@"count"]]];



    NSDictionary *dictOriginal3 = jsonResults[@"pef”];
    [titleArray addObject:[NSString stringWithFormat:@" Pef(%@)”, dictOriginal3[@"count"]]];


    NSDictionary *dictOriginal4 = jsonResults[@"sdf”];
    [titleArray addObject:[NSString stringWithFormat:@" Sdf(%@)”, dictOriginal4[@"count"]]];


        NSArray *arrayFiles = [NSArray arrayWithObjects: dictOriginal, dictOriginal2, dictOriginal3, dictOriginal4, nil];


        NSLog(@"str: %@", titleArray);


        for (NSDictionary *dict in arrayFiles) {
            NSMutableArray *arr = [NSMutableArray array];

            NSArray *a = dict[@"files"];
            for(int i=0; i < a.count; i ++) {


                NSString *strName = [NSString stringWithFormat:@"%@",[[dict[@"files"] objectAtIndex:i] valueForKey:@"name"]];
                // NSLog(@"str: %@", strName);
                [arr addObject:strName];
            }
            [arrayPDFName addObject:arr];
        }











                NSString *errorDesc;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory1 = [paths objectAtIndex:0];
        NSString *plistPath = [documentsDirectory1 stringByAppendingPathComponent:@"SampleData.plist"];


        NSString *error2;


        data  = [ NSPropertyListSerialization dataWithPropertyList:jsonResults format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];


        if(data ) {
            if ([data  writeToFile:plistPath atomically:YES]) {
                NSLog(@"Data successfully saved.");
            }else {
                NSLog(@"Did not managed to save NSData.");
            }
        }
        else {
            NSLog(@"%@",errorDesc);
        }



        NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];




#pragma unused (stringsDictionary)
#pragma unused (error1)

#pragma unused (str)








    }];

    // Starting the task
    [dataTask resume];




}
jj1
  • 135
  • 2
  • 11

1 Answers1

2

You need to use that method like:

// Creating a data task
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]
  dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
  // Use the data here
}];

// Starting the task
[dataTask resume];

Please check dataTaskWithRequest:completionHandler: for more detailed information.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • see my upadte,Actual data is that.How i need to insert your code there.Plese do in code explain.I am new bie – jj1 Nov 04 '15 at 18:47
  • @jj1: This line `NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];` should be inside that completionHandler. Check this tutorial : http://www.raywenderlich.com/67081/cookbook-using-nsurlsession – Midhun MP Nov 04 '15 at 18:50
  • please see my code. If i add that line alone my below all code are getting error – jj1 Nov 04 '15 at 19:28
  • @jj1: Put all the code after [dataTask resume]; inside the block (where I put use the data here) and replace returnData with data and check – Midhun MP Nov 04 '15 at 19:37
  • does i need to this line returnData also with data `NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];` – jj1 Nov 04 '15 at 19:44
  • Actually i am having one doubt.I need to remove this warning alone.If i change the deployment target to ios 7.Then it will run on both ios 9 version `simulator`, `device`, `ipod` – jj1 Nov 04 '15 at 19:45
  • Why are you still calling sendSynchronous request ? The above code is a replacements for that – Midhun MP Nov 04 '15 at 19:52
  • i should not use that line ?? – jj1 Nov 04 '15 at 19:52
  • No, you don't. I think you should read the tutorial I have linked with the answer, so that you will get a better understanding – Midhun MP Nov 04 '15 at 19:54