ios-networking

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

iOS Networking Skill

iOS网络编程技能

Build robust network layers for iOS applications
为iOS应用构建健壮的网络层

Learning Objectives

学习目标

By completing this skill, you will:
  • Master URLSession with async/await
  • Implement type-safe API clients
  • Handle authentication flows (OAuth, JWT)
  • Build offline-first applications
完成本技能学习后,你将能够:
  • 掌握结合async/await的URLSession使用
  • 实现类型安全的API客户端
  • 处理身份验证流程(OAuth、JWT)
  • 构建离线优先的应用程序

Prerequisites

前置要求

RequirementLevel
iOS FundamentalsCompleted
SwiftIntermediate
async/awaitBasic
要求水平
iOS基础已掌握
Swift中级
async/await基础

Curriculum

课程大纲

Module 1: URLSession Fundamentals (4 hours)

模块1:URLSession基础(4小时)

Topics:
  • URLSession configuration
  • URLRequest building
  • Response handling
  • Error management
Code Example:
swift
func fetch<T: Decodable>(_ type: T.Type, from url: URL) async throws -> T {
    let (data, response) = try await URLSession.shared.data(from: url)

    guard let httpResponse = response as? HTTPURLResponse,
          (200...299).contains(httpResponse.statusCode) else {
        throw NetworkError.invalidResponse
    }

    return try JSONDecoder().decode(T.self, from: data)
}
主题:
  • URLSession配置
  • URLRequest构建
  • 响应处理
  • 错误管理
代码示例:
swift
func fetch<T: Decodable>(_ type: T.Type, from url: URL) async throws -> T {
    let (data, response) = try await URLSession.shared.data(from: url)

    guard let httpResponse = response as? HTTPURLResponse,
          (200...299).contains(httpResponse.statusCode) else {
        throw NetworkError.invalidResponse
    }

    return try JSONDecoder().decode(T.self, from: data)
}

Module 2: API Client Architecture (5 hours)

模块2:API客户端架构(5小时)

Topics:
  • Endpoint abstraction
  • Request/response interceptors
  • Retry logic with exponential backoff
  • Request deduplication
主题:
  • 端点抽象
  • 请求/响应拦截器
  • 指数退避重试逻辑
  • 请求去重

Module 3: Codable & JSON (3 hours)

模块3:Codable与JSON(3小时)

Topics:
  • Custom encoding/decoding
  • Date format handling
  • Nested JSON parsing
  • Error handling
主题:
  • 自定义编码/解码
  • 日期格式处理
  • 嵌套JSON解析
  • 错误处理

Module 4: Authentication (5 hours)

模块4:身份验证(5小时)

Topics:
  • OAuth 2.0 flow
  • JWT handling
  • Token refresh logic
  • Secure token storage
主题:
  • OAuth 2.0流程
  • JWT处理
  • 令牌刷新逻辑
  • 安全令牌存储

Module 5: Offline Support (4 hours)

模块5:离线支持(4小时)

Topics:
  • Response caching
  • Request queuing
  • Sync strategies
  • Conflict resolution
主题:
  • 响应缓存
  • 请求队列
  • 同步策略
  • 冲突解决

Module 6: Testing Network Code (3 hours)

模块6:网络代码测试(3小时)

Topics:
  • Mock URLProtocol
  • Stub responses
  • Network condition testing
主题:
  • Mock URLProtocol
  • Stub响应
  • 网络条件测试

Assessment Criteria

评估标准

CriteriaWeight
API client design30%
Error handling25%
Authentication25%
Testing20%
标准权重
API客户端设计30%
错误处理25%
身份验证25%
测试20%

Common Mistakes

常见错误

  1. Force unwrapping responses → Use proper error handling
  2. Ignoring cancellation → Respect Task cancellation
  3. Hardcoded URLs → Use configuration
  4. Missing retry logic → Implement exponential backoff
  1. 强制解包响应 → 使用正确的错误处理
  2. 忽略取消操作 → 遵循Task取消机制
  3. 硬编码URL → 使用配置文件
  4. 缺少重试逻辑 → 实现指数退避策略

Skill Validation

技能验证要求

  1. REST Client: Type-safe API client with Codable
  2. Auth Flow: OAuth 2.0 with token refresh
  3. Offline App: Cached data with sync
  4. Test Suite: 80% network code coverage
  1. REST客户端:基于Codable的类型安全API客户端
  2. 身份验证流程:带令牌刷新的OAuth 2.0
  3. 离线应用:带同步功能的缓存数据
  4. 测试套件:网络代码覆盖率达80%