如何从iPhone应用程序中将用户名和密码传递给Web服务?

时间:2009-07-24 06:27:06

标签: iphone objective-c web-services macos

查看此代码段: -

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

这是一个C#.net代码。在此代码段中,“objcommonService”是网络服务“CommonService”的对象。此Web服务根据其提供的凭据对用户进行身份验证。在C#.NET NetworkCredential中是.NET的标准类。

如果我想在我的iPhone应用中点击需要这些凭据的网络服务,我应该如何通过?

有没有办法做到这一点,以便我的应用程序的用户不会遇到任何困难,如“身份验证失败!!”当他/她使用我的应用程序时。 webservice预先验证用户身份。我需要在我的应用程序调用服务时传递我的凭据。

1 个答案:

答案 0 :(得分:4)

我通过在URLConnection

中使用以下委托方法来实现auth的东西
-(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
}

}

希望这有帮助

相关问题