0

I am currently in a team developing an app for the platform iOS. All the scores in this game are sent to a mysql base and displayed on a webpage. Sending this data to the mysql base requires Internet of course.

My question is, are there anyone where who know how to save this score locally on the phone or tablet, and make it send the score automatically to the mysql base when the device gets an internet signal? Code used to send data from the device to the database:

-(void) startNewGame:(ccTime) delta {

    NSString *strURL = [NSString stringWithFormat:@"http://erlpil.com/stumpp/settscore.php?poeng=%i", score];
Erlpil
  • 85
  • 1
  • 7
  • And of course you are connecting to the database using the unencrypted MySQL network protocol directly aren't you? – trojanfoe Feb 19 '13 at 11:41
  • I am really new to objective C but the code that i used to make it work looks like this: -(void) startNewGame:(ccTime) delta { NSString *strURL = [NSString stringWithFormat:@"http://erlpil.com/stumpp/settscore.php?poeng=%i", score]; – Erlpil Feb 19 '13 at 11:47
  • OK good; you aren't connecting to the database directly; you are doing it the way it should be done. But to answer your question there is no easy way to do this outside of your app; you will need to check upon start-up if there are any scores to sync, and perhaps when the app terminates. – trojanfoe Feb 19 '13 at 11:54
  • What is your question now? Apparently the URL is right. I'll get "Success" as response. – Hermann Klecker Feb 19 '13 at 11:54
  • The question is; how can I get scores saved on devices that don’t have any internet connection. Is there any way I can save the score on the device until it gets internet connection and send it to my database then? – Erlpil Feb 19 '13 at 11:58
  • Yes; you can use a local sqlite database, or use coredata, or a plist or a custom text or binary file. You have a vast array of choices. – trojanfoe Feb 19 '13 at 12:00
  • Aha! As I said im very new to Objective C. Do you have a link to a tutorial where one of these processes is described, so I could follow along that one? – Erlpil Feb 19 '13 at 12:03
  • Here's one for the plist option (I searched "ios store data in file"): http://stackoverflow.com/questions/4075056/what-are-the-options-for-saving-data-in-ios – trojanfoe Feb 19 '13 at 12:09

1 Answers1

0

Yes, you can use a class provided by Apple called Reachability to verify the internet connection status, and NSNotificationCenter to keep waiting for any internet connection status. Thus, you could send the scores to your mysql database using the methods described above by @trojanfoe.

To verify internet status first check if any host is reachable, if it is, we understand that there is an internet connection:

- (BOOL)verifyInternetStatus
{
    Reachability *reachable = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    if (internetStatus == NotReachable) {
        // there's not internet connection, therefore we save the scores locally
    }
    else {
        // send the scores to the database
    }
}

When the internet status changes, Reachability class notifies to its observers through NSNotificationCenter that internet status has changed using the key kReachabilityChangedNotification. So, you need to add your class as an observer of this key:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(verifyInternetStatus:) 
                                             name:kReachabilityChangedNotification
                                           object:nil];

This is one way to doing so. When you want to send the score, call the method verifyInternetStatus it will send the score or will save it locally.

I hope it helps.

EDIT:

Sorry, i've forgot the class:

https://developer.apple.com/iphone/library/samplecode/Reachability/index.html

thxou
  • 691
  • 7
  • 20
  • Awesome, this really helped! Thanks a lot! I am afraid I still don’t know how to save the score locally on the device. Do you know what code I should paste in under if the if (internetStatus == NotReachable) ? – Erlpil Feb 19 '13 at 13:30
  • Yes, if the host is not reachable (`internetStatus == NotReachable`) then you have to save the scores locally, one way to do this is by following the link that @trojanfoe has posted. If the host isn't reachable (`internetStatus != NotReachable`), you should use your own method, like your `startNewGame:` method. – thxou Feb 19 '13 at 15:10