LoginSignup
24
26

More than 5 years have passed since last update.

iOSアプリでネットワークステータスを取得する

Last updated at Posted at 2016-10-20

はじめに

ネットワークの接続状況を確認するライブラリ「Reachability」を使用してみたので備忘録として書き記します。
現在のネットワークの接続状況が3G、WiFi、もしくは圏外なのかを確認する際に便利なのが「Reachability」というライブラリです。

「Reachability」の導入

CocoaPodsを導入している場合は下記の手順を行う。
※CocoaPodsの導入手順についてはこちらの記事に非常に丁寧に書かれています。
http://qiita.com/ShinokiRyosei/items/3090290cb72434852460

cd プロジェクトディレクトリ //プロジェクトディレクトリに移動
pod init //Podfileを作成
vim podfile //Podfileを開く

Podfileの中身を下記の様に編集。
target 'ReachabilityDemo(アプリ名)' doの一行下に
pod 'Reachability'と追記し保存します。

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

target 'ReachabilityDemo' do
  pod 'Reachability'
  # Uncomment this line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for ReachabilityDemo

end
pod install //Reachabilityのインストール

※Xcodeをもし開いている状態なら一度再起動後プロジェクトを再度開いてください。

CocoaPodsを使用しない方法
以下のページからサンプルコードをダウンロードしプロジェクトにReachability.h/mを追加してください。

SystemConfiguration.frameworkの追加

1,プロジェクト名押下
2,Linked Frameworks and Librariesの+を押下
3,systemcと検索バーに入力
4,SystemConfiguration.frameworkを選択しAddボタンを押下

スクリーンショット 2016-10-20 17.57.06(2).png

実装

ViewController.m
#import "ViewController.h"
#import "Reachability.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [reachability currentReachabilityStatus];

    switch (netStatus) {
        case NotReachable:  //圏外
            NSLog(@"圏外");
            break;
        case ReachableViaWWAN:  //3G
            NSLog(@"3G");
            break;
        case ReachableViaWiFi:  //WiFi
            NSLog(@"WiFi");
            break;
        default:  //上記以外
            NSLog(@"上記以外");
            break;
    }

    // 用途としては下記のようにif文でネットワークステータスを判定して使うことが多そう
    if (netStatus == NotReachable) {
        // ここでUIAlertControllerで「ネットワークに接続していません」等のアラートを出すなど
    } else {
        NSLog(@"正常");
        // ここに正常系の処理を書く
    }
}

@end

※コメントでご教示いただいたネットワークステータス変更を通知する方法も追記いたします。

ViewController.m
#import "ViewController.h"
#import "Reachability.h"

@interface ViewController ()
@property (weak, nonatomic) Reachability *reachability;
@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    self.reachability = [Reachability reachabilityForInternetConnection];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(notifiedNetworkStatus:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];
    [self.reachability startNotifier];
}

- (void)notifiedNetworkStatus:(NSNotification *)notification {
    NetworkStatus networkStatus = [self.reachability currentReachabilityStatus];
    switch (networkStatus) {
        case NotReachable:  //圏外
            NSLog(@"圏外");
            break;
        case ReachableViaWWAN:  //3G
            NSLog(@"3G");
            break;
        case ReachableViaWiFi:  //WiFi
            NSLog(@"WiFi");
            break;
        default:  //上記以外
            NSLog(@"上記以外");
            break;
    }
}


@end

さいごに

便利!

24
26
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
26