Add entry to iOS .plist file via Cordova config.xml

后端 未结 15 1332
日久生厌
日久生厌 2020-11-28 21:41

I am new to the Cordova CLI.

I need to perform the following steps programmatically via Cordova.

  1. In the project .plist add a new row
  2. Enter th
相关标签:
15条回答
  • 2020-11-28 22:24

    @TachyonVortex solution seems to be the best option but was crashing down in my case. The issue was caused by an empty NSMainNibFile field that is not right converted by the plist NPM package. In the .plist file

        <key>NSMainNibFile</key>
        <string></string>
        <key>NSMainNibFile~ipad</key>
        <string></string>
    

    is converted to:

        <key>NSMainNibFile</key>
        <string>NSMainNibFile~ipad</string>
    

    I fixed it with by adding to the script:

        obj.NSMainNibFile = '';
        obj['NSMainNibFile~ipad'] = '';
    

    The script finally looks like (scripts/my-hook.js):

    var fs    = require('fs');     // nodejs.org/api/fs.html
    var plist = require('plist');  // www.npmjs.com/package/plist
    
    var FILEPATH = 'platforms/ios/***/***-Info.plist';
    
    module.exports = function (context) {
    
        var xml = fs.readFileSync(FILEPATH, 'utf8');
        var obj = plist.parse(xml);
    
        obj.GDLibraryMode = 'GDEnterpriseSimulation';
        obj.NSMainNibFile = '';
        obj['NSMainNibFile~ipad'] = '';
    
        xml = plist.build(obj);
        fs.writeFileSync(FILEPATH, xml, { encoding: 'utf8' });
    
    };
    

    and config.xml:

    <platform name="ios">
        <hook type="before_build" src="scripts/my-hook.js" />
    </platform>
    
    0 讨论(0)
  • 2020-11-28 22:30

    UPDATE: for people want to use camera with iOS >= 10. This mean, by normal you can config in plugin as:

     <!-- ios -->
     <platform name="ios">
    
         <config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
             <string></string>
         </config-file>
         <config-file target="*-Info.plist" parent="NSCameraUsageDescription">
             <string></string>
         </config-file>
          <config-file target="*-Info.plist" parent="NSPhotoLibraryUsageDescription">
             <string></string>
         </config-file>
    
     </platform>
    

    But for now, you can't config NSCameraUsageDescription and NSPhotoLibraryUsageDescription in plugin. You need to config them in platform -> iOS project by Xcode or in *-Info.plist file.

    Since iOS 10 it's mandatory to add a NSCameraUsageDescription and NSPhotoLibraryUsageDescription in the info.plist.

    Learn more: https://www.npmjs.com/package/cordova-plugin-camera

    0 讨论(0)
  • 2020-11-28 22:35

    You can use the PlistBuddy utility inside a Cordova hook script to modify the *-Info.plist file.

    For example, I have the following script under <project-root>/hooks/after_prepare/010_modify_plist.sh which adds a dictionary property and adds an entry within that dictionary:

    #!/bin/bash
    
    PLIST=platforms/ios/*/*-Info.plist
    
    cat << EOF |
    Add :NSAppTransportSecurity dict
    Add :NSAppTransportSecurity:NSAllowsArbitraryLoads bool YES
    EOF
    while read line
    do
        /usr/libexec/PlistBuddy -c "$line" $PLIST
    done
    
    true
    

    Be sure to make the script executable (chmod +x).

    The true at the end of the script is because PlistBuddy returns with an error exit code if the key being added already exists, and doesn't provide a way to detect if the key already exists. Cordova will report a build error if the hook script exits with an error status. Better error handling is possible but a pain to implement.

    0 讨论(0)
提交回复
热议问题