
Instagram 비공개 API Swift
SwiftyInsta는 더 이상 활발히 유지 관리되지 않습니다.
자세한 내용은 #244를 참조하세요.
최신 대안을 찾고 계신다면 Swiftagram을 확인해보세요.
Instagram은 개발자에게 두 종류의 API를 제공합니다. Instagram API Platform (기능이 매우 제한적이며 곧 중단될 예정)과, 비즈니스 및 크리에이터 계정 전용인 Instagram Graph API입니다.
그러나 Instagram 앱은 세 번째 유형의 API, 소위 Private API 또는 _비공식 API_에 의존하며, SwiftyInsta는 이들을 위한 iOS, macOS, tvOS 및 watchOS 클라이언트로, 전적으로 Swift로 작성되었습니다. 사용자를 위한 더 나은 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 */
}
}
}
사용자가 2단계 인증 코드 또는 챌린지 코드를 입력하면 다음과 같이 하면 됩니다.
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 등)를 그대로 사용할 수 있습니다.
Pull requests 및 _issues_는 언제나 환영합니다.
1.* 관리자2.* 관리자관리자를 적극 모집하고 있습니다.
자세한 내용은 #244를 참조하세요.
SwiftyInsta는 MIT 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 LICENSE를 참조하세요.