2

En fait, j'utilise maintenant des classes JSON pour appeler des services Web, mais maintenant je veux appeler ce webservice en utilisant NSURLConnection quelqu'un me fournir du code pour cela.Comment appeler webService en utilisant NSURLConnection dans ios

Veuillez me fournir les détails des cadres que je dois importer.

Merci d'avance.

+0

"NSURLConnection + JSON" ne pas vous donner des réponses? L'un des premiers liens sur Google: http://stackoverflow.com/questions/20995815/nsurlconnection-getting-json-file – Larme

+0

Vous pouvez utiliser la bibliothèque AFNetworking pour les appels de service Web. –

+1

Pourquoi utilisez-vous 'NSURLConnection'? Son obsolète dans iOS 9.0. Au lieu de cela, utilisez 'NSURLSession' pour la même chose. Vous pouvez référer ce joli [tutoriel sur NSURLSession] (https://www.objc.io/issues/5-ios7/from-nsurlconnection-to-nsurlsession/) par Mattt Thompson. –

Répondre

4
NSURL *url = [NSURL URLWithString:stringurl]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{ 
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
    NSLog(@"%@",dictionary); 
}]; 

Vous pouvez utiliser ceci.

+0

Mais fortement déconseillé pour le nouveau code. '' [[NSURLSession sharedSession] dataTaskWithURL: url completionHandler:^(données NSData * _Nullable, NSURLResponse * _Nullable réponse, NSError * _Nullable error) {...}]; '' est préférable. – dgatwood

3

Vous pouvez faire comme ceci à l'aide synchrone:

NSURL *url=[NSURL URLWithString:urlString]; 
NSURLRequest *req=[NSURLRequest requestWithURL:url]; 
NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil]; 
NSString *response=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 
NSDictionary *dd=[response JSONValue]; 

OU Utilisation délégué Méthode

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 
NSURLResponse *response = nil; 


// Create url connection and fire request 
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
     [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 


#pragma mark NSURLConnection Delegate Methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    // A response has been received, this is where we initialize the instance var you created 
    // so that we can append data to it in the didReceiveData method 
    // Furthermore, this method is called each time there is a redirect so reinitializing it 
    // also serves to clear it 

    _responseData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    // Append the new data to the instance variable you declared 
    NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 


    NSError* error; 
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                 options:kNilOptions 
                  error:&error]; 

    // NSArray* latestLoans = [json objectForKey:@"loans"]; 

    NSLog(@"json: %@", json); 


    [_responseData appendData:data]; 
} 

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection 
        willCacheResponse:(NSCachedURLResponse*)cachedResponse { 
    // Return nil to indicate not necessary to store a cached response for this connection 
    return nil; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // The request is complete and data has been received 
    // You can parse the stuff in your instance variable now 

} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    // The request has failed for some reason! 
    // Check the error var 
}