我正在创建一个简单的游戏(iPhone应用程序),我希望用户根据积分进行排名。在应用程序首次启动时,存储所有用户点然后为用户分配排名的最佳方法是什么?我有一个服务器(一个网站),所以如果需要我可以使用SQL。有什么想法吗?
答案 0 :(得分:3)
我建议你看一下Apple Game Center。它包含(几乎)预建的排行榜。
答案 1 :(得分:1)
您可以使用 NSMutableURLRequest 来访问从mySQL数据库读取排名的php页面。让php输出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
您必须在自己排名的问题中找出为用户搜索数据库所需的逻辑。您可以通过将(例如)?user = USERNAME&amp; pass = PASSWORD附加到.php文件的末尾来传递登录信息...即...
[request setURL:[NSURL URLWithString:@"http://www.yourDomain.com/yourPhpPage.php?user=USERNAME&pass=PASSWORD"]];
USERNAME和PASSWORD将是从沙箱等读取的值...您需要格式化URLWithString(就像使用stringWithFormat一样)
-Chris Allinson
答案 2 :(得分:-1)
是的,在我看来,这是在该屏幕中使用排名功能获取或查看用户信息的最佳选择。
你可以在这个屏幕上使用不同的功能,你必须选择那些按比例排序的用户,通过这种编程你不需要做额外的工作