[英]Best way to store a user's “rank” - iPhone App
我正在創建一個簡單的游戲(iPhone 應用程序),我希望用戶根據積分進行排名。 存儲所有用戶積分然后在應用首次啟動時為用戶分配排名的最佳方式是什么? 我有一個服務器(一個網站),所以如果需要我可以使用 SQL。 有任何想法嗎?
我建議你看看 Apple Game Center。 它包含(幾乎)預建的排行榜。
您可以使用NSMutableURLRequest訪問 php 頁面,該頁面從 mySQL 數據庫中讀取排名。 有 php output xml 並解析結果。 以下代碼向 php 頁面發送請求,然后解析該頁面返回的 xml。 (您還可以將數據發布到不同的 php 頁面以更新數據庫條目等)。
//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 <ranking>...ValueFromDb...</ranking> 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
您必須制定出在數據庫中搜索用戶對自己進行排名的問題所需的邏輯。 您可以通過將 (eg)?user=USERNAME&pass=PASSWORD 附加到 .php 文件的末尾來傳遞登錄信息...即...
[request setURL:[NSURL URLWithString:@"http://www.yourDomain.com/yourPhpPage.php?user=USERNAME&pass=PASSWORD"]];
USERNAME 和 PASSWORD 將是從沙箱等中讀取的值。...您需要格式化 URLWithString(就像使用 stringWithFormat 一樣)
——克里斯·阿林森
是的,我認為這是在您獲取或查看用戶信息的屏幕中使用排名 function 的最佳選擇。
您可以在此屏幕上使用不同的 function ,您必須對 select 用戶按分數遞減排序,通過這種編程,您不必做任何額外的工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.