如何在iPhone上恢复连接?

时间:2012-09-08 11:42:07

标签: iphone objective-c ios

我的应用程序中需要一个自动重新连接功能,但它不起作用,因为当连接再次可用时,iPhone本身不会重新连接。您应该关闭/打开iPhone或切换应用程序以触发它连接。如果连接丢失,即使在Safari中也不起作用。如何在不切换应用程序或关闭iPhone的情况下从我的应用程序执行此操作?

1 个答案:

答案 0 :(得分:0)

使用可达性api。 http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

您可以找到相同的ARC版本HERE

当网络状态发生变化时,可达性api会在默认中心发布通知。因此,请将自己添加为此通知的观察者,并在网络恢复时执行您想要的任何操作..!

上面给出的同一个github页面中的一个简单例子,

    // allocate a reachability object
    Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

    // tell the reachability that we DONT want to be reachable on 3G/EDGE/CDMA
    reach.reachableOnWWAN = NO;

    // here we set up a NSNotification observer. The Reachability that caused the notification
    // is passed in the object parameter
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(reachabilityChanged:) 
                                                 name:kReachabilityChangedNotification 
                                               object:nil];

[reach startNotifier]
相关问题