Koning Koning - 1 month ago 65
HTTP Question

UnityWebRequest not working in iOS

The examples given on https://docs.unity3d.com/Manual/UnityWebRequest.html works on Windows Desktop, Windows Mobile, Android and even in the iOS editor as expected. But when I publish the app to the Apple App Store it fails to connect to the internet. Unlike the other platforms, there is no noticeable delay and it seems to fail immediately when a request is attempted.

The error is:


Unknown Error


Is there any special way that I need to configure the XCode project to allow an iOS app to connect to the internet?

For what it is worth, here is the source code that makes the call:

IEnumerator SyncRequest(string data, Action<string> responseHandler)
{
Debug.Log("Requesting " + data);

UnityWebRequest www = UnityWebRequest.Get(baseurl + "/api-voteoptions.php?" + data);

yield return www.Send();

if (www.isError) // <-- Always true on published iOS app
{
string error = www.error; // <-- "Unknown Error"
Debug.LogWarning(error);

if (responseHandler != null)
{
responseHandler(error);
}
}
else
{
string response = www.downloadHandler.text;
Debug.Log(response);

if (responseHandler != null)
{
responseHandler(response);
}
}
}

Answer

Found a solution. It turns out I had to specify in the Info.plist file that I want to allow "insecure" http connections for the iOS app.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>just-an-example.com</key>
        <string></string>
    </dict>
</dict>
Comments