是的,您可以使用 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