Cordova Phonegap IOS App Settings.Bundle Possible?

此生再无相见时 提交于 2019-11-30 09:23:56

To create a Settings bundle that will work without having to muck in platforms/ios/, create a local plugin in your project.

./src/ios/plugin.xml

<?xml version="1.0" encoding="UTF-8"?>

<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
    id="com.example.application.settings"
    version="0.4.2">

    <name>Application Settings</name>
    <description>My Application's Default Settings</description>
    <license>Proprietary</license>
    <keywords>preferences, settings, default</keywords>
    <repo>https://github.com/</repo>

    <platform name="ios">    
        <resource-file src="Settings.bundle" />
    </platform>
</plugin>

In Xcode, open ./src/ios, and create a new Settings.bundle

./src/ios/Settings.bundle/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>API Server</string>
            <key>Type</key>
            <string>PSGroupSpecifier</string>
        </dict>
        <dict>
            <key>AutocapitalizationType</key>
            <string>None</string>
            <key>AutocorrectionType</key>
            <string>No</string>
            <key>DefaultValue</key>
            <string>https://api.example.com</string>
            <key>IsSecure</key>
            <false/>
            <key>Key</key>
            <string>name_preference</string>
            <key>KeyboardType</key>
            <string>Alphabet</string>
            <key>Type</key>
            <string>PSTextFieldSpecifier</string>
        </dict>
    </array>
    <key>StringsTable</key>
    <string>Root</string>
</dict>
</plist>

At the root of your project, run cordova plugin add ./src/ios. It will say Installing "com.dataonline.dolores.settings" for ios.

Use me.apla.cordova.app-preferences to load those settings from Javascript.

./src/client/preferences.js

function ApplicationPreferences(){
    var _this = this;
    this.server = window.location.origin;
    document.addEventListener('deviceready', function(){
        function loaded(server){_this.server = server;}
        plugins.appPreferences.fetch(loaded, function(){}, 'api_server');
    });
};

applicationPreferences = new ApplicationPreferences();
// Later..
$.get(applicationPreferences.server + "/api/data");

Edit Switched from two <source-file>s to one <resource-file>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!