2009-07-24 8 views
0

Jetez un oeil à cet extrait de code: -Comment passer un nom d'utilisateur et un mot de passe à un service Web depuis une application iPhone?

CommonService objcommonService;
NetworkCredential myCredentials = new NetworkCredential("144552", "[email protected]");
objcommonService.Credentials = myCredentials;

Ceci est un code C# .net. Dans cet extrait "objcommonService" est un objet du webservice "CommonService". Ce service Web authentifie l'utilisateur par rapport à ses informations d'identification fournies. En C# .NET NetworkCredential est une classe standard du .NET.

Si j'essaie d'accéder à un service Web qui nécessite ces informations d'identification dans mon application iPhone, Comment puis-je passer?

Y at-il un moyen de le faire pour que l'utilisateur de mon application ne rencontrera aucune difficulté comme "Authentification a échoué !!" quand il/elle utilise mon application. Le webservice pré authentifie l'utilisateur. Et j'ai besoin de transmettre mes informations d'identification lorsque mon application appelle le service.

Répondre

4

je mets en œuvre les choses auth en utilisant la méthode déléguée suivante dans URLConnection

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ 

if ([challenge previousFailureCount] == 0) { 
    NSURLCredential *newCredential; 
    newCredential=[NSURLCredential credentialWithUser:username 
              password:password 
              persistence:NSURLCredentialPersistenceNone]; 
    [[challenge sender] useCredential:newCredential 
      forAuthenticationChallenge:challenge]; 

} else { 

    [[challenge sender] cancelAuthenticationChallenge:challenge]; 
    // inform the user that the user name and password 
    // in the preferences are incorrect 
} 

}

Hope this helps

Questions connexes