2011-07-05 5 views
1

私はシンプルなゲーム(iPhoneアプリ)を作成しており、ユーザーはポイントに基づいてランク付けしてもらいたい。すべてのユーザーポイントを保存してから、最初にアプリが起動したときにユーザーにランクを割り当てる最善の方法は何ですか?私はサーバー(Webサイト)を持っているので、必要に応じてSQLを使用できます。何か案は?ユーザーの「ランク」を保存する最良の方法 - iPhone App

答えて

1

MySQLデータベースからのランク(複数可)を読み込むPHPページにアクセスするにはNSMutableURLRequestを使用することができますを行う必要はありません。 PHPの出力xmlを持って、結果を解析します。次のコードは、PHPページにリクエストを送信し、そのページから返されたxmlを解析します。 (DBエントリーなどを更新するために、別の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 

あなた自身の質問ランキングで、ユーザーのためにデータベースを検索するために必要なロジックを理解する必要があります。あなたは、(例えば)追加することによって、ログイン情報を渡すことができますか?ユーザー= USERNAME &パス=の.phpファイルの末尾にPASSWORD ...つまり...

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

をUSERNAMEとPASSWORDは値がサンドボックスから読み込まれますなど...あなたは、もちろん

-Chrisアリンソン

3

Apple Game Centerをご覧ください。それは(ほとんど)事前構築されたリーダーボードを含んでいます。

+0

(あなたがstringWithFormatで行うように)だけ4.1以降のデバイスで動作することをURLWithStringをフォーマットする必要があります。古い第一世代の人々には、それをサポートする価値があるかどうかを判断してください。もしそうなら、高得点をSQLサーバーに投稿してください。それらを並べ替え、スコアを保存したユーザーのIDを検索します。その後、対応するランクを返します。複数のスコアがある場合は、ランクを追加して別のソート済みリストに入れ、そこからランクを取得できます。 – FeifanZ

+0

さて、あなたはiOSバージョンの制限に関連することは言及していませんでした。以前のバージョンをサポートするのは価値があるのか​​どうかを判断する必要があります。特にiOS 5のほうがすぐです。 –

-1

ええこれであなたは、ユーザがソートを選択する必要があり、この画面に別の機能を使用することができます

あなたがフェッチやユーザー情報を表示していることを画面にランク機能を使用するには、私の意見では最良の選択肢..プログラミングのこの種で、decrementlyが得点により、あなたはAY余分な作業