NativeScript调试时如何查看应用设置内容?
Great question! When using NativeScript's App Settings for persistent client-side storage, you can easily inspect and modify stored values while debugging—though the steps vary a bit between iOS and Android. Here's how to do it for each platform, plus a handy debugging shortcut:
For iOS (Simulator or Debugged Device)
NativeScript stores App Settings values in a .plist file within your app's sandbox. To access it:
- Open Xcode and go to Window > Devices and Simulators from the top menu.
- Switch to the Simulators tab, select your active simulator, then find your app in the list of installed apps.
- Click the Download Container... button to save a copy of your app's sandbox to your local machine.
- Right-click the downloaded
.xcappdatafile and choose Show Package Contents. - Navigate to
AppData/Library/Preferences/[Your App's Bundle ID].plist—this file contains all key-value pairs saved via App Settings. - You can edit this file with Xcode or a plain text editor. To apply changes, either re-upload the container via the Devices and Simulators window, or simply restart your app in the simulator.
For Android (Emulator or Debugged Device)
On Android, App Settings are stored in an XML file in your app's private data directory. You have two easy ways to access it:
Option 1: Use Android Studio's Device File Explorer
- Open Android Studio and enable the Device File Explorer (go to View > Tool Windows > Device File Explorer if it's not visible).
- Navigate to
/data/data/[Your App's Package Name]/shared_prefs/[Your App's Package Name]_preferences.xml. - Right-click the XML file to download it to your computer, edit it with a text editor, then right-click again to upload the modified file back to the device.
- Restart your app (or force-stop it via Settings) to see the changes take effect.
Option 2: Use ADB Command Line
If you prefer working in the terminal:
- First confirm your device/emulator is connected:
adb devices - Pull the settings file to your local machine:
adb pull /data/data/[Your App's Package Name]/shared_prefs/[Your App's Package Name]_preferences.xml ./ - Edit the XML file with your favorite text editor.
- Push the modified file back to the device:
adb push ./[Your App's Package Name]_preferences.xml /data/data/[Your App's Package Name]/shared_prefs/ - Restart your app to apply the changes.
Quick Debugging Shortcut: Log All App Settings
For faster inspection without digging into platform files, add a helper function to your Angular code to print all stored App Settings values to the console:
import { ApplicationSettings } from "@nativescript/core"; // Call this function from a component, service, or dev menu logAllAppSettings(): void { const allKeys = ApplicationSettings.getAllKeys(); console.log("=== All App Settings ==="); allKeys.forEach(key => { let value: any; if (ApplicationSettings.hasKey(key)) { // Check value type (since App Settings supports string/number/boolean) if (ApplicationSettings.getBoolean(key, null) !== null) { value = ApplicationSettings.getBoolean(key); } else if (!isNaN(ApplicationSettings.getNumber(key, NaN))) { value = ApplicationSettings.getNumber(key); } else { value = ApplicationSettings.getString(key); } console.log(`${key}: ${value}`); } }); }
Call this function during debugging, and you'll see all stored values directly in your NativeScript console.
内容的提问来源于stack exchange,提问作者user1261710

