2012-09-08 53 views
0

我需要在我的應用程序中使用自動重新連接功能,但它不起作用,因爲當連接再次可用時,iPhone本身不會重新連接。您應該關閉/打開iPhone或切換應用程序以觸發其連接。即使在Safari中,如果連接丟失,它也不起作用。如何在不切換應用程序或關閉/關閉iPhone的情況下從我的應用程序執行此操作?如何恢復iPhone上的連接?

+0

什麼樣的連接?你使用哪些課程?顯示一些代碼... – Adam

+0

Wifi連接。但是關於iPhone本身並不重新連接。這些步驟: - 啓動應用程序 - 關閉路由器(iPhone連接丟失) - 路由器上 - > iPhone不嘗試恢復連接,如果您的應用程序仍然活動 – iMx

+0

現在我明白了,只需使用Safari進行測試它不會重新連接。查看Reachability API,我已經聽到了好消息。 – Adam

回答

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]