2021-06-29

Connection refused by iOS when using UnityWebRequest

It works fine in the Unity Editor but in iOS the app gives errors in the logs by saying what I post below. It can't find the file. I have a Json file in StreamingAssets in Unity and then it is copied to persistentDataPath.

In Xcode for the info.plist I added "App Transport Security Settings" & "Allows Arbitrary Loads" and "Allow Local Networking". If not it blocks the connection.

This is the error I get in Xcode.

2021-06-28 11:50:47.655068-0400 Jumper[39664:10886549] [connection] nw_socket_handle_socket_event [C2.1:2] Socket SO_ERROR [61: Connection refused] 2021-06-28 11:50:47.664973-0400 Jumper[39664:10886549] [connection] nw_socket_handle_socket_event [C2.2:2] Socket SO_ERROR [61: Connection refused] 2021-06-28 11:50:47.665697-0400 Jumper[39664:10886549] Connection 2: received failure notification 2021-06-28 11:50:47.665793-0400 Jumper[39664:10886549] Connection 2: failed to connect 1:61, reason -1 2021-06-28 11:50:47.666259-0400 Jumper[39664:10886549] Connection 2: encountered error(1:61) 2021-06-28 11:50:47.668734-0400 Jumper[39664:10886548] Task <23601751-EACC-40FF-81CC-C5F6B6CDC4F6>.<2> HTTP load failed, 0/0 bytes (error code: -1004 [1:61]) 2021-06-28 11:50:47.677576-0400 Jumper[39664:10886545] Task <23601751-EACC-40FF-81CC-C5F6B6CDC4F6>.<2> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x2811a8f00 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <23601751-EACC-40FF-81CC-C5F6B6CDC4F6>.<2>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <23601751-EACC-40FF-81CC-C5F6B6CDC4F6>.<2>" ), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://localhost/private/var/containers/Bundle/Application/3C4E9065-AC69-46EA-98E6-0265B0963F23/Jumper.app/Data/Raw/language_data.json, NSErrorFailingURLKey=http://localhost/private/var/containers/Bundle/Application/3C4E9065-AC69-46EA-98E6-0265B0963F23/Jumper.app/Data/Raw/language_data.json, _kCFStreamErrorDomainKey=1} Uploading Crash Report FileNotFoundException: Could not find file "/var/mobile/Containers/Data/Application/455E9171-A4AD-48C8-9400-0BF577E39324/Documents/language_data.json" at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00000] in <00000000000000000000000000000000>:0 at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize, System.Boolean checkHost) [0x00000] in <00000000000000000000000000000000>:0 at SimpleLocalizationSystem.Language.HasValue (System.String key) [0x00000] in <00000000000000000000000000000000>:0

Here is the code that I use.

private static void Request(string file)
{
    var loadingRequest = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, file));
    loadingRequest.SendWebRequest();
    while (!loadingRequest.isDone)
    {
        if (loadingRequest.isNetworkError || loadingRequest.isHttpError)
        {
            break;
        }
    }
    if (loadingRequest.isNetworkError || loadingRequest.isHttpError)
    {
        // Some error happened.
    }
    else
    {
        File.WriteAllBytes(Path.Combine(Application.persistentDataPath, file), loadingRequest.downloadHandler.data);
    }
}

// Loads key from "StreamingAssets/language_data.json" file.
public static string LoadJson(string key)
{
    Request(jsonFileName);
    var jsonPath = Path.Combine(Application.persistentDataPath, jsonFileName);
    using (StreamReader r = new StreamReader(jsonPath))
    {
        var N = JSON.Parse(r.ReadToEnd());
        string result = N[GetLanguage()][key];
        return result;
    }
}


from Recent Questions - Stack Overflow https://ift.tt/3dmQRhz
https://ift.tt/eA8V8J

No comments:

Post a Comment