views:

618

answers:

1

Hi All,
Have a look to this code snippet:-

CommonService objcommonService;
NetworkCredential myCredentials = new NetworkCredential("144552", "F@mous123456");
objcommonService.Credentials = myCredentials;

This is a C#.net code. In this snippet "objcommonService " is a object of the webservice "CommonService". This webservice pre authenticates the user against its supplied credentials.In C#.NET NetworkCredential is a standard class of the .NET .

If I am trying to hit a webservice which requires these credentials in my iPhone app , How should I pass?

Is there any way to do it so that the user of my app will not face any difficulty like "Authentication Failed !!" when he/she uses my app. The webservice pre authenticates the user.And I need to pass my credentials as my app calls for the service.

Suggest me a way...
Thank you All.

+3  A: 

I implement the auth stuff by using the following delegate method in 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

James Raybould
Thank you James
socialCircus