简体   繁体   中英

Can I log in to my site from my iPhone app?

I'm trying to make a log in or sign up feature for my web site in my iPhone app. My website is a content management system, and like any other CMS, it has log in and registration features. It also has permmissions, dependent on the user account. I think I would have to use UIWebView for this.

Are there any examples or tutorials I can examine?

Check out the documentation for NSURLRequest (and NSMutableURLRequest ): you can use it to make a POST request to your login and registration pages, just like a web browser. You can write the form UI in Cocoa/Objective-C and then send the data to the server.

As far as displaying the result to the user, you'll have to figure out a way to either parse the returned HTML (bad idea) or modify your CMS to return JSON or XML to iPhone requests (better idea).

Edit: Here's some sample code, taken from an app I'm working on (it submits data to Last.fm using POST ):

NSURL *url = [NSURL URLWithString:@"http://example.com/"];
NSString *str = @"This is my example data!";

// everything below here is directly from my app:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:kLastFMClientUserAgent forHTTPHeaderField:@"User-Agent"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setHTTPShouldHandleCookies:NO];

*connection = [[NSURLConnection alloc] initWithRequest:request
                                              delegate:self
                                      startImmediately:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM