LoginSignup
1
2

More than 5 years have passed since last update.

plist形式ファイルを書き換える方法

Last updated at Posted at 2017-06-10

⚠️ この方法では、実機でアプリ実行中に Info.plist ファイルを書き換えることができません。 ⚠️

表題通り、アプリ実行中に Info.plist plist形式ファイル をSwiftが書き換える方法です。
Info.plist読み込みと被ってて、ググってもサクっと出てこなかったので書いておきます。

環境

  • Swift3.1
  • Xcode8.3.3

SourceCode

    func update(using value: String) {
        let filename = "Info"
        guard let filePath = Bundle.main.path(forResource: filename, ofType: "plist") else {
            return
        }
        guard let dic = NSDictionary(contentsOfFile: filePath) else {
            return
        }
        guard var data = dic.copy() as? [String: Any] else {
            return
        }
        let key = "key"
        data[key] = value

        let plist: NSDictionary = data as NSDictionary
        plist.write(toFile: filePath, atomically: false)
    }

MEMO

若干、これやっていいのか?って思ってる。
審査とか大丈夫だよね...?

👇
ダメでした...実機で動かないことを確認しました。

1
2
1

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
2