9
9

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.

大手銀行のAPIを使ったiOSアプリを作ってみる

Last updated at Posted at 2019-07-27

三菱UFJ銀行が提供する銀行APIを使用した簡易iOSアプリを作成してみる。
公式ページが発行するAPIキーを用いて、「個人API 口座」のテスト用データをJSON形式で取得。各種データ項目を配列に格納する。

以下、MUFG APIポータルサイト(公式)。
https://innovation.mufg.jp/api/

##実行環境
【Xcode】Version 10.3
【Swift】Version 5.0.1

##事前準備
1.MUFG APIポータルサイトから銀行APIページへ移動。アカウントを作成する。
2.「APIキー発行」タブより”APIキー発行登録”ボタンを押下。アプリ情報を登録。
3.発行されるクライアントIDを控えておく。(後ほど使用)

##実装

自身のクライアントIDやシーケンスNoをheadersとして定義する。
併せて、JSONデータを格納する為の配列を宣言。
 ※x-btmu-seq-noの例 : "x-btmu-seq-no": "20191231-aseyoshi12345678"

ViewController.swift
let headers = [
        "x-ibm-client-id": "<自身のアプリのクライアントID>",
        "x-btmu-seq-no": "<YYYYMMDD-[16桁の任意の英数字]>",
        "accept": "application/json"
]

var accountIdList: [String] = []
var accountNameList: [String] = []
var accountNameKanaList: [String] = []
var accountNoList: [String] = []
var accountTypeCodeList: [String] = []
var accountTypeDetailCodeList: [String] = []
var accountTypeNameList: [String] = []
var branchNameList: [String] = []
var branchNoList: [String] = []
var currencyCodeList: [String] = []
var primaryAccountFlagList: [String] = []

更にviewDidLoad()の中を以下の通り実装。

ViewController.swift
let request = NSMutableURLRequest(url: NSURL(string: "https://developer.api.bk.mufg.jp/btmu/retail/trial/v1/accounts/")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)

request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
        
let session = URLSession.shared
        
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {
        let httpResponse = response as? HTTPURLResponse
        var lecturerData: Data =  data!
        do {
            let json = try JSONSerialization.jsonObject(with: lecturerData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]
            for (k,v) in json {
                let arr = v as! NSArray
                for dic in arr {
                    let d = dic as! NSDictionary
                    self.accountIdList.append(d["accountId"] as? String ?? "")
                    self.accountNameList.append(d["accountName"] as? String ?? "")
                    self.accountNameKanaList.append(d["accountNameKana"] as? String ?? "")
                    self.accountNoList.append(d["accountNo"] as? String ?? "")
                    self.accountTypeCodeList.append(d["accountTypeCode"] as? String ?? "")
                    self.accountTypeDetailCodeList.append(d["accountTypeDetailCode"] as? String ?? "")
                    self.accountTypeNameList.append(d["accountTypeName"] as? String ?? "")
                    self.branchNameList.append(d["branchName"] as? String ?? "")
                    self.branchNoList.append(d["branchNo"] as? String ?? "")
                    self.currencyCodeList.append(d["currencyCode"] as? String ?? "")
                    let Temp = d["primaryAccountFlag"] as? Int ?? 0
                    self.primaryAccountFlagList.append(String(Temp))
                }
            }
        } catch {
            print(error)
        }
    }
})
dataTask.resume()

これで各配列にString型のテスト用データが格納されるようになった。
UICollectionViewに格納すれば以下のように口座情報を一覧化出来る。
Simulator Screen Shot - iPhone Xʀ - 2019-07-27 at 16.31.59.png

9
9
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
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?