iOS11以降の Network Reachability

Cc8ec9594b83b5dc3c7eef345c05dc8d?s=47 shiz
July 30, 2019

iOS11以降の Network Reachability

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

Cc8ec9594b83b5dc3c7eef345c05dc8d?s=128

shiz

July 30, 2019
Tweet

Transcript

  1. J04Ҏ߱ͷ /FUXPSL3FBDIBCJMJUZ  0UFNBDIJTXJGU

  2. 7*4*545FDIOPMPHJFTגࣜձࣾॴଐ ʢΦϑΟε͸ؙͷ಺ʣ TIJ[!TU[O TIJ[ ͣ͠ !TIJ[ TU[O TIJ[ ։ൃܦݧݴޠ 4XJGU,PUMJO+BWBTDSJQU1)1$+BWB(Pʜ

  3. ΞϓϦར༻؀ڥ͕֦େ wϚϧνϓϥοτϑΥʔϜ wެڞແྉ8Jpͷීٴ΍̑(ͷొ৔ ωοτϫʔΫͷ઀ଓঢ়ଶͷมԽ΋େ͖͘

  4. 4$/FUXPSL3FBDIBCJMJUZ w4ZTUFN$POpHVSBUJPOGSBNFXPSLͷΠϯ λʔϑΣΠε wݱࡏͷ/FUXPSLͷઃఆ΍ର৅ϗετ 8Jpͳ Ͳ ΁઀ଓͰ͖Δ͔Ͳ͏͔Λ֬ೝ͢Δ͜ͱ͕ Ͱ͖Δ https://developer.apple.com/documentation/systemconfiguration/scnetworkreachability-g7d

  5. 3FBDIBCJMJUZTXJGU w"QQMF͕ఏڙ͍ͯ͠ΔαϯϓϧίʔυΛݩʹ ͨ͠ωοτϫʔΫ઀ଓঢ়گΛ֬ೝ͢ΔϥΠ ϒϥϦ wΫϩʔδϟ΍/PUJpDBUJPOͰݱࡏͷ઀ଓঢ়گ Λ֬ೝͰ͖Δ https://github.com/ashleymills/Reachability.swift

  6. 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") }
  7. 3FBDIBCJMJUZͷޡղ let reachability = Reachability()! if reachability.connection == .wifi {

    print("ϦϞʔτ௨৴ͯ͠ը૾Λऔಘ") } else { print(“ϩʔΧϧΩϟογϡΛ࢖͏") }
  8. 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 ઀ଓΛࢼΈΔલʹνΣοΫΛͯ͠͸͍͚ͳ͍
  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. https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/WhyNetworkingIsHard/WhyNetworkingIsHard.html#//apple_ref/doc/ uid/TP40010220-CH13-SW3 ·ͣ͸઀ଓΛࢼͯ͠Έͯ ࣦഊͨ͠ͱ͖ͷݪҼΛಛఆ͢ΔͨΊʹ࢖͏
  10. ͨͩɻɻɻ ࠁʑͱมԽ͢Δ ωοτϫʔΫͷ઀ଓঢ়گΛߟྀͯ͠ ਖ਼࣮͘͠૷͢Δͷ͸೉͍͠

  11. XBJUT'PS$POOFDUJWJUZ https://developer.apple.com/documentation/foundation/urlsessionconfiguration/2908812-waitsforconnectivity wJ04Ͱొ৔ w઀ଓʹࣦഊͨ͠৔߹ɺ࠶઀ଓ͞Εͨ࣌ʹࣗ ಈͰϦΫΤετΛ࠶ૹͯ͘͠ΕΔ wඞཁͳ઀ଓঢ়گΛߟྀͯ͘͠ΕΔ wόοςϦʔͷফඅʹ΋ྑ͍

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

    session = URLSession(configuration: configuration) λΠϜΞ΢τΛઃఆ͢Δ σϑΥϧτ͸̍िؒ configuration.timeoutIntervalForResource = 600 // 10෼
  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()
  14. ͨͩɻɻɻ ༗ޮͳͷ͸ϦΫΤετ࣌ʹ઀ଓ͕ͳ͘࠶઀ଓͨ࣌͠ ⽐ ϦΫΤετ࣌ʹ͸੒ޭͨ͠΋ͷͷ ్தͰ઀ଓ͕੾Εͯ͠·ͬͨ৔߹ʹ͸ ࠶ૹͯ͘͠Εͳ͍ NSURLErrorNetworkConnectionLost 1005)

  15. ରॲ๏ 63-4FTTJPOͷڍಈΛཧղ͢Δ https://developer.apple.com/library/archive/qa/qa1941/_index.html#//apple_ref/doc/uid/DTS40017602 Ϩεϙϯε͕มΘΔϦΫΤετ 1045ͳͲ ͷ৔߹ ঢ়ଶ͕ෆ໌ͳͷͰ࠶ૹ͠ͳ͍ Ͱ͖ͳ͍ Ϩεϙϯε͕͍ͭ΋ಉ͡ϦΫΤετ (&5ͳͲ

    ͷ৔߹ 63-4FTTJPO͕ࣗಈͰ࠶ૹͯ͘͠ΕΔ͜ͱ͕͋Δ
  16. ରॲ๏ ࣮૷ͷํ਑ https://developer.apple.com/library/archive/qa/qa1941/_index.html#//apple_ref/doc/uid/DTS40017602 Ϩεϙϯε͕มΘΔϦΫΤετ 1045ͳͲ ͷ৔߹ ݸʑͷϦΫΤετΛ*%ͳͲͰಛఆͯ͠ɺݸʑʹରԠ͢Δ Ϩεϙϯε͕͍ͭ΋ಉ͡ϦΫΤετ (&5ͳͲ ͷ৔߹

    ୯७ͳϦτϥΠॲཧ͕Մೳɻڞ௨ͷ࣮૷Ͱରॲɻ ˞લఏͱͯ͠ਖ਼͍͠)551ϝιουΛ࢖༻͢Δ ɹؒҧ͍ͬͯΔ৔߹͸αʔόʔଆͰରॲ͢Δํ͕ద੾
  17. ଞͷ໰୊ ϞόΠϧσʔλ௨৴ͷ࢖༻Մ൱͸ϑϥάͰ൑ఆͰ͖ͳ͍ BMMPXT$FMMVMBS"DDFTTϓϩύςΟ $BQUJWF1PSUBM໰୊ ެڞͷແྉ8Jp઀ଓ࣌ͷϩάΠϯϖʔδ  /81BUI.POJUPS J04Ҏ߱ IUUQTRJJUBDPNTIJ[JUFNTBFDGBGBE ʲ4XJGU

    J04ʳJ04Ҏ߱ͷ/FUXPSL3FBDIBCJMJUZ
  18. ͋Γ͕ͱ͏͍͟͝·ͨ͠