基本的 Google Drive HTTP API 客户端,不依赖 Google 的 SDK。 没有外部依赖。
使用 Swift Package Manager 将 GoogleDriveClient
库添加为项目的依赖项。
使用 Google Cloud Console 配置 OAuth 2.0 客户端 ID。 使用 iOS
应用类型。
配置您的应用程序,使其可以处理登录重定向。 对于 iOS 应用,您可以通过在 Info.plist
中添加或修改 CFBundleURLTypes
来实现。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.1234-abcd</string>
</array>
</dict>
</array>
创建客户端
import GoogleDriveClient
let client = GoogleDriveClient.Client.live(
config: .init(
clientID: "1234-abcd.apps.googleusercontent.com",
authScope: "https://www.googleapis.com/auth/drive",
redirectURI: "com.googleusercontent.apps.1234-abcd://"
)
)
确保 redirectURI
包含之前定义的 scheme。
该软件包提供了一个基本的实现,用于将敏感数据安全地存储在钥匙串中。 (可选)您可以提供您自己的、自定义的钥匙串实现,而不是使用默认的钥匙串。
import GoogleDriveClient
let keychain = GoogleDriveClient.Keychain(
loadCredentials: { () async -> GoogleDriveClient.Credentials? in
// load from secure storage and return
},
saveCredentials: { (GoogleDriveClient.Credentials) async -> Void in
// save in secure storage
},
deleteCredentials: { () async -> Void in
// delete from secure storage
}
)
let client = GoogleDriveClient.Client.live(
config: .init(...),
keychain: keychain
)
此存储库包含一个使用 SwiftUI 构建的 iOS 应用程序示例。
GoogleDriveClient.xcworkspace
。Example
Xcode 项目中。GoogleDriveClientExampleApp
构建方案运行应用程序。GoogleDriveClient
库的 UI。该示例应用程序使用 Dependencies 来管理其自身的内部依赖项。 有关 Dependencies
库的更多信息,请查看 官方文档。
GoogleDriveClient (Xcode Workspace)
├─ swift-google-drive-client (Swift Package)
| └─ GoogleDriveClient (Library)
└─ Example (Xcode Project)
└─ GoogleDriveClientExampleApp (iOS Application)
GoogleDriveClient.xcworkspace
。GoogleDriveClient
scheme 来构建库和运行单元测试。版权所有 © 2023 Dariusz Rybicki Darrarski
许可证: MIT