[英]How to initialize from the command line a value from the user defaults database for an iPhone app that is run on the simulator and/or a device
我有一個無法修改的應用程序(盡管有源代碼),該應用程序使用用戶默認系統 ( [NSUserDefaults standardUserDefaults] )中的值來配置該應用程序中的某些設置。
我正在嘗試創建一個命令行腳本,該腳本使用xcodebuild構建應用程序,然后將應用程序直接啟動到iPhone模擬器或設備中。 我希望能夠修改“用戶默認系統”的值,因此當我在iPhone模擬器或iPhone設備上對應用程序進行午餐時,它將從所需的配置開始。 我怎樣才能做到這一點?
我嘗試過的事情:
xcrun simctl spawn <
模擬器設備ID >
默認讀取,但是我的應用程序中的設置沒有出現。
使用/ usr / libexec / PlistBuddy修改〜/ Library / Developer / CoreSimulator / Devices // data / Containers / Data / Application / <
app-id >
/ Library / Preferences / <
app-name >
.plist部分地,但是我只有在第一次運行該應用程序后才能找到該文件夾,並且在實際運行該應用程序之前,我沒有找到任何簡單的方法來從命令行知道該應用程序的應用程序ID是什么iPhone模擬器。 理想情況下,我希望我的應用程序首次使用我要使用的設置運行,如果我使用PlistBuddy選項,則似乎必須運行一次才能找到該文件夾,然后再次運行一次在運行之前進行設置。
有沒有簡單的方法可以做到這一點?
我認為這將完成您想要的。
如果通過xcrun simctl launch <deviceid> <app identifier>
在模擬器中啟動應用程序, xcrun simctl launch <deviceid> <app identifier>
提供參數。
xcrun simctl help launch
Usage: simctl launch [-w | --wait-for-debugger] <device> <app identifier> [<argv 1> <argv 2> ... <argv n>]
If you want to set environment variables in the resulting environment, set them in the calling environment with a SIMCTL_CHILD_ prefix.
這些參數的標准示例是用於設置AppleLocale或AppleLanguages。 但是,只要您在參數前加一個破折號,並在其后跟隨一個不帶破折號的參數,這將使第一個參數成為應用程序默認值的鍵,第二個參數將是值。
例如,我在我的application:didFinishLaunchingWithOptions:
方法中有以下代碼 ,以便打印所有默認值:
NSLog(@"Defaults: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
在命令行中,我輸入了:
xcrun simctl launch booted com.Myapp -AppleLanguages "(ru)" -variableFromCommandLineWithoutParameters -variableFromCommandLineWithParameter "hello"
在模擬器的日志中,我發現:
1月25日18:27:46 AaronMacPro MyApp [70005]:默認值:{
AddingEmojiKeybordHandled = 1; AppleITunesStoreItemKinds = ( audiobook, "tv-episode", booklet, software, "software-update", "itunes-u", ringtone, "tv-season", movie, mix, wemix, song, tone, artist, "podcast-episode", podcast, eBook, document, album, "music-video" ); AppleKeyboards = ( "en_US@hw=US;sw=QWERTY", "emoji@sw=Emoji", "en_US@hw=US;sw=QWERTY" ); AppleKeyboardsExpanded = 1; AppleLanguages = ( ru ); AppleLanguagesDidMigrate = "9.1"; AppleLocale = "en_US"; ApplePasscodeKeyboards = ( "en_US@hw=US;sw=QWERTY", "emoji@sw=Emoji", "en_US@hw=US;sw=QWERTY" ); NSInterfaceStyle = macintosh; NSLanguages = ( ru, en ); variableFromCommandLineWithParameter = hello;
}
垃圾郵件很多,但是如果您向下滾動,則可以找到我設置的值。 AppleLanguages
鍵已用“ ru”而不是默認的“ en”填充。 我的variableFromCommandLineWithParameter
顯示為“ hello”。 我的variableFromCommandLineWithoutParameters
沒有以任何方式顯示。
我沒有像您說過的那樣使用xcrun simctl spawn
,但是根據幫助文本,如果需要的話,它也允許這樣的參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.