2011-07-05 32 views
1

我正在创建一个简单的游戏(iPhone应用程序),我希望用户可以根据点数进行排名。当应用第一次启动时,存储所有用户点数然后为用户分配级别的最佳方式是什么?我有一个服务器(一个网站),所以我可以根据需要使用SQL。有任何想法吗?存储用户“级别”的最佳方式 - iPhone App

回答

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 &通= PASSWORD到PHP文件的末尾... IE浏览器...

[request setURL:[NSURL URLWithString:@"http://www.yourDomain.com/yourPhpPage.php?user=USERNAME&pass=PASSWORD"]]; 

用户名和密码将被值在沙箱阅读等等......你需要格式化URLWithString(像你这样做stringWithFormat)

克里斯爱莲心

3

我建议你看看Apple Game Center。它包含(几乎)预先构建的排行榜。

+0

当然,这仅适用于4.1或更高版本的设备。对于较早的第一代,决定是否值得努力支持他们。如果是这样,请将高分发布到SQL服务器中。对它们排序并搜索您存储分数的用户标识。然后返回相应的等级。如果有多个分数,您可以添加这些等级,将它们放入另一个排序列表中,并从那里获得排名。 – FeifanZ

+0

好吧,你没有提到任何与iOS版本限制有关的东西。你需要决定是否值得支持旧版本,特别是iOS 5版本。 –

-1

是啊,这是我认为最好的选择是使用RANK函数在屏幕上你在哪里取或查看用户信息...

,你可以使用不同的功能,此屏幕,你必须选择用户分类通过这种编程,你不必做额外的工作