
Instagram Private API Swift
ご注意ください:SwiftyInsta は現在積極的にメンテナンスされていません。
詳細については #244 を参照してください。
最新の代替手段をお探しの場合は、Swiftagram をご確認ください。
Instagram は開発者に2種類のAPIを提供しています。機能が非常に制限され廃止が近い Instagram API Platform と、Business および Creator アカウントのみの Instagram Graph API です。
しかし、Instagram アプリは3番目のタイプの API、いわゆる Private API または Unofficial API に依存しており、SwiftyInsta は完全に Swift で書かれた iOS、macOS、tvOS、watchOS クライアントです。
より良いInstagram体験をユーザーに提供したり、さまざまなタスクを自動化するボットを作成することができます。
これらの Private API は トークン や アプリ登録 を必要としませんが、外部での使用はInstagramによって 許可 されていません。
自己責任で使用してください。
File/Swift Packages/Add Package Dependency… を選択します。https://github.com/TheM4hd1/SwiftyInsta.git を貼り付けます。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 に依存しています。
Credentials// 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 が自動的にレスポンスをキャッチします。
LoginWebViewControllerlet 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 を実装し、.webView(/* your login web view */) を使用して APIHandler の authenticate メソッドに渡します。
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 など)を使用してください。
プルリクエスト と イシュー は大歓迎です。
1.* メンテナー2.* メンテナー現在、メンテナーを積極的に募集しています。
詳細については #244 を参照してください。
SwiftyInsta は MIT ライセンスの下でライセンスされています。詳細については LICENSE をご覧ください。