SwiftyInsta
Instagram 为开发者提供了两种类型的 API:功能非常有限且即将停止使用的 Instagram API 平台,以及仅供 Business 和 Creator 账户使用的 Instagram Graph API。
但是,Instagram 应用依赖于第三种类型的 API,所谓的 Private API 或 Unofficial API,而 SwiftyInsta 是为它们而写的 iOS, macOS, tvOS 和 watchOS 客户端,全部使用 Swift 编写。您可以为您的用户提供更好的 Instagram 体验,或者编写自动化不同任务的机器人。
这些 Private API 不需要任何 令牌 或 应用注册,但它们并未获得 Instagram 的官方授权用于外部使用。使用时请自负风险。
安装
Swift Package Manager (Xcode 11 及以上版本)
- 从菜单中选择
文件
/Swift 包
/添加包依赖…
- 粘贴
https://github.com/TheM4hd1/SwiftyInsta.git
- 按照步骤操作。
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 SwiftyInsta 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftyInsta', '~> 2.0'
end
然后,运行以下命令
$ pod install
SwiftyInsta 依赖于 CryptoSwift 和 keychain-swift。
登录
凭据
// these need to be strong references.
self.credentials = Credentials(username: /* username */, password: /* password */, verifyBy: .text)
self.handler = APIHandler()
handler.authenticate(with: .user(credentials)) {
switch $0 {
case .success(let response, _):
print("Login successful.")
// persist cache safely in the keychain for logging in again in the future.
guard let key = response.persist() else { return print("`Authentication.Response` could not be persisted.") }
// store the `key` wherever you want, so you can access the `Authentication.Response` later.
// `UserDefaults` is just an example.
UserDefaults.standard.set(key, forKey: "current.account")
UserDefaults.standard.synchronize()
case .failure(let error):
if error.requiresInstagramCode {
/* update interface to ask for code */
} else {
/* notify the user */
}
}
}
一旦用户输入了双因素身份验证码或挑战码,您只需这样做
self.credentials.code = /* the code */
前面的 authenticate(with: completionHandler:)
中的 completionHandler
将自动捕获响应。
LoginWebViewController
(仅限 iOS 12 及以上版本)
let login = LoginWebViewController { controller, result in
controller.dismiss(animated: true, completion: nil)
// deal with authentication response.
guard let (response, _) = try? result.get() else { return print("Login failed.") }
print("Login successful.")
// persist cache safely in the keychain for logging in again in the future.
guard let key = response.persist() else { return print("`Authentication.Response` could not be persisted.") }
// store the `key` wherever you want, so you can access the `Authentication.Response` later.
// `UserDefaults` is just an example.
UserDefaults.standard.set(key, forKey: "current.account")
UserDefaults.standard.synchronize()
}
if #available(iOS 13, *) {
present(login, animated: true, completion: nil) // just swipe down to dismiss.
} else {
present(UINavigationController(rootViewController: login), // already adds a `Cancel` button to dismiss it.
animated: true,
completion: nil)
}
或者使用 LoginWebView
实现您自己的自定义 UIViewController
,并将其传递给 APIHandler
的 authenticate
方法,使用 .webView(/* your login web view */)
。
Authentication.Response
如果您已经持久化了一个用户的 Authentication.Response
// recover the `key` returned by `Authentication.Response.persist()`.
// in our example, we stored it in `UserDefaults`.
guard let key = UserDefaults.standard.string(forKey: "current.account") else { return print("`key` not found.") }
// recover the safely persisted `Authentication.Response`.
guard let cache = Authentication.Response.persisted(with: key) else { return print("`Authentication.Response` not found.") }
// log in.
let handler = APIHandler()
handler.authenticate(with: .cache(cache)) { _ in
/* do something here */
}
使用方法
所有端点都可以从您的 APIHandler
实例轻松访问。
let handler: APIHandler = /* a valid, authenticated handler */
// for instance you can…
// …fetch your inbox.
handler.messages.inbox(with: .init(maxPagesToLoad: .max),
updateHandler: nil,
completionHandler: { _, _ in /* do something */ })
// …fetch all your followers.
handler.users.following(user: .me,
with: .init(maxPagesToLoad: .max),
updateHandler: nil,
completionHandler: { _, _ in /* do something */ })
此外,现在响应将显示由 API 返回的 JSON
文件中包含的每个值:只需访问任何 ParsedResponse
的 rawResponse
并开始浏览,或者保持使用建议的配件(例如 User
的 username
name 等,以及 Media
的 aspectRatio
、takenAt
、content
等)。
贡献
拉取请求 和 问题 均非常欢迎。
许可协议
SwiftyInsta 采用了 MIT 许可协议。有关更多信息,请参阅 LICENSE。