1

更新された到達可能性ライブラリを使用して、インターネット接続が到達可能かどうかをテストしています。インターネットに接続できない場合に備えてメッセージをログに記録しようとしましたが、ログがデバッグされません。

//Test the internet connection
Reachability* reach = [Reachability reachabilityForInternetConnection];
reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"Internet connexion unreachable");//Although Internet cnx is off, this message is not displayed
    return;
};

// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];

到達可能性ライブラリを誤解していますか? インターネットがオフのときに特定のタスクを実行する方法は? ありがとう。

PS: 私の iPad は Wi-Fi のみで、3G サービスはありません。

4

3 に答える 3

2

Swift 4でAlamofireを使用してinternerをチェックする

import Foundation
import Alamofire

class Connectivity {

   class func isConnectedToInternet() ->Bool {
        return NetworkReachabilityManager()!.isReachable
   }
}

次に、この関数を呼び出します

if Connectivity.isConnectedToInternet() {
    print("Yes! internet is available.")
    // do some tasks..
}
于 2018-03-11T18:41:33.547 に答える
0

さて、私が得た最善の方法は、Appleのサンプルコードに従うことです。

   //Test the internet connection
   Reachability* reach = [Reachability reachabilityForInternetConnection];

   NetworkStatus netStatus = [reach currentReachabilityStatus];
   //See if the reachable object status is "ReachableViaWifi"
if (netStatus!=ReachableViaWiFi) {
    //If not
    NSLog(@"wifi unavailable");
    //Alert the user about the Internet cnx
    WBErrorNoticeView *notice = [WBErrorNoticeView errorNoticeInView:self.view title:@"Network Error" message:@"Check your internet connection."];
    notice.sticky = NO;
    [notice show];
    return;//Exit the method
}
于 2013-02-07T20:34:12.770 に答える
0

実際には、変更された到達可能性を受け取る通知に登録する必要があります。

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

ここで Apple の例を見てください:

于 2013-02-07T18:10:25.790 に答える