Since i'm new to iPhone programming, I want to do something like that: In the main screen of my app, i ask for username and password and authorize user using these info calling .net web service. In this main screen, there is a switch controller "Remember me" so that app 开发者_如何学运维will remember the user next time he run the app. My questions are: 1. how can i call .net web services in iPhone? 2. how can i persist these username and password information (like cookie in web app), so that user will not be asked for credential info? i would really appreciate, thanks.
Persisting user credentials
If you are storing a username and password you should use the Keychain. Using the Keychain on iPhone is analogous to Keychain on the Mac.
To use Keychain import "Security/Security.h".
Apple has an example here of adding and retrieving a username and password in their documentation. The example methods
- (void)mySetObject:(id)inObject forKey:(id)key;
- (id)myObjectForKey:(id)key;
- (void)resetKeychainItem;
will enable you to persist and retrieve your user credentials almost without modifying the example code.
Calling webservices that require authentication
Depending on you authentication scheme you can either
- provide username and password directly as parameters in the URL using
NSURLConnection
- provide a
didReceiveAuthenticationChallenge
method in yourNSURLConnection
delegate if your webservice use the "Basic Authentication" scheme
- you use nsmutableurlrequest to generate a web services request
- use nsuserdefaults to persist the username and password
Here's an example of the first:
-(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");
}
}
and an example of the second:
[[NSUserDefaults standardUserDefaults] setObject:self.userName.text forKey:@"Username"];
[[NSUserDefaults standardUserDefaults] setObject:self.password.text forKey:@"Password"];
You have two choices: User Preferences or Core Data. Both are capable - Core Data might be overkill.
NSURLRequest can be used to retrieve URLs such as your authentication service. And NsUserDefaults can be used to save settings in between app invocations.
Note: Check out ennuikiller's awesome example.
精彩评论