17

iOSアプリケーションのバージョン番号を設定ファイルに出力したいだけです。

設定ファイルをアプリケーションフォルダに追加する必要があることを理解しています。

ビルドして実行すると、標準設定バンドルに付属する4つの設定が表示されます。

単純な読み取り専用文字列を取得するために、2番目の値を次のように変更します

ここに画像の説明を入力してください

コード(didFinishLaunchingWithOptions :)では、次のように呼び出します。

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
[[NSUserDefaults standardUserDefaults] setValue:version forKey:@"version_number"];
[[NSUserDefaults standardUserDefaults] synchronize];

驚いたことに、何も起こりません。Group要素とtoogleスイッチとスライダーが表示されますが、タイトル行は表示されません。誰かが私が欠けているものを知っていますか?

どうもありがとうございます!

4

6 に答える 6

26

私も同じ問題を抱えていました。Titleプロパティを表示するには、Settings.bundle「デフォルト値」も追加する必要があります(空の場合もあります)。

1)Titleオブジェクト(私の場合はアイテム0)を右クリックし、[行の追加]を選択します。

2)作成した行のドロップダウンで[デフォルト値]を選択します

ここに画像の説明を入力してください

3)表示したいdidFinishLaunchingWithOptions値を設定します。例:NSUserDefaults

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"0.0.1" forKey:@"appVersion"];
[defaults synchronize];

結果:

ここに画像の説明を入力してください

于 2014-03-28T12:22:47.663 に答える
24

わかりました、私もこの問題を抱えていました。解決策(一種)は、デフォルト値フィールドを提供し、それに値を与えることでした。これは実際にはドキュメントに明示的に記載されています。デフォルト値はTitleプロパティの必須フィールドであるため、指定しないと、タイトルは設定ペインに表示されません。残念ながら、一度設定すると、おそらく設計どおりに値を変更できないようです。ドキュメントには、読み取り専用のプロパティであるとも記載されています。私が試しようとしている解決策は、新しいビルドを作成するたびに、Root.plistファイルにバージョン番号を明示的に入れることです。SUPERは理想的ではありませんが、うまくいくと思います。

編集:設定バンドルのバージョン番号の更新に関するこの投稿をチェックしてください

編集:わかりました、これは機能しました(上記の投稿のおかげで、bashスクリプトを少しハッキングしましたが、ほとんど経験がありませんでした)。これがスクリプトです(「スクリプトの実行」ビルドでインラインで記述しました)。段階):

#!/bin/bash

builtInfoPlistPath=${TARGET_BUILD_DIR}/${INFOPLIST_PATH}

#increment the build number
buildNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$builtInfoPlistPath")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$builtInfoPlistPath"

#compose the version number string
versionString=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$builtInfoPlistPath")
versionString+=" ("
versionString+=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$builtInfoPlistPath")
versionString+=")"

#write the version number string to the settings bundle
#IMPORTANT: this assumes the version number is the first property in the settings bundle!
/usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:0:DefaultValue $versionString" "Settings.bundle/Root.plist"

... 以上です!チャームのように機能します!それが私の問題を解決したので、それがあなたの問題に役立つことを願っています。現在の唯一の問題は、ビルド番号とのわずかな不一致です...

編集:...この投稿に対するvakioの2番目のコメントで修正しました。代わりに、info.plistへのパスを(スクリプトの実行フェーズの前に)すでに処理されているパスに設定します。

編集:これが私の最新バージョンです。これは外部ファイルにあり、ビルド番号を増やす前に一部のソースファイルが変更されていることを確認します。

 #!/bin/bash

 #note: for simplicity, it's assumed that there's already a bundle version (which is an integer) and a version string. set them in the Summary pane!

 #get path to the BUILT .plist, NOT the packaged one! this fixes the off-by-one bug
 builtInfoPlistPath=${TARGET_BUILD_DIR}/${INFOPLIST_PATH}
 echo "using plist at $builtInfoPlistPath"

 modifiedFilesExist=false
 #this is the modification date to compare to -- there's a possible bug here, if you edit the built plist directly, for some reason. probably you shouldn't do that anyways.
 compModDate=$(stat -f "%m" "$builtInfoPlistPath")

 for filename in *
 do
     modDate=$(stat -f "%m" "$filename")
     if [ "$modDate" -gt "$compModDate" ]
     then
         modifiedFilesExist=true;
         echo "found newly modified file: $filename"
         break
     fi
 done

 if $modifiedFilesExist
 then
     echo "A file is new, bumping version"

     #increment the build number
     buildNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$builtInfoPlistPath")
     echo "retrieved current build number: $buildNumber"
     buildNumber=$(($buildNumber + 1))
     /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$builtInfoPlistPath"
     /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

     #compose the version number string
     versionString=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$builtInfoPlistPath")
     versionString+=" ("
     versionString+=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$builtInfoPlistPath")
     versionString+=")"

     #write the version number string to the settings bundle
     #IMPORTANT: this assumes the version number is the second property in the settings bundle!
     /usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:1:DefaultValue $versionString" "Settings.bundle/Root.plist"
 else
     echo "Version not incremented -- no newly modified files"
 fi 
于 2012-10-31T18:24:12.877 に答える
3

設定バンドルをに同期できますがNSUserDefaults、奇妙なことに、最初は同期しません。最初に[設定]から値を取得する必要があります。NSUserDefaultsその後、でそれらの値に対して行った編集は、NSUserDefaults[設定]バンドルに自動的に適用されます。

この素敵な記事を参照しました。

編集:

バージョンを保存するだけの場合は、このようなものが機能します。(このサンプルコードは、ある意味ではやり過ぎですが、フローを理解するのは簡単なはずです)

//Get the bundle file
NSString *bPath = [[NSBundle mainBundle] bundlePath];
NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];

//Get the Preferences Array from the dictionary
NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];

//Save default value of "version_number" in preference to NSUserDefaults 
for(NSDictionary * item in preferencesArray) {
    if([[item objectForKey:@"key"] isEqualToString:@"version_number"]) {
        NSString * defaultValue = [item objectForKey:@"DefaultValue"];
        [[NSUserDefaults standardUserDefaults] setObject:defaultValue forKey:@"version_number"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

//Save your real version number to NSUserDefaults
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
[[NSUserDefaults standardUserDefaults] setValue:version forKey:@"version_number"];
[[NSUserDefaults standardUserDefaults] synchronize];    
于 2012-10-17T11:26:55.817 に答える
3

これは私のために働いた:

Root.plistをソースとして編集します。ファイルを右クリックして、[名前を付けて開く]->[ソースコード]を選択します。

タイトルの部分を追加します:

   <key>PreferenceSpecifiers</key>
        <array>
            <dict>
                <key>DefaultValue</key>
                <string>NoVersion</string>
                <key>Key</key>
                <string>Version</string>
                <key>Title</key>
                <string>Version</string>
                <key>Type</key>
                <string>PSTitleValueSpecifier</string>
            </dict>

そして-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

これを追加:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [[NSUserDefaults standardUserDefaults] setObject:version forKey:@"Version"];

アプリが最初に起動された後に機能します。

于 2013-01-21T12:37:18.083 に答える
0

迅速なコードを書かない別の解決策は次のとおりです。

1/設定バンドルを作成する

これにより、デバイスの設定にアプリの新しいセクションが作成されます

  • プロジェクト名を右クリック->新しいファイル->設定バンドル

ここに画像の説明を入力してください

2/Root.plistを変更します

これにより、アプリの設定に表示する内容が設定されます-新しい設定バンドル内で、Root.plist- >名前を付けて開く->ソースコードを右クリックします

  • 次のコードをコピーして貼り付けます。

    <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
     <plist version="1.0">
     <dict>
         <key>PreferenceSpecifiers</key>
         <array>
            <dict>
                <key>Title</key>
                <string>About</string>
                <key>Type</key>
                <string>PSGroupSpecifier</string>
            </dict>
            <dict>
                <key>DefaultValue</key>
                <string></string>
                <key>Key</key>
                <string>version_preference</string>
                <key>Title</key>
                <string>Version</string>
                <key>Type</key>
                <string>PSTitleValueSpecifier</string>
            </dict>
        </array>
        <key>StringsTable</key>
        <string>Root</string>
    </dict>
    </plist>
    

3/実行スクリプトを作成する

これにより、常にInfo.plistからアプリのバージョンが取得され、アプリの設定に表示されます。

  • 左側のパネルでプロジェクトターゲットに移動し、[ビルドフェーズ]をクリックします
  • 「+」ボタンをクリックして、「新規実行スクリプトフェーズ」をクリックします
  • [最新の実行スクリプト]セクションで、以下をコピーして貼り付けます。

    #Getting Current Version
    VERSIONNUM=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString"   "${PROJECT_DIR}/${INFOPLIST_FILE}")
    
    #Setting the version number in settings
    /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:1:DefaultValue $VERSIONNUM" <YOUR-APP-NAME>/Settings.bundle/Root.plist
    
  • YOUR-APP-NAMEをアプリ名に置き換えるだけです

于 2017-03-22T16:03:06.600 に答える
-1

NSUserDefaultsは、設定ファイルに値を書き込みません。アプリのユーザーデフォルトのplistにのみデータが保存されます。

別のファイルに何かを保存するには、これを自分で作成する必要があります。多分これは役立つでしょう:

iOS-plistにデータを書き込む方法は?

于 2012-10-17T11:18:48.410 に答える