Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use an <strong>NSMutableURLRequest</strong> to access a php page that reads the ranking(s) from a mySQL database. Have the php output xml and parse the result. The following code sends a request to a php page, then parses the xml that is returned by that page. (You could also post data to a different php page to update DB entries, etc.).</p> <pre><code>//IN THE .h class (of a viewController) ... : UIViewController { //I use a label to display the data IBOutlet UILabel *label1; //Create global variable NSString *tempString; //Dataset for response from HTTP Request NSMutableData *receivedData; NSXMLParser *xmlParser; } -(void) initiateAPIConnection; @property (nonatomic, retain) NSString *tempString; @property (nonatomic, retain) UILabel *label1; @end //IN THE .h class //IN THE .m class //... @synthesize label1, tempString; //... -(void)initiateAPIConnection{ NSString *post = [NSString stringWithFormat:@"user=Chris"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:@"http://www.yourDomain.com/yourPhpPage.php"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setTimeoutInterval:10.0]; //fail after 10 seconds with no response [request setHTTPBody:postData]; NSURLConnection *conn=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; if (conn){ NSLog(@"In if conn"); receivedData = [[NSMutableData data] retain]; NSLog(@"End of if conn"); } else{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Conn error" message:@"No Server" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; [alert release]; } }//initiateAPIConnection -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; [alert release]; } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response; NSLog(@"%i",[urlResponse statusCode]); [receivedData setLength:0]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [receivedData appendData:data]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ xmlParser = [[NSXMLParser alloc]initWithData:receivedData]; [xmlParser setDelegate:self]; //you may want to enable these features of NSXMLParser. [xmlParser setShouldProcessNamespaces:NO]; [xmlParser setShouldReportNamespacePrefixes:NO]; [xmlParser setShouldResolveExternalEntities:NO]; [xmlParser parse]; } //XMLdeligate methods -(void)parserDidStartDocument:(NSXMLParser *)parser{ NSLog(@"Started Parsing"); } -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{ NSLog(@"Started Element name: %@", elementName); } -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ NSLog(@"Found characters: %@", string); tempString = string; } -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ NSLog(@"Finished Element name: %@", elementName); //my outputted xml would have &lt;ranking&gt;...ValueFromDb...&lt;/ranking&gt; in it's response if([elementName isEqualToString:@"ranking"]){ //display result in a label (you could save it to a local variable instead) label1.text = tempString; } } -(void)parserDidEndDocument:(NSXMLParser *)parser{ NSLog(@"Finished Parsing"); } //... //Don't forget to dealloc -(void)dealloc { //... [label1 release]; [tempString release]; [xmlParser release]; [receivedData release]; //... [super dealloc]; } //IN THE .m class </code></pre> <p>You'll have to work out the logic required to search the database for the user in questions ranking yourself. You can pass login information by appending (e.g.) ?user=USERNAME&amp;pass=PASSWORD to the end of the .php file ... i.e. ...</p> <pre><code>[request setURL:[NSURL URLWithString:@"http://www.yourDomain.com/yourPhpPage.php?user=USERNAME&amp;pass=PASSWORD"]]; </code></pre> <p>USERNAME and PASSWORD would be values read from the sandbox etc. ... you'll need to format the URLWithString (like you do with stringWithFormat)</p> <p>-Chris Allinson</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload