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. Dismiss Notice

UnityWebRequest never finishing on iOS device

Discussion in 'Scripting' started by RobAmmerlaan, Aug 21, 2018.

  1. RobAmmerlaan

    RobAmmerlaan

    Joined:
    Jun 25, 2018
    Posts:
    4
    Hello everyone,

    I'm running into a problem when using UnityWebRequest to try and check my network connection on iOS devices.

    Code (csharp):
    1. public static IEnumerator HasNetworkConnection(Action onSuccess, Action onError = null) {
    2.     var url = https:// + [ API url ] + /
    3.     var request = UnityWebRequest.Get(url);
    4.  
    5.     yield return request.SendWebRequest();
    6.     Debug.Log (request.downloadHandler.text);
    7.     if (request.isNetworkError || request.isHttpError) {
    8.         Debug.Log ("Error " + request.error);
    9.         if (onError != null)
    10.            onError.Invoke();
    11.         // Show dialog
    12.     }
    13.     else
    14.         onSuccess.Invoke ();
    15. }
    This code works on Windows, MacOS and Android devices and correctly shows a dialog if there is no connection and fires the onSuccess callback when there is a connection.

    When I log request.downloadProgress on iOS it constantly prints 0 and goes on forever without ending. So it won't get past
    Code (csharp):
    1.  yield return request.SendWebRequest();
    Is this a common problem on iOS? Is there another solution that works for all devices?

    Thanks in advance.
     
    Last edited: Aug 21, 2018
  2. RobAmmerlaan

    RobAmmerlaan

    Joined:
    Jun 25, 2018
    Posts:
    4
    I found out that this problem has to do with the activated services on my iPhone.
    There is a SIM card inserted but I can't use mobile network with it. When 'Mobile Data' is enabled though, and WIFI is disabled,
    Code (csharp):
    1. Debug.Log(Application.internetReachability);
    still prints 'ReachableViaCarrierDataNetwork'.

    That is probably why my UnityWebRequest will fire and think it has a connection, even though there is no real data stream up or down. I noticed that when it has gone on for around 30 minutes without any progress it does actually stop and return an error: 'NSURLConnection finished with error'.

    I don't really want to make my application WIFI only. Is there a way for me to check if the carrier data network is able to upload and download data?
     
  3. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Never encountered this before, unitywebrequest works fine on iOS for us. For what its worth, a cleaner way of checking for network is:


    Code (CSharp):
    1. if (Application.internetReachability != NetworkReachability.NotReachable)
    2.             {}
    What iOS and Unity version are you using?
     
  4. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Interesting! To my knowledge there is no such method, but you could overwrite unityrequests timeout to return before the 30 minutes:

    https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest-timeout.html
     
  5. RobAmmerlaan

    RobAmmerlaan

    Joined:
    Jun 25, 2018
    Posts:
    4
    I've used that way of checking before this one, but that didn't work in my case. That code should be used to check if your are connected to WIFI or mobile data, but not if you can establish a connection.

    https://docs.unity3d.com/ScriptReference/Application-internetReachability.html
    say this:
    'Note: Do not use this property to determine the actual connectivity. E.g. the device can be connected to a hot spot, but not have the actual route to the network. Non-handhelds are considered to always be capable of NetworkReachability.ReachableViaLocalAreaNetwork.'

    EDIT:
    Forgot including my versions. I'm using Unity 2017.4.6f1 and my iPhone is running iOS 11.4.1
     
    Last edited: Aug 21, 2018
  6. RobAmmerlaan

    RobAmmerlaan

    Joined:
    Jun 25, 2018
    Posts:
    4
    I've put timeout on 3 seconds and it works very well! I was afraid that timeout meant it would automatically stop after x seconds, regardless of the download progress. But when it does receive data, it doesn't stop. This is what I needed, thanks a lot!

    I do hope that Unity changes the UnityWebRequest to automatically handle this, so I don't need to set a timeout.
     
  7. shadow_tm

    shadow_tm

    Joined:
    Jul 2, 2014
    Posts:
    8
    Hi! I have the same problems
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,639
    On the other thread about this it was discovered that this issue happens when people use ARKit. Do you use ARKit too?
     
  9. Azirell

    Azirell

    Joined:
    Oct 16, 2015
    Posts:
    29
    I have same problem. I use ARKit.
     
  10. SlavoSS

    SlavoSS

    Joined:
    Jul 14, 2016
    Posts:
    2
    Same here...
     
  11. Azirell

    Azirell

    Joined:
    Oct 16, 2015
    Posts:
    29
    Last edited: Dec 15, 2020
  12. Azirell

    Azirell

    Joined:
    Oct 16, 2015
    Posts:
    29
    btw, .net web requests seem to work fine.
    but I'm not able to rewrite my app requesting functionality to .net so fast.
    so it's not for sure