我正在創建一個簡單的遊戲(iPhone應用程序),我希望用戶可以根據點數進行排名。當應用第一次啓動時,存儲所有用戶點數然後爲用戶分配級別的最佳方式是什麼?我有一個服務器(一個網站),所以我可以根據需要使用SQL。有任何想法嗎?存儲用戶「級別」的最佳方式 - iPhone App
1
A
回答
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。它包含(幾乎)預先構建的排行榜。
-1
是啊,這是我認爲最好的選擇是使用RANK函數在屏幕上你在哪裏取或查看用戶信息...
,你可以使用不同的功能,此屏幕,你必須選擇用戶分類通過這種編程,你不必做額外的工作
相關問題
- 1. iphone-存儲數據的最佳方式
- 2. 最佳方式必須存儲在sqlite的iPhone應用程序
- 3. 在iPhone上保存用戶登錄信息的最佳方式
- 4. 在App Engine Blob存儲中存儲圖像的最佳方式是什麼?
- 5. 存儲朋友的每個用戶列表的最佳方式
- 6. 在Android中存儲用戶數據的最佳方式
- 7. 存儲用戶期望語言的最佳方式
- 8. 存儲用戶權限的最佳方式是什麼?
- 9. 存儲用戶設置的最佳方式
- 10. Play Framework 2存儲用戶密碼哈希的最佳方式
- 11. 存儲關注用戶的最佳方式
- 12. 存儲用戶推特關注者數據的最佳方式
- 13. 在MySQL中存儲用戶「收藏夾」的最佳方式
- 14. Symfony2存儲用戶數據的最佳方式?
- 15. 在iPhone上存儲數據的最佳方式是什麼?
- 16. 存儲數據的最佳方式
- 17. iPad - 存儲數據的最佳方式
- 18. 存儲配置的最佳方式
- 19. 存儲數據的最佳方式
- 20. 以C存儲ROE的最佳方式#
- 21. 存儲點列表的最佳方式
- 22. 存儲文件的最佳方式
- 23. 存儲輸入值的最佳方式
- 24. 存儲彙總值的最佳方式
- 25. 在SQL中存儲標籤,類別和int []的最佳方式
- 26. 最佳方式將數據存儲在iPhone
- 27. 最佳方式存儲上傳圖像
- 28. 最佳方式爲用戶
- 29. 在客戶端存儲Laravel Passport Token的最佳方式Angular2/Angular4
- 30. 存儲數據客戶端的最佳方式
當然,這僅適用於4.1或更高版本的設備。對於較早的第一代,決定是否值得努力支持他們。如果是這樣,請將高分發布到SQL服務器中。對它們排序並搜索您存儲分數的用戶標識。然後返回相應的等級。如果有多個分數,您可以添加這些等級,將它們放入另一個排序列表中,並從那裏獲得排名。 – FeifanZ
好吧,你沒有提到任何與iOS版本限制有關的東西。你需要決定是否值得支持舊版本,特別是iOS 5版本。 –