Oh man, writing network code is the pits haha. This is pretty much my first time and it can be overwhelming at times trying to make sure you’ve covered all the exceptions and errors. We have a level editor built into our game and you can upload the levels you create. In order to keep some kind of quality control, we thought it would be best if everyone had to have a username to upload. Extending this idea, we thought it would be good to tie the username into the greenpixel.ca forums. It all works fairly smoothly, but the downside is I had to add logins and registration to the app. This spawns a number of cases like:
Try to upload -> is there a saved username?
Yes -> upload and handle errors
No -> go to login screen. Do you already have a username pass?
Yes -> Upload and handle errors save credentials
No -> Register new user. Is the username or email unique? etc etc.
So much sending and receiving and popping up error messages. :D
Looks like it all works now, but I will have to test it all strenuously later and try to simulate the phone losing its network connection.
The networking portion is fairly straightforward, in order to talk to a server, you just need to create an NSURLConnection object. I chose to make a global object that keeps getting initted as I wanted the ability to cancel it.
NSURLConnection *connection = [NSURLConnection alloc];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.website.com/php/dothings.php?id=%d", id]]];
connection = [connection initWithRequest:request delegate:self];
Which sends a GET request. You can attach simple variables to the URL itself, but for more complicated variables, like strings that contain spaces or &, you’ll need to use the function “stringByAddingPercentEscapesUsingEncoding” in the NSString class. Maybe it’s best to use it anyway!
There are 4 delegate methods you have to implement if you want to get anything back from the server. They are:
(the exact method names are found in the NSURLConnectionDelegate class)
You’ll need a global NSData object to collect the data as it is returned from the server. This should be reset each time:
NSData *serverData = [[NSMutableData alloc] initWithCapacity:0];
The data is received in chunks if the total size is more than the iPhone can handle. Our map data has this problem, so just keep appending to the serverData object:
This will keep getting called until it is done. When everything is finished, either …didFinishLoading or …didFailWithError will get called. Then it is time to decide what to do with the data.
Something to keep in mind! The minimum timeout for an iOS app is 240 seconds regardless of what you set it to. 240 seconds is a ridiculously long time to realise the server isn’t going to talk to you. Instead of setting a timeout, I just run a counter in the gameloop. If the counter exceeds a timeout limit, I pull the plug and run the connection fail code.
Sorry if this is hard to read or unhelpful, my brain is fairly fried right now! Time for some Mega Man 2 and then sleep.