0

我目前在一个团队中为 iOS 平台开发应用程序。这场比赛的所有分数都被发送到一个mysql库并显示在一个网页上。将这些数据发送到 mysql 库当然需要 Internet。

我的问题是,有没有人知道如何在手机或平板电脑上本地保存这个分数,并让它在设备收到互联网信号时自动将分数发送到 mysql 基地?用于将数据从设备发送到数据库的代码:

-(void) startNewGame:(ccTime) delta {

    NSString *strURL = [NSString stringWithFormat:@"http://erlpil.com/stumpp/settscore.php?poeng=%i", score];
4

1 回答 1

0

是的,您可以使用 Apple 提供的名为 Reachability 的类来验证 Internet 连接状态,并使用 NSNotificationCenter 继续等待任何 Internet 连接状态。因此,您可以使用上述@trojanfoe 描述的方法将分数发送到您的 mysql 数据库。

要验证 Internet 状态,首先检查是否有任何主机可访问,如果是,我们知道有 Internet 连接:

- (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
    }
}

当 Internet 状态发生变化时,Reachability 类通过 NSNotificationCenter 通知其观察者 Internet 状态已使用 key 更改kReachabilityChangedNotification。因此,您需要将您的类添加为该键的观察者:

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

这是这样做的一种方法。当您要发送分数时,调用verifyInternetStatus它将发送分数或将其保存在本地的方法。

我希望它有所帮助。

编辑:

对不起,我忘记了课程:

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

于 2013-02-19T12:55:41.733 回答