1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ドコモのOAuth SDK for iOSをSwiftで試してみた。その2

Posted at

ドコモのOAuth SDK for iOSをSwiftで試してみた。その1の続き。

その1では認証に必要なパラメータをコード内に書いていましたが、
テキストフィールドを用意してそこに書いたパラメータで認証できるようにしました。

ViewController.swift
import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    //OAuthインスタンス用変数
    var oAuth: OAuth!
    
    /*
    OAuth認証に必要なパラメータと入力用TextField
    */
    //クライアントID
    var clientID = ""
    @IBOutlet weak var clientIDTextField: UITextField!
    //クライアントシークレット
    var secret = ""
    @IBOutlet weak var secretTextField: UITextField!
    //スコープ
    var scope = ""
    @IBOutlet weak var scopeTextField: UITextField!
    //リダイレクトURI
    var redirectUri = ""
    @IBOutlet weak var redirectUrlTextField: UITextField!
    
    /*
    OAuth認証成功時に取得できる情報
    */
    //アクセストークン
    var access_token  = ""
    //リフレッシュトークン
    var refreshtoken = ""
    //トークンの有効期限
    var expires_in = ""
    //トークンタイプ
    var token_type  = ""
    
    /*
    OAuth認証失敗時に取得できる情報
    */
    //エラーコード
    var error_code: Int = 0
    //エラーメッセージ
    var rror_message = ""
    //例外オブジェクト
    var cause: NSError!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    //改行ボタンが押されたらキーボードを閉じる
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        return true
    }
    
    
    //認証ボタンが押された時の処理
    @IBAction func clickOauthButton(sender: AnyObject) {
        
        //各TextFieldに入力されたパラメータを取得
        clientID = clientIDTextField.text
        secret = secretTextField.text
        scope = scopeTextField.text
        redirectUri = redirectUrlTextField.text
        
        //OAuthインスタンス生成
        oAuth = OAuth()
        
        //OAuth認証に必要なパラメータを設定
        oAuth.setClientID(clientID)
        oAuth.setSecret(secret)
        oAuth.setScope(scope)
        oAuth.setRedirectUri(redirectUri)
        
        //OAuth 認証を開始
        oAuth.startAuth(self, onComplete: { (token) -> Void in
            //アクセストークンの取得
            self.access_token = token.accessToken
            //リフレッシュトークンの取得
            self.refreshtoken = token.refreshToken
            //トークンの有効期限の取得
            self.expires_in = token.expiresIn
            //トークンタイプの取得
            self.token_type = token.tokenType
            //認証結果アラート表示
            self.oAuthSucsses()
            
            /*
            //取得情報出力
            println("認証成功!")
            println("access_token: \(access_token)")
            println("refreshtoken: \(refreshtoken)")
            println("expires_in: \(expires_in)")
            println("token_type: \(token_type)")
            println("scope: \(scope)")
            */
            
            
            }) { (error) -> Void in
                //エラーコードの取得
                self.error_code = error.code
                //エラーメッセージの取得
                self.rror_message = error.localizedDescription
                //例外オブジェクトの取得
                self.cause = error.cause
                //認証結果アラート表示
                self.oAuthNotSucsees()
                
                /*
                //取得情報出力
                println("認証失敗!")
                println("error_code: \(self.error_code)")
                println("rror_messagself.e: \(self.rror_message)")
                println("cause: \(self.cause)")
                */
        }
    }
    
    
    /*
    認証結果アラート生成
    */
    //認証成功時
    func oAuthSucsses() {
        
        let alert = UIAlertController(title: "認証成功!", message: "認証に成功しました。", preferredStyle: UIAlertControllerStyle.Alert)
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
        
        alert.addAction(okAction)
        presentViewController(alert, animated: true, completion: nil)
        
    }
    
    //認証失敗時
    func oAuthNotSucsees(){
        
        let alert = UIAlertController(title: "認証失敗!", message: "認証に失敗しました。", preferredStyle: UIAlertControllerStyle.Alert)
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
        
        alert.addAction(okAction)
        presentViewController(alert, animated: true, completion: nil)
    }
}

スクリーンショット 2015-06-19 22.11.51.png

インターフェースが激しくダサいのはお見逃しください。
ここのコードはこうした方がいいなどあればご指摘いただけるとありがたいです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?