ぶぴログ

開発でのあれこれをアレしていくだけのただのメモ。

Fabric - Swift2 : 認証時のエラーなど

Twitter認証エラーの時にNSErrorで得られるcodeはEnumで以下のように定義されている

/**
 *  Error codes surfaced by the Twitter SDK with the `TWTRLogInErrorDomain` error domain.
 */
public enum TWTRLogInErrorCode : Int {
    
    /**
     * Unknown error.
     */
    case Unknown
    /**
     * User denied login.
     */
    case Denied
    /**
     * User canceled login.
     */
    case Canceled
    /**
     * No Twitter account found.
     */
    case NoAccounts
    /**
     * Reverse auth with linked account failed.
     */
    case ReverseAuthFailed
    /**
     *  Refreshing session tokens failed.
     */
    case CannotRefreshSession
    /**
     *  No such session or session is not tracked
     *  in the associated session store.
     */
    case SessionNotFound
    /**
     * The login request failed.
     */
    case Failed
    /**
     * The system account credentials are no longer valid and the 
     * user will need to update their credentials in the Settings app.
     */
    case SystemAccountCredentialsInvalid
}

だから例えば認証のキャンセル時はアラートを出したくない場合は以下のように判定すれば良い

if error!.code != TWTRLogInErrorCode.Canceled.rawValue { ... }

rawValue はSwiftの列挙型のメンバに割り当てられたInt値を返却してくれる