2009-12-25 62 views
2

由于我是iPhone编程的新手,我想要这样做:在我的应用程序的主屏幕中,我要求用户名和密码,并授权用户使用这些信息调用.net Web服务。在这个主屏幕中,有一个开关控制器“记住我”,以便应用程序在下次运行应用程序时记住用户。我的问题是: 1.我如何在iPhone中拨打.net网络服务? 2.我如何坚持这些用户名和密码信息(如cookie在Web应用程序中),以便用户不会被要求提供凭据信息? 我真的很感激,谢谢。iPhone中的用户凭证持久性

回答

1
  1. 您使用nsmutableurlrequest生成一个Web服务请求
  2. 使用NSUserDefaults的坚持的用户名和密码,

这是第一个示例:

-(void)sendUpdate { 

    User *user = [User sharedManager]; 

    NSData *parkedLocation = [[NSString stringWithFormat:@"<location><latitude>%f</latitude><longitude>%f</longitude><userid>%@</userid><udid>%@</udid></location>", 
           coordinate.latitude, coordinate.longitude, userid, user.udid] dataUsingEncoding:NSASCIIStringEncoding]; 
    //NSLog("parkedlocation = %@", parkedLocation); 
    NSString *URLstr = LOCATIONS_URL; 
    if (controller.put_url) 
     URLstr = controller.put_url; 
    NSURL *theURL = [NSURL URLWithString:URLstr]; 
    //NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0]; 
    NSLog(@"IN SEND UPDATE put_url = %@", controller.put_url); 

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0]; 
    if (controller.put_url) { 
     [theRequest setHTTPMethod:@"PUT"]; 
    } else { 
     [theRequest setHTTPMethod:@"POST"]; 
    } 
    [theRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; 
    [theRequest setHTTPBody:parkedLocation]; 

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE]; 

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if (!theConnection) { 
     NSLog(@"COuldn't get a connection to the iParkNow! server"); 
    } 
} 

的一个例子,第二个:

[[NSUserDefaults standardUserDefaults] setObject:self.userName.text forKey:@"Username"]; 
     [[NSUserDefaults standardUserDefaults] setObject:self.password.text forKey:@"Password"]; 
+0

感谢您的紧急回复。我会试试看。 – molloy 2009-12-25 02:35:11

+0

我建议使用钥匙串来存储用户证书,有链接到另一个评论中的例子。 – 2009-12-25 07:18:00

0

您有两种选择:用户首选项或核心数据。两者都有能力 - 核心数据可能是矫枉过正的。

+0

我应该学习核心数据。我认为这将是艰难但非常好的...... – molloy 2009-12-26 22:07:07

0

NSURLRequest可用于检索URL,如您的验证服务。并且NsUserDefaults可用于保存应用程序调用之间的设置。

说明:查看ennuikiller的好例子。

2

坚持用户凭据

如果要存储的用户名和密码,你应该使用钥匙扣。在iPhone上使用钥匙串与Mac上的钥匙串类似。

使用钥匙串导入“Security/Security.h”。

Apple有一个示例here在他们的文档中添加和检索用户名和密码。示例方法

- (void)mySetObject:(id)inObject forKey:(id)key; 
    - (id)myObjectForKey:(id)key; 
    - (void)resetKeychainItem; 

将使您能够坚持和检索您的用户凭证,而无需修改示例代码。

调用需要认证的

根据您的身份验证方案,web服务您可以