2016-02-19 6 views
1

Il semble que chaque fois que je veux modifier et mettre à jour mon application, Apple a changé quelque chose qui me bloque de «laissez-moi juste modifier rapidement et télécharger mon nouvelle version'. Ils ont maintenant désactivé la possibilité d'utiliser NSURLConnection pour la montre. Je l'ai déjà utilisé pour la montre, mais il semble qu'Apple me demande maintenant d'utiliser NSURLSession et je n'arrive pas à comprendre comment cela fonctionne. Voici mon code, quelqu'un peut jeter un coup d'oeil et laissez-moi savoir comment l'ajuster afin qu'il fonctionne avec NSURLSession.Je veux changer mon code de données de poste en url de NSURLConnection à NSURLSession

- (void)someFunction:(NSString *)startTime { 
    // Download the json file 
    NSUserDefaults *prefs = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.domain.something"]; 
    NSURL *jsonFileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://aurlwithparameter%@&another%@", [prefs stringForKey:@"user_id"], startTime]]; 
    _starttedTime = startTime; 
    // Create the request 
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl]; 

    // Create the NSURLConnection 
    [NSURLConnection connectionWithRequest:urlRequest delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [self.delegate connectionTimedOutMakingTempBlock:true]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // Initialize the data object 
    _registeredUser = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // Append the newly downloaded data 
    [_registeredUser appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // Create an array to store the locations 

    // Parse the JSON that came in 
    NSUserDefaults *prefs = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.domain.something"]; 

    [prefs setObject:_starttedTime forKey:@"temp_start_time"]; 

    // Ready to notify delegate that data is ready and pass back items 
    if (self.delegate) 
    { 
     [self.delegate tempDidStart]; 
    } 
} 

}

Répondre

1

Vous pouvez utiliser l'extrait suivant. Le bloc completionHandler remplace les méthodes déléguées que vous utiliseriez avec NSURLConnection (par exemple -connectionDidFinishLoading: etc.).

// Create the request 
NSURLRequest *urlRequest = ...; 

NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *task = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

    // Handle success/error response here. 

}]; 
1
NSURLSession *session = [NSURLSession sharedSession]; 
     NSURLSessionDataTask *task = [session dataTaskWithRequest:request 
               completionHandler: 
             ^(NSData *data, NSURLResponse *response, NSError *connectionError) { 
              // ... 
              NSLog(@"coming inside session side"); 
              }]; 

C'est le meilleur lien, vous pouvez consulter cette

NSURLSESSION LINK