LoginSignup
6
12

More than 5 years have passed since last update.

CocoaPodsで入れたライブラリのライセンス表記画面を作りたい

Last updated at Posted at 2017-04-05

端末の設定画面に謝辞を入れる方法

よくこちらの方法で、謝辞を入れることが多かったのですが...
[iOS] アプリの設定画面にバージョン表記と謝辞を自動で設定する / Developer.IO

アプリの中でも表示させたい

TableView&自作レイアウトのセルで、どうにか表示できないかとトライしてみた。

イメージはこんな感じ

sc500.png

表示用のデータを取得する

Setting.bundleの中にAcknowledgements.plistがある前提。
Pods/Targets Support Files/{PROJECT_NAME}/Acknowledgements.plistを直接見に行けるかどうかは試してない)

var items: [Any] = []
if let path = Bundle.url(forResource: "Acknowledgements", 
                         withExtension: "plist", 
                         subdirectory: nil, 
                         in: Bundle.main.url(forResource: "Settings", withExtension: "bundle")!) {

    let dic = NSDictionary(contentsOf: path)!
    self.items = dic["PreferenceSpecifiers"] as! [Any]

    // 最初と最後以外が必要な情報
    self.items.removeFirst()
    self.items.removeLast()
}

セルをカスタマイズ

シンプルにこんな感じにしてみた。
スクリーンショット 2017-04-06 00.51.48.png

セルの高さを可変にする

これだけで可変にできるようになったのはありがたい。

self.tableView.estimatedRowHeight = 100
self.tableView.rowHeight = UITableViewAutomaticDimension

表示させる部分

ライブラリを作る際に自動生成されるLICENSEファイルには、最後に改行が付いている。
(最後に改行コードが2つ付いているライブラリは、おそらくそのまま使用しているのだろう)

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "licenseCell", for: indexPath) as! LicenseCell

    let item = self.items[indexPath.row] as! NSDictionary

    cell.titleLabel.text = item["Title"] as? String
    cell.licenseLabel.text = item["License"] as? String

    // 最後に付いてる改行を取り除きたかった
    cell.footertextLabel.text = (item["FooterText"] as! String).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

    return cell
}

最後に...

plistファイルからのデータ取得ができることを知ったのは、個人的に大きい。

6
12
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
6
12