views:

378

answers:

4

Hi Guys, 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.

+1  A: 
  1. you use nsmutableurlrequest to generate a web services request
  2. 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"];
ennuikiller
thanks for your urgent response. i'll try it.
molloy
I'd recommend to use keychain for storing user credentials, there is link to example in another comment.
Valerii Hiora
A: 

You have two choices: User Preferences or Core Data. Both are capable - Core Data might be overkill.

marcc
i should study on core data. i think that will be the hard but very nice...
molloy
A: 

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.

jspcal
+2  A: 

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

Niels Castle