Search Unity

UnityWebRequest.Put fails on iOS

Discussion in 'iOS and tvOS' started by Erkberg, Oct 15, 2017.

  1. Erkberg

    Erkberg

    Joined:
    Jun 25, 2013
    Posts:
    3
    I am trying to upload a file to a vanilla Apache Webserver (v2.4.18) using UnityWebRequest and its Put(uri, bodyData)-Method. The server uses HTTPS and an authentication via username and password. Here's the code I use:
    Code (CSharp):
    1. string url = "https://" + username + ":" + password + "@" + serverUrl + "/" + filename;
    2. byte[] myData = Encoding.UTF8.GetBytes(csvString);
    3. UnityWebRequest www = UnityWebRequest.Put(url, myData);
    4. yield return www.Send();
    5. // and some debugging errors etc. after that
    In the editor this works just fine and the file is uploaded correctly, but on iOS it just doesn't work.
    What's weird is that both in the editor and on iOS the yield seems to never finish, so I don't get to the debugging code on either platform. When i put the debugging into the Update-Method www.isError is false, but the response stays empty.

    A colleague reported that he got NSURLConnection error code -1002, which is a NSURLErrorUnsupportedURL error. Because of this I tried to percent escape the password and filename, because they include characters like : and =, but to no avail. I also tried setting NSAppTransportSecurity -> NSAllowsArbitraryLoads to true in the Info.plist, but this doesn't help either.

    Do I need to configure the app or the server somehow? Why does this work in the editor but not on iOS?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,731
    Try creating an instance of System.Uri class and take value of it's AbsoluteUri property. Check if that url is valid and correct.

    Can you show the full script? I don't know of the reason why the code after yield return is not getting executed.
     
  3. Erkberg

    Erkberg

    Joined:
    Jun 25, 2013
    Posts:
    3
    I tried the AbsoluteUri property and the url is the same as before and is valid.
    The above posted code is all there is to it, I'm trying to Debug.Log("after request"); right after the yield return www.Send(); but it is never reached. (not allowed to post the username, password or serverUrl, but the filename is just "testios.csv" and the password contains an "=" if that's important, otherwise there are no special characters used)
    I crossposted this issue on Unity Answers (http://answers.unity3d.com/questions/1420928/unitywebrequestput-fails-on-ios.html) and tried the answer provided there, but the result is the same. It works perfectly fine in the editor and on Android (except for the endless yield, but the file is uploaded), but not on iOS.
    I posted that a colleague gets a NSURLConnection error code -1002, but I don't get anything when building from Xcode, no matter if I use the code posted above or the one from Unity Answers. Is there anything special I need to do to receive such error codes? At the moment I'm just running the app from Xcode on my device.

    Btw I'm using Unity 5.6.0f3, maybe this is due to some bug fixed in a later version?
     
  4. Erkberg

    Erkberg

    Joined:
    Jun 25, 2013
    Posts:
    3
    I finally got it to work. The reason it did not work before was also the reason for the endless yield of the www.Send(); I made a mistake in the callchain of the coroutine. Not exactly sure what was wrong, because I was just calling the coroutine through another coroutine which was started but did not yield the result, but hey, it works now. Thanks a lot for your answer and the help!
     
  5. Smith-Hou

    Smith-Hou

    Joined:
    Jan 2, 2018
    Posts:
    1
    Hey, hi, I have the same problem, android and PC are running fine, but the code that was returned by UnityWebRequest request on iOS real machine has been Code: -1002, please help me.
    Here's a sample of my code
    Code (CSharp):
    1.        
    2. private void SendRequest(string url)
    3.         {
    4.             StartCoroutine(SendFormUrl(url, this.formDic));
    5.             ClearFormDict();
    6.         }
    7.         private IEnumerator SendFormUrl(string url, Dictionary<string, List<string>> dic, HFBaseHttpRequest request =null)
    8.         {
    9.  
    10.             url = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(url));
    11.             Debug.Log("send the url:" + url);
    12.             UnityEngine.Networking.UnityWebRequest WebRequest = UnityEngine.Networking.UnityWebRequest.Get(url);
    13.             WebRequest.SetRequestHeader("kl-go-token", token);
    14.             WebRequest.SetRequestHeader("kl-go-device", device);
    15.             WebRequest.SetRequestHeader("kl-go-version", version);
    16.  
    17.  
    18.             yield return WebRequest.Send();
    19.  
    20.            
    21. Debug.Log( " ==result==code==== :WebRequest.Send()==" + WebRequest.responseCode);
    22.         }
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,731
    Why are you converting URL back and forth? This won't achieve anything.
    Can you paste an actual URL that you use for this request?