2017-10-03 1 views
0

1) Le code suivant me donne une erreur sur IOS mais fonctionne sur Android.IOS React natif fetch() POST: Échec de la demande réseau

2) On peut faire IOS i avec chercher "obtenir", mais "post" échoue

donc ce ne fonctionne pas sur ios:

var data = new FormData(); 
    data.append("fileUpload", { uri: imageUri, name: filename, type: "image/jpeg" }); 
    data.append("filename", filename); 
    data.append("name", "uploadedFile"); 
    const config = { 
     method: "POST", 
     headers: { 
      Accept: "application/json", 
      "Content-Type": "multipart/form-data;" 
     }, 
     body: data 
    }; 

    return fetch(url, config).then((response) => response.json()) 
     .then(res => { return res ;}) 
     .catch(error => { 
      console.error(error); 
      return { name: "network error", description: "" }; 
     }); 

je tente de fixer le info.plist sur IOS selon un autre poste (comme React Native fetch() Network Request Failed) mais l'erreur toujours là

<?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>CFBundleDevelopmentRegion</key> 
     <string>en</string> 
     <key>CFBundleDisplayName</key> 
     <string>myapp</string> 
     <key>CFBundleExecutable</key> 
     <string>$(EXECUTABLE_NAME)</string> 
     <key>CFBundleIdentifier</key> 
     <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> 
     <key>CFBundleInfoDictionaryVersion</key> 
     <string>6.0</string> 
     <key>CFBundleName</key> 
     <string>$(PRODUCT_NAME)</string> 
     <key>CFBundlePackageType</key> 
     <string>APPL</string> 
     <key>CFBundleShortVersionString</key> 
     <string>1.0</string> 
     <key>CFBundleSignature</key> 
     <string>????</string> 
     <key>CFBundleVersion</key> 
     <string>1</string> 
     <key>LSRequiresIPhoneOS</key> 
     <true/> 
     <key>NSAppTransportSecurity</key>     // i add this 
     <dict>            // i add this 
      <key>NSAllowsArbitraryLoads</key>    // i add this 
      <true/>           // i add this 
      <key>NSAllowsArbitraryLoadsInWebContent</key> // i add this 
      <true/>           // i add this 
     </dict>            // i add this 
     <key>NSCameraUsageDescription</key> 
     <string>Your message to user when the camera is accessed for the first time</string> 
     <key>NSLocationWhenInUseUsageDescription</key> 
     <string></string> 
     <key>NSMicrophoneUsageDescription</key> 
     <string>Your message to user when the microsphone is accessed for the first time</string> 
     <key>NSPhotoLibraryUsageDescription</key> 
     <string>Your message to user when the photo library is accessed for the first time</string> 
     <key>UIAppFonts</key> 
     <array> 
      <string>Zocial.ttf</string> 
      <string>Feather.ttf</string> 
     </array> 
     <key>UILaunchStoryboardName</key> 
     <string>LaunchScreen</string> 
     <key>UIRequiredDeviceCapabilities</key> 
     <array> 
      <string>armv7</string> 
     </array> 
     <key>UISupportedInterfaceOrientations</key> 
     <array/> 
     <key>UIViewControllerBasedStatusBarAppearance</key> 
     <false/> 
     <key>LSApplicationCategoryType</key> 
     <string></string> 
    </dict> 
    </plist> 

je tente de redémarrer projet Xcode, propre et construire tous.

un conseil? merci

+0

Avez-vous ajouté votre domaine dans le info.plist? – Raymond

Répondre

1

iOS n'autorise pas les requêtes HTTP non sécurisées. Pour ce faire, vous devez ajouter votre domaine comme une exception dans votre info.plist.

Ajouter les éléments suivants:

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSExceptionDomains</key> 
    <dict> 
    <key>yourserver.com</key> 
    <dict> 
     <!--Include to allow subdomains--> 
     <key>NSIncludesSubdomains</key> 
     <true/> 
     <!--Include to allow HTTP requests--> 
     <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
     <true/> 
     <!--Include to specify minimum TLS version--> 
     <key>NSTemporaryExceptionMinimumTLSVersion</key> 
     <string>TLSv1.1</string> 
    </dict> 
    </dict> 
</dict> 
+0

puis-je mettre plusieurs exceptions de serveur. j'ai toujours cette erreur sur d'autres api? J'utilise 2 apis hébergés dans différents domaines. NSAppTransportSecurity NSExceptionDomains domaine1.org NSIncludesSubdomains NSTemporaryExceptionAllowsInsecureHTTPLoads NSTemporaryExceptionMinimumTLSVersion TLSv1.1 domaine2.org même que domaine 1 - AlainIb il y a 30 minutes – AlainIb

+0

Oui, vous pouvez. Vous pouvez ajouter votre domaine sous le troisième ''. Il ressemblera à ceci: ' yourserver1.com [....] yourserver2.com [...] ' – Raymond