Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS11以降の Network Reachability

iOS11以降の Network Reachability

よく使われているReachabilityに関する誤解やネットワークの接続状況を確認するためにiOS11以降で登場したフレームワークやAppleのドキュメントに記載のある接続が切断された時の対処法などについての資料です。

[email protected]

July 30, 2019
Tweet

Other Decks in Programming

Transcript

  1. 3FBDIBCJMJUZTXJGU https://github.com/ashleymills/Reachability.swift //declare this property where it won't go out

    of scope relative to your listener let reachability = Reachability()! reachability.whenReachable = { reachability in if reachability.connection == .wifi { print("Reachable via WiFi") } else { print("Reachable via Cellular") } } reachability.whenUnreachable = { _ in print("Not reachable") } do { try reachability.startNotifier() } catch { print("Unable to start notifier") }
  2. 3FBDIBCJMJUZͷޡղ let reachability = Reachability()! if reachability.connection == .wifi {

    print("ϦϞʔτ௨৴ͯ͠ը૾Λऔಘ") } else { print(“ϩʔΧϧΩϟογϡΛ࢖͏") }
  3. 3FBDIBCJMJUZͷޡղ “Always attempt to make a connection. Do not attempt

    to guess whether network service is available, and do not cache that determination.” https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/WhyNetworkingIsHard/WhyNetworkingIsHard.html#//apple_ref/doc/ uid/TP40010220-CH13-SW3 ઀ଓΛࢼΈΔલʹνΣοΫΛͯ͠͸͍͚ͳ͍
  4. 3FBDIBCJMJUZͷਖ਼͍͠࢖͍ํ Important: The SCNetworkReachability API is not intended for use

    as a preflight mechanism for determining network connectivity. You determine network connectivity by attempting to connect. If the connection fails consult the SCNetworkReachability API to help diagnose the cause of the failure. https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/WhyNetworkingIsHard/WhyNetworkingIsHard.html#//apple_ref/doc/ uid/TP40010220-CH13-SW3 ·ͣ͸઀ଓΛࢼͯ͠Έͯ ࣦഊͨ͠ͱ͖ͷݪҼΛಛఆ͢ΔͨΊʹ࢖͏
  5. ઃఆํ๏ λΠϛϯά͸63-4FTTJPOͷΠϯελϯεੜ੒લ let configuration = URLSessionConfiguration.default configuration.waitsForConnectivity = true let

    session = URLSession(configuration: configuration) λΠϜΞ΢τΛઃఆ͢Δ σϑΥϧτ͸̍िؒ configuration.timeoutIntervalForResource = 600 // 10෼
  6. ઃఆํ๏ EFMFHBUFͰ࠶઀ଓ࣌ͷΠϕϯτΛड͚औΔ let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue:

    nil) 63-4FTTJPO͸EFMFHBUFΛڧࢀর͢Δ “The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you don’t invalidate the session, your app leaks memory until it exits.” session.invalidateAndCancel()