2013-07-21 4 views
0

Pour envoyer la requête HTTP, je me sers NSURLConnection comme ceci:créer une notification de connectionDidFinishLoading

NSURLConnection *connection = [[NSURLConnection alloc] 
           initWithRequest:request 
           delegate:self 
           startImmediately:YES]; 

A la fin de connectionDidFinishLoading, je dois poster des notifications différentes, en fonction de la requête HTTP qui vient de se terminer . Cependant à l'intérieur connectionDidFinishLoading Je n'ai pas un identifiant logique clairement le type de la demande qui a été envoyé:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

// here i want to post various notifications, depending on the HTTP request that was completed 


} 

Quelle est la meilleure solution ici? Merci!

+0

Vous pouvez sous-classe NSURLConnection pour stocker des propriétés supplémentaires qui sont nécessaires dans le rappel. Voir http://stackoverflow.com/questions/8331303/using-custom-subclass-of-nsurlconnection-how-does-it-find-the-additional-data pour un exemple. –

Répondre

0

Connexion a la méthode d'arrivée passe l'objet NSURLConnection, qui a la demande et url:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSLog(@"currentRequest: %@", connection.currentRequest); 
    NSLog(@"originalRequest: %@", connection.originalRequest); 

    // here do a if statement that compares url 
    if ([connection.currentRequest.URL.absoluteString isEqualToString:@"http://google.co.uk"]) { 
     NSLog(@"Equal to google"); 
     // post notification 
    }  
} 
+0

Merci, je l'ai compris :-) Mais l'URL en elle-même n'est pas toujours suffisamment informative pour corréler avec une requête spécifique. Y a-t-il des informations plus explicites que je peux transmettre à cette méthode pour l'identifier? –

+0

je ne peux penser qu'aux champs d'en-tête http –

0

Vous pouvez utiliser comme cadre MKNetworkKit. Dans ce cadre, vous pouvez écrire comme ceci:

- (void) sendRequest: (NSString*) aRequestPath 
      httpMethod: (NSString*) aHttpMethod 
     paramsBlock: (SMFillParametersForRequestBlock) aParamsBlock 
     successBlock: (SMSaveRequestResultBlock) aSuccessBlock 
      errorBlock: (SMErrorRequestResultBlock) aErrorBlock 
      userInfo: (id) anUserInfo 
{ 

    MKNetworkEngine* network_engine= [[MKNetworkEngine alloc] initWithHostName: MuseumsHostName]; 

    NSMutableDictionary* params = [[NSMutableDictionary alloc] init]; 

    if (aParamsBlock) 
    { 
     aParamsBlock(params); 
    } 

    MKNetworkOperation* operation = [network_engine operationWithPath: aRequestPath 
                   params: params 
                  httpMethod: aHttpMethod 
                    ssl: NO]; 
    [operation onCompletion: ^(MKNetworkOperation* completedOperation){ 

     // parse response of current request: 
     aSuccessBlock(completedOperation, anUserInfo, ...); 

    } onError: ^(NSError *error){ 

     // error handler: call block 

    }]; 

    [network_engine enqueueOperation: operation]; 
} 

Croyez-moi, c'est la meilleure solution

Questions connexes