2016-05-11 10 views
0

Ich versuche, meinen Code von NSUrlConnection zu NSUrlSession zu aktualisieren. Die App läuft einwandfrei, keine Fehler, aber das Token wird nie in die MySQL-Datenbank hochgeladen ... Ich habe wirklich keine Ahnung? Soll ich Delegierte implementieren oder tue ich etwas Grundlegendes sehr falsch? :-)NSUrlSession iso NSUrlConnection - Geräte-Token auf eine MySQL-Datenbank hochladen

NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"]; 
//Information is only uploaded to SQLite database if 'deviceToken' is not null 
if ([deviceToken length] > 0) { 
    NSURL *url = [NSURL URLWithString:@"http://sampleUrl.com/do.php"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSString *varString = [NSString stringWithFormat:@"deviceToken=%@", deviceToken]; 

    NSData *requestData = [varString dataUsingEncoding:NSUTF8StringEncoding]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody: requestData]; 


    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 

    NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; 

    /*NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:requestData completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) { 
     //Handle 
    }];*/ 

    //NSURLSessionUploadTask *uploadTask2 = [session uploadTaskWithStreamedRequest:request]; 
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) { 
     //Handle 
    }]; 
    [dataTask resume]; 

Irgendwelche Ideen? Danke und Gruß, Tom

Antwort

0

löste ich das Problem:

NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"]; 

//Information is only uploaded to SQLite database if 'deviceToken' is not null 
if ([deviceToken length] > 0) { 
    // Create URL and URLRequest 
    NSURL *url = [NSURL URLWithString:@"http://url.com/something.php"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

    //Create the body 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSString *varString = [NSString stringWithFormat:@"deviceToken=%@", deviceToken"]]; 

    // Create POST parameters and add it to HTTPBody 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody: [varString dataUsingEncoding:NSUTF8StringEncoding]]; 

    // Set up a simple and asynchronous NSURLSession to send data to the php 
    // Set up the session configuration and header data 
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 

    [config setRequestCachePolicy:NSURLRequestUseProtocolCachePolicy]; 
    [config setHTTPAdditionalHeaders:@{ 
             @"Accept" : @"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 

             @"Content-Type" : @"application/x-www-form-urlencoded", 

             @"Content-Length" : [NSString stringWithFormat:@"%lu", (unsigned long)[[varString dataUsingEncoding:NSUTF8StringEncoding] length]] 
             }]; 

    // Create the session and the task 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; 
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) { 
     if (error) 
      NSLog(@"Connection failed! Error - %@ %@", 
        [error localizedDescription], 
        [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 

     if (response) 
     { 
      NSLog(@"response description: %@", response.description); 
      [prefs setBool:YES forKey:@"tokenSent"]; 
     } 

     if (data) 
      NSLog(@"data description: %@", data.description); 
    }]; 
    // Start the task 
    [task resume]; 
} 

Grüße, Tom