iOS11以降の Network Reachability

Cc8ec9594b83b5dc3c7eef345c05dc8d?s=47 shiz
July 30, 2019

iOS11以降の Network Reachability




July 30, 2019


  1. 6.

    3FBDIBCJMJUZTXJGU //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. 7.

    3FBDIBCJMJUZͷޡղ let reachability = Reachability()! if reachability.connection == .wifi {

    print("ϦϞʔτ௨৴ͯ͠ը૾Λऔಘ") } else { print(“ϩʔΧϧΩϟογϡΛ࢖͏") }
  3. 8.

    3FBDIBCJMJUZͷޡղ “Always attempt to make a connection. Do not attempt

    to guess whether network service is available, and do not cache that determination.” uid/TP40010220-CH13-SW3 ઀ଓΛࢼΈΔલʹνΣοΫΛͯ͠͸͍͚ͳ͍
  4. 9.

    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. uid/TP40010220-CH13-SW3 ·ͣ͸઀ଓΛࢼͯ͠Έͯ ࣦഊͨ͠ͱ͖ͷݪҼΛಛఆ͢ΔͨΊʹ࢖͏
  5. 12.

    ઃఆํ๏ λΠϛϯά͸63-4FTTJPOͷΠϯελϯεੜ੒લ let configuration = URLSessionConfiguration.default configuration.waitsForConnectivity = true let

    session = URLSession(configuration: configuration) λΠϜΞ΢τΛઃఆ͢Δ σϑΥϧτ͸̍िؒ configuration.timeoutIntervalForResource = 600 // 10෼
  6. 13.

    ઃఆํ๏ 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()