
Instagram Private API Swift
Si prega di notare che SwiftyInsta non è più mantenuto attivamente.
Fare riferimento a #244 per maggiori informazioni.
Dai un'occhiata a Swiftagram se stai cercando alternative aggiornate.
Instagram offre due tipi di API agli sviluppatori. La Instagram API Platform (estremamente limitata nelle funzionalità e prossima alla dismissione), e la Instagram Graph API solo per account Business e Creator.
Tuttavia, le app Instagram si basano su un terzo tipo di API, la cosiddetta Private API o API non ufficiale, e SwiftyInsta è un client iOS, macOS, tvOS e watchOS per esse, scritto interamente in Swift. Puoi provare a creare un'esperienza Instagram migliore per i tuoi utenti, o scrivere bot per automatizzare diversi compiti.
Queste Private API non richiedono token o registrazione dell'app ma non sono autorizzate da Instagram per uso esterno. Usalo a tuo rischio.
File/Swift Packages/Add Package Dependency… dal menu.https://github.com/TheM4hd1/SwiftyInsta.git.CocoaPods è un gestore di dipendenze per progetti Cocoa. Puoi installarlo con il seguente comando:
$ gem install cocoapods
Per integrare SwiftyInsta nel tuo progetto Xcode usando CocoaPods, specificalo nel tuo Podfile:
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftyInsta', '~> 2.0'
end
Quindi, esegui il seguente comando:
$ pod install
SwiftyInsta dipende da CryptoSwift e 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 */
}
}
}
Una volta che l'utente ha digitato il codice di autenticazione a due fattori o il codice di verifica, basta fare
self.credentials.code = /* the code */
E il completionHandler nel precedente authenticate(with: completionHandler:) catturerà automaticamente la risposta.
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)
}
Oppure implementa il tuo UIViewController personalizzato usando LoginWebView, e passalo a un metodo authenticate di APIHandler usando .webView(/* your login web view */).
Authentication.ResponseSe hai già salvato la risposta Authentication.Response di un utente:
// 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 */
}
Tutti gli endpoint sono facilmente accessibili dalla tua istanza 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 */ })
Inoltre, le risposte ora mostrano ogni singolo valore contenuto nel file JSON restituito dall'API: basta accedere a qualsiasi ParsedResponse rawResponse e iniziare a navigare, oppure attenersi agli accessori suggeriti (ad esempio username, name di User, ecc. e aspectRatio, takenAt, content di Media, ecc.).
Pull request e issue sono più che benvenuti.
1.* maintainer2.* maintainerSiamo attivamente alla ricerca di manutentori.
Fare riferimento a #244 per maggiori informazioni.
SwiftyInsta è concesso in licenza sotto la licenza MIT. Vedi LICENSE per maggiori informazioni.