Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

WWW class is empty on iOS

Discussion in 'iOS and tvOS' started by SimonW, Jun 22, 2012.

  1. SimonW

    SimonW

    Joined:
    Feb 20, 2012
    Posts:
    13
    I have some networking code using Unity's WWW class that reads data from a remote server. It works fine in the editor, but when I try it from an iPad the www object is completely empty - no headers, no error, nothing. Here's a stripped down version of what I'm doing:

    Code (csharp):
    1. WWW www;
    2.  
    3. public void StartLogin (string username, string password)
    4. {  
    5.     WWWForm form = new WWWForm();
    6.     form.AddField("username", username);
    7.  
    8.     Hashtable headers = form.headers;
    9.  
    10.     headers["username"] = username;
    11.     headers["password"] = password;
    12.     headers["deviceId"] = 2;
    13.  
    14.     www = new WWW(loginUrl, form.data, headers);
    15.     //StartCoroutine(WaitForLoginRequest());    
    16. }
    17.  
    18. void Update()
    19. {
    20.     if (www != null  www.isDone)
    21.     {
    22.        DoLoginRequest();
    23.     }
    24. }
    25.  
    26. void DoLoginRequest()
    27. {
    28.        //Code goes here
    29.  
    30.        www = null;
    31. }
    32.  
    (I know that passing all the data in the headers is a bit weird. I didn't write the service.)

    As you can see, I started using coroutines but switched over to a simpler version using Update (didn't help). I know the connection is getting through to the server because it's logging the attempt, but I have no idea where all my information is going on the iOS side.
     
  2. robin_notts

    robin_notts

    Joined:
    Apr 7, 2010
    Posts:
    86
    I too had problems with the WWW class on iOS. I ended up switching to a third party library to get the job done, but I would really like to see this fixed. Out of interest are you using any third party DLLs in your app? I ask because I couldn't seem to replicate this issue in a simple test app.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Do not do that with Update, use Yield instead.

    Half the WWW problems people have are caused by incorrect usage of WWW which must be called in a coroutine which is yielded either directly or till www.isDone is true, as its running in a background thread.
    The documentation doesn't point out that its asyncronous with the example showing the yield wwwobject for the fun of it ;)
     
  4. SimonW

    SimonW

    Joined:
    Feb 20, 2012
    Posts:
    13
    As I said, I did try it with both coroutines (using yield) and waiting for isDone in Update() (the version you see here). Both of these had exactly the same result.

    robin_notts: We're using a few third party libraries, but I don't know if there are custom dlls involved. I'm using unity basic, so I imagine not, but I know people with pro have been involved in the past and I'm not clear on exactly what does and doesn't work under those circumstances.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You said so but I don't see the code beside the start call, hence pointing it out that WWW on mobile will not work without coroutines.

    But in this case it might additionally be related to the headers. Does NSUrlConnection of iOS UIKit even support header providing? Cause thats what Unity uses on iOS for WWW for example (it uses the native path on every platform, hence it behaves differently on every platform).

    Any reason why you do not simply provide it through the WWWForm data to a HTTPS connection? (cause www on ios, as mentioned above, uses nsurlconnection which is perfectly fine with https)
     
  6. SimonW

    SimonW

    Joined:
    Feb 20, 2012
    Posts:
    13
    NSUrlConnection seems to provide the functions, but of course that's no guarantee they're hooked in properly on the Unity side.

    Unfortunately, this is just how the webservice I'm working with does things. We're currently trying to get it rewritten to use more standard input data and responses (right now the response is all in the header too). I don't know the reason it was originally done this way. I'll post an update if the more usual way works.