2011-07-05 46 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函數在屏幕上你在哪裏取或查看用戶信息...

,你可以使用不同的功能,此屏幕,你必須選擇用戶分類通過這種編程,你不必做額外的工作