【问题标题】:Plist not changing value [swift]Plist 不改变值 [swift]
【发布时间】:2016-11-14 09:59:02
【问题描述】:

我想为我的应用实现应用内版本,但我不明白为什么不起作用?显示我已成功添加的输出,但是当我从 Xcode 打开 pList 文件时,它没有改变?为什么?这是我的代码:

func version(){

    let urlString = jocomAPIKey + "/feed/version"
    let CFBundleShortVersionKey = "CFBundleVersion"

    //First get the nsObject by defining as an optional anyObject
    let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleVersion"]
    let version = nsObject as! String
    print("app version: \(version)")


    Alamofire.request(.POST, urlString , parameters: ["os" : "iphone"])

        .responseData { response in

            let versionXML = SWXMLHash.parse(response.data!)
            let versionString = (versionXML["rss"]["channel"]["item"]["version"].element?.text)
            print("version string: \(versionString)")

            if let plist = Plist(name: "Info") {

                let dict = plist.getMutablePlistFile()!
                dict[CFBundleShortVersionKey] = versionString
                //3
                do {
                    try plist.addValuesToPlistFile(dict)
                    print("added")
                } catch {
                    print(error)
                }
                //4
                print(plist.getValuesInPlistFile())
            } else {
                print("Unable to get Plist")
            }


        }


}

【问题讨论】:

  • 您不能在运行时写入 info.plist(也不能写入应用程序包中的任何其他文件)。
  • @vadian 但我想实现自己的 api 版本来强制用户每次更新,我该怎么做?请指导我

标签: swift plist


【解决方案1】:

您不能在运行时更新您的 (Bundle plist)。如果您想使用,那么您只需在您的Document Directory 上创建一个copy,然后您可以执行任何operation

Write and Read a plist in swift with simple data

【讨论】:

  • 但是我想实现自己的api版本来强制用户每次更新,我该怎么做?请指导我
  • 一旦你将bundle plis复制到文档目录,你就可以检查或更新你的api版本了。
  • 您的意思是将 plist 文件复制到新的 plist 文件中?那么只有我可以读写plist?
  • 是的,文件路径必须是应用程序文件目录,并且访问所有文件目录plist。
  • 如果您不知道如何复制,请查看此答案:- stackoverflow.com/questions/25100262/…
【解决方案2】:

Apple 不允许在应用程序运行时更新 info.plist 更新。

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多