一个 iOS 库,可以更轻松地检查和分享网络日志,并且可以轻松嵌入到你的 iOS 应用中
![]() |
![]() |
![]() |
![]() |
![]() |
---|
要使用 Swift Package Manager 将 Sauron
集成到你的 Xcode 中,请将其添加到 Package.swift
的依赖项中
dependencies: [
.package(url: "https://github.com/volatilegg/Sauron.git", .upToNextMajor(from: "1.0"))
]
要使用 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
使用 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
}
)
将 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
}
注意 默认模式为 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
打开 RequestListPresentView()
视图以查看完整的日志 UI
NavigationLink(destination: RequestListPresentView()) {
Text("Open Log")
}
打开 RequestsListViewController
视图以查看完整的日志 UI
viewController.show(RequestsListViewController.makeViewController())