LoginSignup
21
13

More than 3 years have passed since last update.

Swift 4 ネットワークの接続チェック

Last updated at Posted at 2018-08-29

はじめに

iOSアプリ上で、端末のネットワークの接続状況をチェックする要件が出てきた為、
ライブラリがあるか調べてみた。
ReachabilitySwiftというライブラリがあったので、使用方法記載する。

導入

CocoaPodsからインストール

Podfile
pod 'ReachabilitySwift'

実装

Uitls.swift
import Reachability

final class Network {
    
    static func isOnline() -> Bool {
        guard let reachability = Reachability() else { return false }
        return reachability.connection != .none
    }
    
}

呼び出し元

Test.swift
if Network.isOnline {
    // オンライン
} else {
    // オフライン
}
21
13
0

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
21
13