Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS에서 네트워크 연결 상태 감지 #1

Open
gbmksquare opened this issue Mar 18, 2019 · 0 comments
Open

iOS에서 네트워크 연결 상태 감지 #1

gbmksquare opened this issue Mar 18, 2019 · 0 comments
Assignees
Labels
Framework about cocoa framework

Comments

@gbmksquare
Copy link
Contributor

gbmksquare commented Mar 18, 2019

iOS에서 네트워크 연결 상태 감지

우리는 Apple이 제공하는 Reachability 샘플 코드나 그 변종 버전을 사용해 iOS에서 네트워크 연결성을 확인하는 것이 익숙합니다. iOS 12부터는 Network.framework에 포함되어 있는 NWPathMonitor 클래스를 사용해 네트워크 연결을 확인할 수 있습니다.

이 프레임워크는 애플의 공식 API이기에 서드파티 코드를 더 이상 추가할 필요가 없고, 캡티브 포탈 문제도 일부 해결합니다. 단 iOS 12 이상에서만 사용 가능하고 캡티브 포탈을 완전하게 해결하지 않기 때문에 사용에 주의가 필요합니다.

키워드

Network.framework
NWPathMonitor
NWPath
NWInterface.InterfaceType
pathUpdateHandler
start(queue: )

사용

NWPathMonitor는 특정 네트워크 인터페이스나 모든 네트워크 인터페이스의 변화를 감지하고 pathUpdateHanlder을 통해 변화를 알려줍니다.

이 클래스 사용시 강한 참조를 남겨야합니다.

NWPathMonitor 생성시 파라미터를 주지않으면 모든 네트워크 인터페이스의 변화를 확인하고, 특정 인터페이스 타입을 파라미터로 지정하면 해당 네트워크 인터페이스의 변화만 알려줍니다. 이때 다른 네트워크 인터페이스의 변화는 pathUpdateHandler를 호출하지 않습니다.

let networkMonitor = NWPathMonitor()
let wifiMonitor = NWPathMonitor(requiredInterfaceType: .wifi)

NWInterface.InterfaceType의 종류에는 wifi, cellular, wiredEthernet,loopback, other가 있습니다.

pathUpdateHandlerNWPath를 반환합니다.

monitor.pathUpdateHandler = { path in
	if path.status == .satisfied {
		print("Connected"
	}
}

NWPathMonitor가 변화를 감지하려면 CLLocationManager처럼 시작 메서드를 호출해야합니다.

let queue = DispatchQueue.glboal(gos: .background)
monitor.start(queue: queue)

사용 완료 후 cancel()을 호출해줘야합니다.

Captive Portal Problem

NWPathMonitor

NWPathMonitor는 네트워크 변화를 감지하는 클래스입니다.

currentPath 프로퍼티는 start(queue: )가 호출되기 전에 nil을 반환합니다.

자세한 내용은 애플 문서를 확인하시길 바랍니다.
https://developer.apple.com/documentation/network/nwpathmonitor

NWPath

NWPath는 네트워크 연결에 대한 정보를 담고 있는 구조체입니다. iOS 12 이상부터 사용할 수 있습니다.

현재 네트워크 연결 상태를 확인하는 status 외에도, 해당 네트워크 연결이 DNS, IPv4, IPv6를 지원하는지 확인하는 supportsDNS, supportsIPv4, supportsIPv6 등 프로퍼티가 있습니다. 특히, isExpensive 프로퍼티를 통해 현재 네트워크 연결이 셀룰러처럼 “비싼” 비용을 필요로 하는지 확인할 수 있습니다.

자세한 내용은 애플 문서를 확인하시길 바랍니다.
https://developer.apple.com/documentation/network/nwpath

라이브러리

Connectivity (MIT)
iOS 8+ 에서는 Reachability를 사용하고 iOS 12 이상에서는 NWPathMonitor을 사용하는 네트워크 연결 확인 라이브러리.
https://github.com/rwbutler/Connectivity

Related Articles 🔖

N/A

Links 🔗

Reference

https://medium.com/@rwbutler/nwpathmonitor-the-new-reachability-de101a5a8835
https://medium.com/@rwbutler/solving-the-captive-portal-problem-on-ios-9a53ba2b381e

Related

https://developer.apple.com/videos/play/wwdc2018/715/
https://developer.apple.com/library/archive/samplecode/Reachability/Introduction/Intro.html

Library

https://github.com/rwbutler/Connectivity

Documentation

https://developer.apple.com/documentation/network/nwpathmonitor
https://developer.apple.com/documentation/network/nwpath

@clsoft clsoft added the Framework about cocoa framework label Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Framework about cocoa framework
Projects
None yet
Development

No branches or pull requests

2 participants