关于 Siwa
Siwa 是 SwiftyInsta 的辅助框架,用于解决登录问题,例如 sentry_block
错误。
您需要结合这两个框架才能使用 Instagram API
中所有可用的功能。
使用 loginHandler
请求登录验证,使用 apiHandler
执行其他请求,例如 like, follow, comment, 接收媒体
等。
安装
CocoaPods
pod 'Siwa'
手动
要在项目中手动使用此库,您可能需要:
- 在
通用 > 链接的框架和库
中添加编译后的框架 - 克隆项目,右键单击根项目,然后选择“添加文件...”,然后选择
Siwa.xcodeproj
。之后,转到“项目 > 集成库”并选择Siwa.framework
,构建项目并导入Siwa
创建API处理器
import SwiftyInsta
import Siwa
// ...
var loginHandler: Siwa.APIHandler
var apiHandler: APIHandlerProtocol
init() {
loginHandler = try! Siwa.APIBuilder()
.setUser(user: Siwa.User(username: "username", password: "password"))
.setRequestDelay(delay: .zero)
.build()
apiHandler = try! APIBuilder()
.createBuilder()
.setHttpHandler(urlSession: URLSession(configuration: .default))
.setRequestDelay(delay: DelayModel.init(min: 0, max: 0))
.setUser(user: SessionStorage.create(username: "username", password: "password"))
.build()
}
登录
func login() {
loginHandler.login { (resLogin) in
if resLogin.isSucceeded {
if let cookeis = loginHandler.getCookies() {
let sessionCache = SessionCache.from(cookeis: cookeis)
apiHandler.login(cache: sessionCache, completion: { (cacheLogin) in
if cacheLogin.isSucceeded {
// ...
} else {
// ...
}
}
}
} else {
guard let loginStatus = resLogin.value else { return }
switch loginStatus {
case .checkpointRequired:
// ...
case .twoFactorRequired:
// ...
case .inccorrectPassword, .invalidUsername:
// ...
default:
// ...
}
}
}
}
处理挑战
enum VerificationMethod: String {
case email = "1"
case sms = "0"
}
// request security code
try loginHandler.sendChallengeVerification(method: .sms, completion: { (resChallenge) in
guard let challengeStatus = resChallenge.value else { return }
switch challengeStatus {
case .codeSent:
// security code sent to email/sms
default:
// failed to send code, probably selected method is wrong.
}
}
// send security code
try loginHandler.sendChallengeSecurityCode(code: code) { (resChallenge) in
guard let challengeStatus = resChallenge.value else { return }
switch challengeStatus {
case .accepted:
let cookies = self.loginHandler.getCookies()
// ...
case .checkpointDismiss:
self.loginHandler.loginAfterCheckpointDismissed(completion: { (resDismiss) in
guard let dismissValue = resDismiss.value else { return }
switch dismissValue {
case .alreadyLoggedIn:
let cookies = self.loginHandler.getCookies()
// ...
case .checkpointLoop:
// user needs to trust login via instagram app, then login again
case .twoFactorRequired:
// a security code sent to user's phonenumber
// you need to send security code here.
case .default:
// ...
}
}
case .incorrect:
// incorrect challenge security code.
case default:
// ...
}
}
处理双因素认证
let code = "security code or backup code"
try loginHandler.sendTwofactorSecurityCode(code: code, completion: { (resTwofactor) in
guard let twofactorValue = resTwofactor.value else { return }
switch twofactorValue {
case .success:
let cookies = self.loginHandler.getCookies()
// ...
default:
// ...
}
}