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.
A开发者_JS百科re 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];
精彩评论