0

1) The following code give me an error on IOS but work on Android.

2) On IOS i Can make fetch with "get" but "post" fails

so this don't work on 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: "" };
        });

i try to fix the info.plist on IOS according to other post (like React Native fetch() Network Request Failed) but the error still there

    <?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>

i try to restart xcode, clean project and build all.

any advice ? thanks

1
  • Did you add your domain in the info.plist?
    – Ray
    Commented Oct 3, 2017 at 13:39

1 Answer 1

2

iOS does not allow insecure HTTP requests. In order to do so, you have to add your domain as an exception inside your info.plist.

Add the following:

<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>
2
  • can i put multiple server exceptions. i still have this error on other api ? i use 2 apis hosted in differents domaines. <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>domaine1.org</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSTemporaryExceptionMinimumTLSVersion</key> <string>TLSv1.1</string> </dict> <key>domaine2.org</key> <dict> same as domaine 1 </dict> </dict> </dict> – AlainIb 30 mins ago
    – AlainIb
    Commented Oct 10, 2017 at 14:59
  • Yes, you can. You can add your domain underneath the third <dict>. It will look like: <dict><key>yourserver1.com</key><dict>[....]</dict><key>yourserver2.com</key><dict>[...]<dict>
    – Ray
    Commented Oct 11, 2017 at 5:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.