Sauron

"Buy Me A Coffee"

一个 iOS 库,可以更轻松地检查和分享网络日志,并且可以轻松嵌入到你的 iOS 应用中

List Simulator Screenshot - iPhone 14 Pro - 2023-04-25 at 15 45 30 Simulator Screenshot - iPhone 14 Pro - 2023-04-25 at 15 45 33 Simulator Screenshot - iPhone 14 Pro - 2023-04-25 at 15 45 35 Simulator Screenshot - iPhone 14 Pro - 2023-04-25 at 15 45 38

灵感来源于 ReqresWormhole

安装

Swift Package Manager

要使用 Swift Package Manager 将 Sauron 集成到你的 Xcode 中,请将其添加到 Package.swift 的依赖项中

dependencies: [
    .package(url: "https://github.com/volatilegg/Sauron.git", .upToNextMajor(from: "1.0"))
]

Cocoapods

要使用 Cocoapods 将 Sauron 集成到你的 Xcode 中,请将其添加到你的 Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'Sauron', '~> 1.0.0'
end

用法

1. 网络配置

默认网络配置

使用 Reqres.defaultSessionConfiguration 作为 URLSession.configuration

let url = URL(string: "https://pokeapi.co/api/v2/pokemon/108")!
let urlSessionConfig = Reqres.defaultSessionConfiguration(
  additionalHeaders: ["Content-Type":"application/json; charset=UTF-8"],
  additionalHeaders: 40,
  timeoutIntervalForResource: 40,
  requestCachePolicy: .useProtocolCachePolicy
)
let urlSession = URLSession(configuration: config, delegate: nil, delegateQueue: nil)

urlSession.dataTask(
  with: url,
  completionHandler: { (data, response, error) in
      // handling response callback
  }
)

Alamofire/Moya

Reqres.defaultSessionConfiguration 注入到 Alamofire.Session(configuration:{{configuration}})

let urlSessionConfig = Reqres.defaultSessionConfiguration(
  additionalHeaders: ["Content-Type":"application/json; charset=UTF-8"],
  additionalHeaders: 40,
  timeoutIntervalForResource: 40,
  requestCachePolicy: .useProtocolCachePolicy
)

// Usage for Alamofire
let sessionManager = Alamofire.Session(configuration: urlSessionConfig)

// Usage for Moya
var provider = MoyaProvider<PokemonAPI>(
  endpointClosure: endpointClosure,
  session: sessionManager,
  plugins: []
)

provider.request(target) { result in
  // handling response callback
}

2. 配置日志模式

注意 默认模式为 disabled,应该是生产环境 (production) 中使用的模式

使用此代码片段来控制模式

// Display all network information
Sauron.shared.logMode = .enable

// Remove all network information
Sauron.shared.logMode = .disable

// Display network information without request's headers
Sauron.shared.logMode = .disableHeader

3. 配置日志 UI

SwiftUI

打开 RequestListPresentView() 视图以查看完整的日志 UI

NavigationLink(destination: RequestListPresentView()) {
  Text("Open Log")
}

UIKit

打开 RequestsListViewController 视图以查看完整的日志 UI

viewController.show(RequestsListViewController.makeViewController())