问题
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);
}
}
}
回答1:
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>
回答2:
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
{
}
}
来源:https://stackoverflow.com/questions/40069824/unitywebrequest-not-working-in-ios