Jaebi의 Binary는 호남선

Swift Local Network Connection 본문

Swift

Swift Local Network Connection

jaebijae 2024. 6. 19. 12:39

Preliminary

  • iOS14 SDK 이상 - Info.plistNSLocalNetworkUsageDescription 설정

NWConnection

  • local endpoint와 remote endpoint를 연결하여 데이터 전송하는 객체
  • initializer → NWEndpoint를 생성하고 NWParameters 지정
    • NWEndpoint → network 연결의 local 또는 remote endpoint
      • hostPort, service(Bonjour Service), url, unix 등 case로 서비스 Enpoint 지정
    • NWParameters → network 경로 제약, 데이터 전송 옵션, 연결을 위해 사용되는 프로토콜 종류
      • tls, tcp, dtls, udp
  • Connection 객체가 생성되면 stateUpdateHandler로 connection 상태 확인
  • connection 상태 handling 후 start로 연결
    • Network 통신은 비동기로 이뤄져야 함으로 queue를 명시해줘야함

NSPathMonitor

  • Network 변경을 monitoring하는 observer
  • pathUpdateHandler를 통해 path interfaceType이 wifi / cellular / wiredEthernet / loopback 등 인지 알 수 있음

연결 후 send와 Receive

  • connection state가 ready 되었을때
    • NWConnection.send(content: completion:) - connection에 데이터 send
    • NWConnection.receiveMessage(completion:) - 데이터 받을때 처리

Reference

 

NWConnection | Apple Developer Documentation

A bidirectional data connection between a local endpoint and a remote endpoint.

developer.apple.com

 

[Network] NWConnection

- 목적: 특정 프로그램의 UDP 포트 번호와 IP 주소 값을 통해, Data를 주고받을 수 있는 NWConnection을 이해한다. 우선, NWConnection이 뭐하는 객체인지 이해하자 NWConnection A bidirectional data connection between a

aviciiiphone.tistory.com

 

Network FrameWork를 이용한 HTTP 통신

HTTP 요청 실습을 하기 위해서 Swift Framework 중 하나인 Network를 사용해야 했는데 이에 대한 한글뿐 아니라 정리 문서가 너무 빈약해서 애 먹었다… 꽤나 최신에 나온 프레임워크인거 같기도 하고

ghis22130.github.io

 

 

'Swift' 카테고리의 다른 글

Swift - Memory 기초  (0) 2024.07.20
Swift - 문법 (접근 제어)  (0) 2024.07.20
Swift Wi-Fi Configuration  (0) 2024.06.19
WWDC 2024 - WeatherKit Sample App  (0) 2024.06.13
WeatherKit 사용 (WWDC 2024)  (1) 2024.06.13