[英]Xamarin.ios does NOT work on real iOS device but it works on Simulator
我構建了一個移動應用程序。 此應用程序在 iOS 模擬器、Android 模擬器和 Android 真實設備上運行良好。 但是當我在 TestFlight 上發布它並安裝在真正的 iOS 設備上時,這個應用程序無法運行。 我沒有收到任何錯誤。 我的微調器(激活器指示器)一直在運行,我無法從 HttpResponse 獲得任何響應。 我用Web 服務構建了這個應用程序。 因此,我正在使用http://46.221..... url 來發布或獲取請求。 由於安全原因,我認為iOS不支持http請求。 但如果這是真的那么為什么這個應用程序在模擬器上運行得很好? 我找不到任何解決方案。 這是我的部分代碼:
public async Task<BSUser> ValidateUser(string userName, string password)
{
string url = Xamarin.Essentials.Preferences.Get(Constants.URL_KEY, "") + "/api/Validateuser";
HttpClient _Client = new HttpClient();
var data = new Dictionary<string, string>
{
{"userName", userName},
{"password", password}
};
string jsonData = JsonConvert.SerializeObject(data);
HttpContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage httpResponse = await _Client.PostAsync(url, content);
if (httpResponse.IsSuccessStatusCode)
{
var responseData = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
var result = JsonConvert.DeserializeObject(responseData).ToString();
UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(result);
BSUser value = new BSUser();
value.UserName = userInfo.userCode;
return value;
}
else
{
return null;
}
}
catch (SystemException)
{
return null;
}
}
我發現了我項目的問題。 它與 HTTPS-HTTP 或安全情況無關。 這是如此荒謬和不合邏輯,但這是真正的問題。 我的應用程序在任何 iOS 模擬器上運行得很好,但是,模擬器的最低版本是 Visual Studio 中的iPhone-8 。 但是當我在iPhone-6 的真實設備上安裝我的應用程序時,出現了Newtonsoft.Json.JsonSerializationException錯誤。 但是我的 HttpResponse 返回了 200OK ,我看到來自我的服務的所有值都是 json。我認為舊版本和新版本的 iPhone 轉換 Json 序列化之間存在一些差異。
看起來你真的應該利用 HTTPS 而不是 HTTP。你可以參考這些頁面以獲取更多信息:
這是ios中ATS的問題,如果你想使用HTTP,你可以配置它。 https://learn.microsoft.com/en-us/xamarin.ios/app-fundamentals/ats#configuring-ats-options 。
其他人遇到和你一樣的問題: Xamarin Forms,iOS不會在WebView打開鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.