2015-09-14 28 views
7

對於我的應用程序,我需要爲iOS的Info.plist文件添加一些設置。我認爲最好的方法是將這些設置添加到我的config.xml文件中(我使用的是PhoneGap)。當我添加以下到config.xml文件並運行PhoneGap:修改config.xml以添加屬性到Info.plist iOS

cordova build ios 

cordova update platform ios 

沒有被添加到我的Info.plist文件,我絕對不知道這是爲什麼。編譯顯示'成功',所以我不認爲有語法錯誤。

我已經試過:

<platform name="ios"> 
    <allow-intent href="itms:*" /> 
    <allow-intent href="itms-apps:*" /> 
    <config-file target="*-Info.plist" parent="NSAppTransportSecurity"> 
     <array> 
      <dict> 
       <key>NSExceptionDomains</key> 
       <array> 
        <dict> 
         <key>s3.amazonaws.com</key> 
         <array> 
          <dict> 
           <!--Include to allow subdomains--> 
           <key>NSIncludesSubdomains</key> 
           <true/> 
           <!--Include to allow insecure HTTP requests--> 

<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
           <true/> 
           <!--Include to specify minimum TLS version--> 
           <key>NSTemporaryExceptionMinimumTLSVersion</key> 
           <string>TLSv1.1</string> 
          </dict> 
         </array> 
        </dict> 
       </array> 
      </dict> 
     </array> 
    </config-file> 
</platform> 

而且

<platform name="ios"> 
    <allow-intent href="itms:*" /> 
    <allow-intent href="itms-apps:*" /> 
    <config-file target="*-Info.plist" parent="NSAppTransportSecurity"> 
     <dict> 
      <key>NSExceptionDomains</key> 
       <dict> 
       <key>s3.amazonaws.com</key> 
       <dict> 
        <!--Include to allow subdomains--> 
        <key>NSIncludesSubdomains</key> 
        <true/> 
        <!--Include to allow insecure HTTP requests--> 
        <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
        <true/> 
        <!--Include to specify minimum TLS version--> 
        <key>NSTemporaryExceptionMinimumTLSVersion</key> 
        <string>TLSv1.1</string> 
       </dict> 
      </dict> 
     </dict> 
    </config-file> 
</platform> 

而且

<gap:config-file platform="ios" parent="NSAppTransportSecurity"> 
    <dict> 
     <key>NSExceptionDomains</key> 
     <dict> 
      <key>s3.amazonaws.com</key> 
      <dict> 
       <!--Include to allow subdomains--> 
       <key>NSIncludesSubdomains</key> 
       <true/> 
       <!--Include to allow insecure HTTP requests--> 
       <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
       <true/> 
       <!--Include to specify minimum TLS version--> 
       <key>NSTemporaryExceptionMinimumTLSVersion</key> 
       <string>TLSv1.1</string> 
      </dict> 
     </dict> 
    </dict> 
</gap:config-file> 

而且

<gap:config-file platform="ios" parent="NSAppTransportSecurity"> 
    <array> 
     <dict> 
      <key>NSExceptionDomains</key> 
      <array> 
       <dict> 
        <key>s3.amazonaws.com</key> 
        <array> 
         <dict> 
          <!--Include to allow subdomains--> 
          <key>NSIncludesSubdomains</key> 
          <true/> 
          <!--Include to allow insecure HTTP requests--> 

          <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
          <true/> 
          <!--Include to specify minimum TLS version--> 
          <key>NSTemporaryExceptionMinimumTLSVersion</key> 
          <string>TLSv1.1</string> 
         </dict> 
        </array> 
       </dict> 
      </array> 
     </dict> 
    </array> 
</gap:config-file> 

但是沒有任何內容添加到Info.plist文件中。我在這裏做錯了什麼?

+0

[通過Cordova config.xml添加條目到iOS .plist文件]的可能重複(http://stackoverflow.com/questions/22769111/add-entry-to-ios-plist-file-via-cordova-config -xml) – lifeisfoo

回答

5

我使用適用於iOS的構建鉤實現此目的。所以,在config.xml中我把這樣的:

<hook type="before_build" src="../scripts/ios_before_build.sh" /> 

裏面的:

<platform name="ios"> 

config.xml中元素

然後我會創建一個名爲文件../ scripts/ios_before_build.sh,確保它具有執行權限(chmod 755 ../scripts/ios_before_build.sh),然後將腳本設置爲使用PlistBuddy對.plist文件進行必要的更改。

val=$(/usr/libexec/plistbuddy -c "add NSAppTransportSecurity:NSAllowsArbitraryLoads bool true" platforms/ios/AppName/AppName-Info.plist 2>/dev/null) 

我抑制plistbuddy的返回代碼:

比如我在這裏對SSL加密後端的網址爲我開發不使用HTTPS應用程序的API關閉的iOS 9需求因爲如果物品已經存在,它將會失敗。在這裏,我添加了一個字典並設置了一個布爾值,但是您可以按照PlistBuddy documentation執行其他各種操作。

然後,當你這樣做:

cordova build ios 

該腳本將運行,改變你的plist那麼科爾多瓦版本將繼續。

我覺得這個更清潔,因爲我不喜歡將平臺或插件文件夾檢入到我的Cordova項目的版本控制中。

+0

謝謝。這工作像一個魅力。 –

+1

ios_before_build.sh可能需要第一行是「#!/ usr/bin/env bash「,否則會在運行掛接時顯示」command not found「 –

+0

@Simon Prickett HI,你能解釋一下如何創建.sh文件嗎?我在windows上。編譯我的應用程序,我用Application Loader上傳了一個錯誤,並提示「缺少Info.plist鍵...應用程序的Info.plist必須包含NSPhotoLibraryUsageDescription鍵......」我不知道如何與plist合作, – Sarah