Reputation: 380
I have an in-house iOS app that on launch will check a JSON file to see if there is a new version to download and if it is required or optional.
However, it seems the app is using a cached version of that file to check so it will never see updates. I know how to trick it into never getting a cached version: in the future I will be passing in a timestamp as a parameter to the JSON URL. However, until they get that update I will have this issue. And I want them to be able to get this update without having them re-download the app.
Is there any way to clear the cached version from the iPhone and make sure that the app is not using it? On the server I have already disabled client cache via web.config for the folder the JSON file is in and that didn't solve the issue.
Here is the code I use to check for the updates:
protected async override void OnAppearing()
{
var CurrentVersion = new Version(DependencyService.Get<IScreen>().Version);
HelperFile myHelper = new HelperFile();
//var page = new ContentPage();
//App.Current.MainPage = page;
//Pass Information to Webservice
Uri jsonUrl1 = new Uri(string.Format("https://website.com/mobileapp/VersionNo.json"));
var result = await myHelper.GetResponseString(jsonUrl1);
JObject rss = JObject.Parse(result);
var RecentVersion = new Version((string)rss["version"]);
var MinimumVersion = new Version((string)rss["minimum"]);
string ChangeLog = (string)rss["changelog"];
string ShowChangeLog = (string)rss["showchangelog"];
var isRequired = CurrentVersion.CompareTo(MinimumVersion);
var isOptional = CurrentVersion.CompareTo(RecentVersion);
if (isRequired < 0)
{
//go to forced update page
var updatePopup = new UpdatePopup(false, RecentVersion, MinimumVersion, ChangeLog, ShowChangeLog);
await Navigation.PushPopupAsync(updatePopup);
}
else if (isOptional < 0)
{
//go to optional update page
var updatePopup = new UpdatePopup(true, RecentVersion, MinimumVersion, ChangeLog, ShowChangeLog);
await Navigation.PushPopupAsync(updatePopup);
}
else
{
App.Current.MainPage = new NavigationPage(new CheckUpdatedInformation());
}
}
GetResponseString code:
public async Task<string> GetResponseString(Uri url)
{
try
{
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromMinutes(5);
using (HttpResponseMessage response = await client.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
using (HttpContent content = response.Content)
{
string myContent = await content.ReadAsStringAsync();
return myContent;
}
}
else
{
return "Error";
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
Upvotes: 0
Views: 241
Reputation: 24470
On iOS, if you want to prevent NSUrlSessionHandler from caching requests, you will have to initialize it with DisableCaching
set to true.
Right now it is implicitly used by your HttpClient, but you will need to do something like:
_client = new HttpClient(new NSUrlSessionHandler
{
DisableCaching = true
});
If you are on MAUI, it is even better to configure a HttpClientFactory which uses this NSUrlSessionHandler and have the factory create instances of HttpClient as needed. Putting HttpClient in a using statement is not recommended. It leaks sockets and could potentially starve your App and Servers you are hitting from sockets.
Upvotes: 1