Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 08-08-2009, 12:00 AM   #1 (permalink)
Tutorial Author
 
SimpleSDK's Avatar
 
Join Date: Jul 2009
Location: Orange County, CA
Posts: 25
SimpleSDK is on a distinguished road
Send a message via AIM to SimpleSDK
Default [Tutorial] Making PHP High Scores

Today I'm going to show you guys how to create a very simple high score system.

1. Make a new project. We're gonna use a View-based Application. Name it "PHP Scores".

2. Open up your view controller XIB file. Follow my video tutorial.

3. Go to Classes > PHP_ScoresViewController.h and add the following to your @interface statement.

Add below @implementation:
Code:
@synthesize countTimer, winAlert, loseAlert;
Code:
UIAlertView *winAlert;
UIAlertView *loseAlert;
NSTimer *countTimer;
NSUInteger time;
4. Click here to download the 4 PHP files that we will use to show the high scores. The files work on Apache, but depends on what web server you have.

5. Now extract the zip file and open up index.php. At the top of the file, you'll see:

HTML Code:
<title>"Game Name Here" Scores</title>
Replace "Game Name Here" with the name of your game. Now save your file.

6. Open up "post.php", and follow step 5 again, except with post.php instead of index.php.

7. Now you need to get a website host that has Apache on it. Hostmonster is paid, but works really well. I can't provide links to free sites right now.

8. Now make a directory named "scores" on your website wherever you want. this will host our PHP files.

9. Upload index.php, bar.png, posts.txt, post.php, and submit.php to the scores directory. We're now done with The PHP side.

Now we're moving on to the applications end. For this game, I have to clear all 5 boxes in less than 5 seconds.

10. Go to Classes > PHP_ScoresViewController.m. In the IBAction, pushButton1, enter the following code.

Code:
- (IBAction)push1 {
    button1.hidden = YES;
	
	if (button1.hidden == YES) {
		if (button2.hidden == YES) {
			if (button3.hidden == YES) {
				if (button4.hidden == YES) {
					if (button5.hidden == YES) {
						[countTimer invalidate];
					}
				}
			}
		}
	}
}
11. Now, Copy that code into the other IBAction's besides pushStart. Now change the top line to button#.hidden = YES. Then we stop the timer so it stops the time so we can freeze it to read the score.

# = Then button number we called it in Interface Builder.

12. Now we need To tell it if all buttons are hidden, pop up the alert view we declared in the .h to pop up with the different selections the user can perform. Indside the bottom "if" statement, copy this alert view code into each bottom if statement in each action.

Code:
winAlert = [[UIAlertView alloc] initWithTitle:@"You've won!" message:@"This gets covered." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Submit", @"High Scores", nil];
[winAlert show];
[winAlert release];
ABOVE: Setting up the alert when you win.

13. Lets start the code to start the game. Lets go to our "pushStart" action and type the following code.

Code:
time = 5.0;
countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
ABOVE: We're saying that time will equal 5 seconds when we hit start. That is setting the time that you have in the beginning. We then start a timer to our countDown statement we will add in the next step.

14. Now, we're going to tell time to countdown when we hit start. It will countdown since we added the NSTimer to "pushStart". Type the following code below the IBAction "pushStart".

Code:
- (void)countDown {
	time = time -1;
	[self updateLabel];
	
	if (time == 0) {
		time = time +0;
		[self updateLabel2];
	}
}
ABOVE: Telling time that time will equal time -1 stating that time will go down 1 second. We then tell it to start the updateLabel statement we will create in the next step. Next, we say that if time is 0, basically game over, to not count anymore, by adding zero.

15. Lets add the updateLabel statement. This will update the label to whatever time is. Type the following code below the countDown statement.

Code:
-(void)updateLabel {
	timeLabel.text = [NSString stringWithFormat:@"%i", time];
}
ABOVE: Setting the labels text to whatever time is by using %i to show time in the label.

16. Next, lets make our updateLabel2 statement. Copy the following code below the updateLabel statement.

Code:
-(void)updateLabel2 {
	countLabel.text = [NSString stringWithFormat:@"5", time];
	[countTimer invalidate];
}
ABOVE: Resetting the labels text to 5, but still hastime going invisibly since we added 0. Next we invalidate the timer which stops the counting.

17. Now go back to our countDown statement and in the "if time == 0" statement, add the following.

Code:
if (button1.hidden == NO) {
	loseAlert = [[UIAlertView alloc] initWithTitle:@"You've lost!" message:@"Better luck next time." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[loseAlert show];
	[loseAlert release];
}
		
if (button2.hidden == NO) {
	loseAlert = [[UIAlertView alloc] initWithTitle:@"You've lost!" message:@"Better luck next time." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[loseAlert show];
	[loseAlert release];
}
		
if (button3.hidden == NO) {
	loseAlert = [[UIAlertView alloc] initWithTitle:@"You've lost!" message:@"Better luck next time." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[loseAlert show];
	[loseAlert release];
}
		
if (button4.hidden == NO) {
	loseAlert = [[UIAlertView alloc] initWithTitle:@"You've lost!" message:@"Better luck next time." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[loseAlert show];
	[loseAlert release];
}
		
if (button5.hidden == NO) {
	loseAlert = [[UIAlertView alloc] initWithTitle:@"You've lost!" message:@"Better luck next time." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[loseAlert show];
	[loseAlert release];
}
ABOVE: Saying that if any one of the buttons aren't hidden, pop up the you lost alert view.

18. Go to Classes > PHP_ScoresViewController.h and add the following to our @interface.

Code:
UITextField *userName; //This goes in @interface
@property (nonatomic, retain) UITextField *userName; // Below bottom @property method
19. We are now going to add a text field to our win alert. This is where we will enter our username to send to our site. Type the following code in each of our winAlert statements above [winAlert show];

Code:
userName = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[userName setBackgroundColor:[UIColor whiteColor]];
[userName setBorderStyle:UITextBorderStyleRoundedRect];
userName.backgroundColor = [UIColor clearColor];
userName.returnKeyType = UIReturnKeyDone;
userName.keyboardAppearance = UIKeyboardAppearanceAlert;
[userName addTarget:self action:@selector(userNameReturnKey) forControlEvents:UIControlEventEditingDidEndOnExit];
[userName resignFirstResponder];
[winAlert addSubview:userName];
20. Now add the following code to detect which button in the alert view is pressed and its action. Paste this below updateLabel2.

Code:
- (void)alertView:(UIAlertView *)winAlert clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 1) {
		
	}
	
	if (buttonIndex == 2) {
		
	}
}
ABOVE: Adding our NSInteger, buttonIndex, to detect which button is pressed and its action.

buttonIndex 1 = Submit
buttonIndex 2 = High Scores

21. Connect to submit.php to send the score. Well, here we go! Type the following in the buttonIndex 1 statement.

Code:
NSString *score = [[NSString alloc] initWithFormat:[countLabel text]];
NSString *user = [[NSString alloc] initWithFormat:[userName text]];
NSString *urlToAuthPage = [[NSString alloc] initWithFormat:@"&username=%@&score=%@", user, score];
NSLog(urlToAuthPage);

NSData *postData = [urlToAuthPage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d",[urlToAuthPage length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://yourdomain.com/scores/submit.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
			
if (conn) {
	NSLog(@"Connection Successful");
} 
			
else {
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Connection" message:@"There was an error connecting" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
	[alert show];
	[alert release];
}
ABOVE: We convert our text field and label to an NSString. Now we create an NSLog that is put together well to be read in the Console to make sure the right outlet is being read. We then make our NSStrings ASCII Encoding language. We then create a URL request a connection to our PHP script on our server. Next, we locate the URL of our PHP script that submits the score, submit.php then tell it that its a form. We then make a connect to request that allows us to get into the script. Then an NSLog and alert view to say if there is an error or success.

22. Now follow this tutorial on switching views. We will switch to a view that holds a web view showing our scores. Use buttonIndex 2 statement.

23. In that tutorial add a web view to it and follow my tutorial here to load a page. The page is "http://yourdomain.com/scores/index.php".

Now you now have global scores for your app. Questions? Email me here.
__________________
-Alec
SimpleSDKŠ
YouTube | Website | Applications

Last edited by SimpleSDK; 11-26-2009 at 11:24 PM.
SimpleSDK is offline   Reply With Quote
Old 08-08-2009, 10:05 AM   #2 (permalink)
Tutorial Author
 
Join Date: Jan 2009
Posts: 144
meowmix23F is on a distinguished road
Default

Good tutorial.

However, whenever I used this, I'd just use a formatted NSString with the values encoded in the URL. Obviously there are many kinks to this, but it works fine:

For Example:

NSString *myURL = [[NSString alloc] initWithFormat:@"www.mywebsite.com/score.php?score=%d&username=%@",score,[name stringByReplacingOccurencesOfString:@" " withString:@"%20"]];

// my URL is the URL we want the contents of. Create a new method and detach it as a new thread. The method should do this:

NSString *result = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:myURL]];

NSLog(@"Response from server: %@",result);

Now, you can use NSXMLParser to parse it as an RSS feed if you have the PHP script return it in XML format.
meowmix23F is offline   Reply With Quote
Old 09-08-2009, 01:34 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 162
web20devxer is on a distinguished road
Default

Hello.
Any way you could make the project source code available for download?
Thanks!
web20devxer is offline   Reply With Quote
Old 09-30-2009, 11:12 PM   #4 (permalink)
Registered Member
 
kierster's Avatar
 
Join Date: Mar 2009
Location: Canada!
Posts: 261
kierster is on a distinguished road
Default Thanks!

This is exactly what I needed, thank you! Now all I need is to decide on using google app engine or buying a server? Im leaning to google
__________________
Check out some of my apps:
Boltz ($0.99)
FreeBoltz (FREE)
Cross Digits ($2.99) [Universal!]
Cross Digits Lite (FREE) [Universal!]
Targets ($0.99) (Facebook | YouTube Demo)
Greg's Apps
kierster is offline   Reply With Quote
Old 10-23-2009, 09:31 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: Cyprus
Posts: 13
krabos is on a distinguished road
Default The download link is dead

Can you please fix the download link of the 4 php files?
__________________
iLucky - coming soon
krabos is offline   Reply With Quote
Old 11-18-2009, 06:31 PM   #6 (permalink)
dot
Registered Member
 
Join Date: Feb 2009
Posts: 72
dot is on a distinguished road
Default

Quote:
Originally Posted by krabos View Post
Can you please fix the download link of the 4 php files?
bumping this
dot is offline   Reply With Quote
Old 11-22-2009, 11:48 AM   #7 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 3
appstudent is on a distinguished road
Default

hi i was not able to download the php zip file. was it taken down? if so, can we get a new link?
appstudent is offline   Reply With Quote
Old 11-24-2009, 06:01 PM   #8 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 32
magonicolas is on a distinguished road
Default

Please can someone help me with my specific game high score system?
Please.
My e mail is magonicolas@me.com
magonicolas is offline   Reply With Quote
Old 11-26-2009, 07:21 AM   #9 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 1
alien_roger is on a distinguished road
Default

Code:
if (!button1.hidden ||... || !button5.hidden) {
	loseAlert = [[UIAlertView alloc] initWithTitle:@"You've lost!" message:@"Better luck next time." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[loseAlert show];
	[loseAlert release];
}
maybe that looks better?
alien_roger is offline   Reply With Quote
Old 11-26-2009, 11:27 PM   #10 (permalink)
Tutorial Author
 
SimpleSDK's Avatar
 
Join Date: Jul 2009
Location: Orange County, CA
Posts: 25
SimpleSDK is on a distinguished road
Send a message via AIM to SimpleSDK
Default

Alright I updated the link. Go to the post again and you should be able to download them.
__________________
-Alec
SimpleSDKŠ
YouTube | Website | Applications
SimpleSDK is offline   Reply With Quote
Old 12-16-2009, 04:14 AM   #11 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 298
stanny is on a distinguished road
Default

I would like to follow this but where's the video tutorial in step 2?
stanny is offline   Reply With Quote
Old 03-30-2010, 08:29 PM   #12 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 59
lxLionHartxl is on a distinguished road
Default

replying to subscribe to this thread...
lxLionHartxl is offline   Reply With Quote
Old 04-28-2010, 07:33 AM   #13 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 1
szpak is on a distinguished road
Default

thank you!
szpak is offline   Reply With Quote
Old 06-13-2010, 11:32 PM   #14 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 5
waffles123 is on a distinguished road
Default

I'm a little confused with step number 11. What is this "pushStart" action? Thanks.
waffles123 is offline   Reply With Quote
Old 07-18-2010, 08:44 PM   #15 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 2
teryaki is on a distinguished road
Default NEATO!

The server side PHP files alone in this tutorial are so worth it. With slight modification I have gotten my app to post pics to my server and am working on passing form information back and forth. YOU ROCK !!
teryaki is offline   Reply With Quote
Old 10-05-2010, 02:47 PM   #16 (permalink)
Yko
Registered Member
 
Join Date: Sep 2010
Posts: 26
Yko is on a distinguished road
Default

I can't seem to get this to work. For some reason the POST stuff does not seem to be doing anything.

I set a button trigger action which does the POST and display the index.php and no new score is updated. It seems like the POST code fails to write to the posts.txt file in submit.php. Is there any way I can debug it to know where it might fail?

Thanks
Yko is offline   Reply With Quote
Old 10-06-2010, 12:53 AM   #17 (permalink)
Registered Member
 
Join Date: Oct 2010
Location: San Jose, CA, USA
Age: 30
Posts: 15
MyFirstMobileApp is on a distinguished road
Default

Nice tutorial, it will be really help me.
Thanks for sharing .
MyFirstMobileApp is offline   Reply With Quote
Old 10-06-2010, 08:29 AM   #18 (permalink)
Yko
Registered Member
 
Join Date: Sep 2010
Posts: 26
Yko is on a distinguished road
Default

Quote:
Originally Posted by Yko View Post
I can't seem to get this to work. For some reason the POST stuff does not seem to be doing anything.

I set a button trigger action which does the POST and display the index.php and no new score is updated. It seems like the POST code fails to write to the posts.txt file in submit.php. Is there any way I can debug it to know where it might fail?

Thanks
I just wanted to share what I found out. The reason why it didn't work for me is because of the permission for posts.txt. If anyone have this problem, do a sudo chmod 777 posts.txt inside that directory and it should work.

Great tutorial although I would prefer a web service tutorial where only data is passed along instead of accessing a website via a browser inside the app.

Oky
Yko is offline   Reply With Quote
Old 06-19-2011, 05:46 PM   #19 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 2
jimothy is on a distinguished road
Default

Also, you can use this tutorial I found: Add High Scores to Your iOS Game
jimothy is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 463
16 members and 447 guests
alexeir, David-T, Dj_kades, Elad, foslock, HemiMG, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, Pauluz85, pipposanta, QuantumDoja, robsmy, sacha1996, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:21 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0