axiom-networking-migration by charleswiltgen/axiom
npx skills add https://github.com/charleswiltgen/axiom --skill axiom-networking-migration您正在使用哪种网络 API?
├─ 用于 HTTP/HTTPS REST API 的 URLSession?
│ └─ 继续使用 URLSession — 这是处理 HTTP 的**正确**工具
│ URLSession 处理缓存、Cookie、身份验证挑战、
│ HTTP/2/3,并且针对 Web API 进行了深度优化。
│ Network.framework 用于自定义协议,**而非** HTTP。
│
├─ BSD 套接字(socket、connect、send、recv)?
│ └─ 迁移到 NWConnection(iOS 12+)
│ → 参见下面的迁移 1
│
├─ NWConnection / NWListener?
│ ├─ 需要 async/await? → 迁移到 NetworkConnection(iOS 26+)
│ │ → 参见下面的迁移 2
│ └─ 基于回调的代码运行良好? → 保持(未弃用)
│
├─ 用于 TCP/TLS 的 URLSession StreamTask?
│ └─ 需要 UDP 或自定义协议? → NetworkConnection
│ 仅需用于 HTTP 的 TCP/TLS? → 继续使用 URLSession
│ → 参见下面的迁移 3
│
├─ SCNetworkReachability?
│ └─ **已弃用** — 替换为 NWPathMonitor(iOS 12+)
│ let monitor = NWPathMonitor()
│ monitor.pathUpdateHandler = { path in
│ print(path.status == .satisfied ? "在线" : "离线")
│ }
│
└─ CFSocket / NSStream?
└─ **已弃用** — 替换为 NWConnection(iOS 12+)
→ 参见下面的迁移 1
| BSD 套接字 | NWConnection | 备注 |
|---|
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
socket() + connect() | NWConnection(host:port:using:) + start() | 默认非阻塞 |
send() / sendto() | connection.send(content:completion:) | 异步,立即返回 |
recv() / recvfrom() | connection.receive(minimumIncompleteLength:maximumLength:completion:) | 异步,立即返回 |
bind() + listen() | NWListener(using:on:) | 自动端口绑定 |
accept() | listener.newConnectionHandler | 每个连接的回调 |
getaddrinfo() | 让 NWConnection 处理 DNS | 智能解析与竞速 |
SCNetworkReachability | connection.stateUpdateHandler 等待状态 | 无竞态条件 |
setsockopt() | NWParameters 配置 | 类型安全的选项 |
// 迁移前 — 阻塞式,手动 DNS,易出错
var hints = addrinfo()
hints.ai_family = AF_INET
hints.ai_socktype = SOCK_STREAM
var results: UnsafeMutablePointer<addrinfo>?
getaddrinfo("example.com", "443", &hints, &results)
let sock = socket(results.pointee.ai_family, results.pointee.ai_socktype, 0)
connect(sock, results.pointee.ai_addr, results.pointee.ai_addrlen) // 阻塞
let data = "Hello".data(using: .utf8)!
data.withUnsafeBytes { ptr in
send(sock, ptr.baseAddress, data.count, 0)
}
// 迁移后 — 非阻塞,自动 DNS,类型安全
let connection = NWConnection(
host: NWEndpoint.Host("example.com"),
port: NWEndpoint.Port(integerLiteral: 443),
using: .tls
)
connection.stateUpdateHandler = { state in
if case .ready = state {
let data = Data("Hello".utf8)
connection.send(content: data, completion: .contentProcessed { error in
if let error = error {
print("发送失败: \(error)")
}
})
}
}
connection.start(queue: .main)
[weak self](async/await 处理取消)| NWConnection(iOS 12-25) | NetworkConnection(iOS 26+) | 备注 |
|---|---|---|
connection.stateUpdateHandler = { state in } | for await state in connection.states { } | 异步序列 |
connection.send(content:completion:) | try await connection.send(content) | 挂起函数 |
connection.receive(minimumIncompleteLength:maximumLength:completion:) | try await connection.receive(exactly:) | 挂起函数 |
| 手动 JSON 编码/解码 | Coder(MyType.self, using: .json) | 内置 Codable 支持 |
| 自定义帧处理器 | TLV { TLS() } | 内置类型-长度-值 |
到处使用 [weak self] | 无需 [weak self] | 任务取消自动处理 |
// 迁移前 — 完成处理程序,手动内存管理
let connection = NWConnection(host: "example.com", port: 443, using: .tls)
connection.stateUpdateHandler = { [weak self] state in
switch state {
case .ready:
self?.sendData()
case .waiting(let error):
print("等待中: \(error)")
case .failed(let error):
print("失败: \(error)")
default:
break
}
}
connection.start(queue: .main)
func sendData() {
let data = Data("Hello".utf8)
connection.send(content: data, completion: .contentProcessed { [weak self] error in
if let error = error {
print("发送错误: \(error)")
return
}
self?.receiveData()
})
}
func receiveData() {
connection.receive(minimumIncompleteLength: 10, maximumLength: 10) { [weak self] (data, context, isComplete, error) in
if let error = error {
print("接收错误: \(error)")
return
}
if let data = data {
print("已接收: \(data)")
}
}
}
// 迁移后 — Async/await,自动内存管理
let connection = NetworkConnection(
to: .hostPort(host: "example.com", port: 443)
) {
TLS()
}
// 在后台任务中监控状态
Task {
for await state in connection.states {
switch state {
case .preparing:
print("连接中...")
case .ready:
print("就绪")
case .waiting(let error):
print("等待中: \(error)")
case .failed(let error):
print("失败: \(error)")
default:
break
}
}
}
// 使用 async/await 发送和接收
func sendAndReceive() async throws {
let data = Data("Hello".utf8)
try await connection.send(data)
let received = try await connection.receive(exactly: 10).content
print("已接收: \(received)")
}
[weak self]// 迁移前 — 用于 TCP/TLS 流的 URLSession
let task = URLSession.shared.streamTask(withHostName: "example.com", port: 443)
task.resume()
task.write(Data("Hello".utf8), timeout: 10) { error in
if let error = error {
print("写入错误: \(error)")
}
}
task.readData(ofMinLength: 10, maxLength: 10, timeout: 10) { data, atEOF, error in
if let error = error {
print("读取错误: \(error)")
return
}
if let data = data {
print("已接收: \(data)")
}
}
// 迁移后 — 用于 TCP/TLS 的 NetworkConnection
let connection = NetworkConnection(
to: .hostPort(host: "example.com", port: 443)
) {
TLS()
}
func sendAndReceive() async throws {
try await connection.send(Data("Hello".utf8))
let data = try await connection.receive(exactly: 10).content
print("已接收: \(data)")
}
技能 : axiom-ios-networking, axiom-networking-legacy
每周安装量
89
代码仓库
GitHub 星标数
610
首次出现
2026年1月21日
安全审计
安装于
opencode73
claude-code69
codex68
gemini-cli66
cursor66
github-copilot63
What networking API are you using?
├─ URLSession for HTTP/HTTPS REST APIs?
│ └─ Stay with URLSession — it's the RIGHT tool for HTTP
│ URLSession handles caching, cookies, auth challenges,
│ HTTP/2/3, and is heavily optimized for web APIs.
│ Network.framework is for custom protocols, NOT HTTP.
│
├─ BSD Sockets (socket, connect, send, recv)?
│ └─ Migrate to NWConnection (iOS 12+)
│ → See Migration 1 below
│
├─ NWConnection / NWListener?
│ ├─ Need async/await? → Migrate to NetworkConnection (iOS 26+)
│ │ → See Migration 2 below
│ └─ Callback-based code working fine? → Stay (not deprecated)
│
├─ URLSession StreamTask for TCP/TLS?
│ └─ Need UDP or custom protocols? → NetworkConnection
│ Need just TCP/TLS for HTTP? → Stay with URLSession
│ → See Migration 3 below
│
├─ SCNetworkReachability?
│ └─ DEPRECATED — Replace with NWPathMonitor (iOS 12+)
│ let monitor = NWPathMonitor()
│ monitor.pathUpdateHandler = { path in
│ print(path.status == .satisfied ? "Online" : "Offline")
│ }
│
└─ CFSocket / NSStream?
└─ DEPRECATED — Replace with NWConnection (iOS 12+)
→ See Migration 1 below
| BSD Sockets | NWConnection | Notes |
|---|---|---|
socket() + connect() | NWConnection(host:port:using:) + start() | Non-blocking by default |
send() / sendto() | connection.send(content:completion:) | Async, returns immediately |
recv() / recvfrom() | connection.receive(minimumIncompleteLength:maximumLength:completion:) | Async, returns immediately |
// BEFORE — Blocking, manual DNS, error-prone
var hints = addrinfo()
hints.ai_family = AF_INET
hints.ai_socktype = SOCK_STREAM
var results: UnsafeMutablePointer<addrinfo>?
getaddrinfo("example.com", "443", &hints, &results)
let sock = socket(results.pointee.ai_family, results.pointee.ai_socktype, 0)
connect(sock, results.pointee.ai_addr, results.pointee.ai_addrlen) // BLOCKS
let data = "Hello".data(using: .utf8)!
data.withUnsafeBytes { ptr in
send(sock, ptr.baseAddress, data.count, 0)
}
// AFTER — Non-blocking, automatic DNS, type-safe
let connection = NWConnection(
host: NWEndpoint.Host("example.com"),
port: NWEndpoint.Port(integerLiteral: 443),
using: .tls
)
connection.stateUpdateHandler = { state in
if case .ready = state {
let data = Data("Hello".utf8)
connection.send(content: data, completion: .contentProcessed { error in
if let error = error {
print("Send failed: \(error)")
}
})
}
}
connection.start(queue: .main)
| NWConnection (iOS 12-25) | NetworkConnection (iOS 26+) | Notes |
|---|---|---|
connection.stateUpdateHandler = { state in } | for await state in connection.states { } | Async sequence |
connection.send(content:completion:) | try await connection.send(content) | Suspending function |
connection.receive(minimumIncompleteLength:maximumLength:completion:) | try await connection.receive(exactly:) |
// BEFORE — Completion handlers, manual memory management
let connection = NWConnection(host: "example.com", port: 443, using: .tls)
connection.stateUpdateHandler = { [weak self] state in
switch state {
case .ready:
self?.sendData()
case .waiting(let error):
print("Waiting: \(error)")
case .failed(let error):
print("Failed: \(error)")
default:
break
}
}
connection.start(queue: .main)
func sendData() {
let data = Data("Hello".utf8)
connection.send(content: data, completion: .contentProcessed { [weak self] error in
if let error = error {
print("Send error: \(error)")
return
}
self?.receiveData()
})
}
func receiveData() {
connection.receive(minimumIncompleteLength: 10, maximumLength: 10) { [weak self] (data, context, isComplete, error) in
if let error = error {
print("Receive error: \(error)")
return
}
if let data = data {
print("Received: \(data)")
}
}
}
// AFTER — Async/await, automatic memory management
let connection = NetworkConnection(
to: .hostPort(host: "example.com", port: 443)
) {
TLS()
}
// Monitor states in background task
Task {
for await state in connection.states {
switch state {
case .preparing:
print("Connecting...")
case .ready:
print("Ready")
case .waiting(let error):
print("Waiting: \(error)")
case .failed(let error):
print("Failed: \(error)")
default:
break
}
}
}
// Send and receive with async/await
func sendAndReceive() async throws {
let data = Data("Hello".utf8)
try await connection.send(data)
let received = try await connection.receive(exactly: 10).content
print("Received: \(received)")
}
// BEFORE — URLSession for TCP/TLS stream
let task = URLSession.shared.streamTask(withHostName: "example.com", port: 443)
task.resume()
task.write(Data("Hello".utf8), timeout: 10) { error in
if let error = error {
print("Write error: \(error)")
}
}
task.readData(ofMinLength: 10, maxLength: 10, timeout: 10) { data, atEOF, error in
if let error = error {
print("Read error: \(error)")
return
}
if let data = data {
print("Received: \(data)")
}
}
// AFTER — NetworkConnection for TCP/TLS
let connection = NetworkConnection(
to: .hostPort(host: "example.com", port: 443)
) {
TLS()
}
func sendAndReceive() async throws {
try await connection.send(Data("Hello".utf8))
let data = try await connection.receive(exactly: 10).content
print("Received: \(data)")
}
Skills : axiom-ios-networking, axiom-networking-legacy
Weekly Installs
89
Repository
GitHub Stars
610
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode73
claude-code69
codex68
gemini-cli66
cursor66
github-copilot63
Wagmi 3.x 以太坊 React Hooks 使用指南 - 配置、连接器与核心Hooks详解
100 周安装
ScrapeNinja:高性能网络爬虫API,绕过反爬虫,支持JS渲染与代理轮换
101 周安装
Vitest 测试框架:Vite 驱动的下一代 JavaScript/TypeScript 测试工具
102 周安装
代码重构专家:提升代码质量与可维护性的核心技术与实践指南
103 周安装
zapper技能 - Bankrbot开源AI技能库中的自动化工具,支持多平台安装
101 周安装
SEO关键词集群构建器:智能组织关键词,规划内容架构与内部链接策略
73 周安装
bind() + listen() | NWListener(using:on:) | Automatic port binding |
accept() | listener.newConnectionHandler | Callback for each connection |
getaddrinfo() | Let NWConnection handle DNS | Smart resolution with racing |
SCNetworkReachability | connection.stateUpdateHandler waiting state | No race conditions |
setsockopt() | NWParameters configuration | Type-safe options |
| Suspending function |
| Manual JSON encode/decode | Coder(MyType.self, using: .json) | Built-in Codable support |
| Custom framer | TLV { TLS() } | Built-in Type-Length-Value |
[weak self] everywhere | No [weak self] needed | Task cancellation automatic |