4

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);
        }
    }
}
Jasper Citi
  • 1,673
  • 1
  • 23
  • 31
  • This could be anything. I could be a problem with your server or problem with the url you are sending the request to. Unfortunately, the only way to help is to make it possible to replicate your problem. To do this, you have have to provide the url and the parameter you are passing to it. This includes the `data`, `responseHandler`, and the `baseurl` variables. – Programmer Oct 16 '16 at 15:58
  • As I said, it works on all other platforms and even on iOS from a Apple Macbook 100% without issues. It is ONLY when I publish the app on the Apple App Store that I get the above mentioned issues. So 1) It cannot be the server because it works from the other platforms 2) it cannot be the code because the same code without modifying it works on the other platforms 3) it cannot be the data because it is a GET request for exactly the same data. The only difference is the App Store and whatever Unity is doing behind the scenes to pack the code into ObjectiveC or into an App "bundle". – Jasper Citi Oct 16 '16 at 16:08
  • **"As I said, it works on all other platforms and even on iOS"** That doesn't matter if it works on other platforms or not. I've solved few problems like this on SO and they end up being the sever or the url. **"it cannot be the code because the same code without modifying it works on the other platforms"** Sure, we can just blame it on App Store. We both know that we don't have the power to fix anything on App Store so I should move on. Happy coding! – Programmer Oct 16 '16 at 16:19
  • I suspect I am missing some sort of XCode project settings or flag or something, but I could not find anything helpful besides adding a "wifi" flag to the "capabilities" in the plist file, but even that made no difference. All other mobile device projects I had to explicitly indicate that I required "Internet" permission for the app, but I couldn't find anything similar for ios. I guess that is the issue but I don't know how and where to set it. – Jasper Citi Oct 17 '16 at 06:18

2 Answers2

4

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>
Jasper Citi
  • 1,673
  • 1
  • 23
  • 31
0
IEnumerator loadingImage()
{
    string url = "http://picturekeyboardapps.in/UnityApp/CarStunt/getCarTextures.php";
    UnityWebRequest webrequest = UnityWebRequest.Get(url);
    yield return webrequest.SendWebRequest();
    if (webrequest.isNetworkError || webrequest.isHttpError)
    {
        print("Network Error"); // return Unkown Error IOS Mobile
    }
    else
    {
    }
}
Panos
  • 18,992
  • 6
  • 45
  • 54
aartiui 12
  • 41
  • 3