I'm seeing an issue where I'm running the below multiple times with different URLs, and I'm getting the same data back 'sometimes' (it seems random). I'm wondering if iOS is caching some pages and giving me back the same data and not actually going to the URL.
Is there a cache? If so, is there a way to clear it before each call?
if let dataString = try? Data(contentsOf: url!) {
let pageContent = String(data: dataString, encoding: String.Encoding.utf8)
}
Take a look at init(contentsOf:options:) which provides an option called NSData.ReadingUncached
. That's evidence that the simple form is cached and an answer to how you can control whether its cached.
A better approach with even more control is via URLSession