I would like to replicate the Stackoverflo开发者_如何转开发w "Ask Question" page, to be run natively on the iPhone. The view contains two text boxes (a title and a text section) and a submit button.
When I press the submit button, how do I send the contents of the view to Stackoverflow?
What relevant iOS classes do I need to use?
Is it possible to do this without any APIs? I want a generic solution for any website.
You can do something like this:
NSURL *url = [NSURL URLWithString:@"http://stackoverflow.com/questions/ask/submit"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *body = [NSStringstringWithFormat:@"title=%@&post-text=%@", title, question];
NSData *requestBody = [body dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestBody];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
White woking with StackOverflow in particular in a native iPhone app you'll have to use API:
https://stackapps.com/questions/1/api-documentation-and-help
精彩评论