strano
strano

Reputation: 181

iOS - Ephemeral Session and UIWebView

I'd like to load a web page into a UIWebView, using URLSession.

An ephemeral session, indeed, because i wouldn't like to store the session if, for example, i've a login on Twitter/FB/whatever site.

So, i've a UIWebView named webPage

@IBOutlet var webPage: UIWebView!

a UITextField, where i can write the URL

@IBOutlet var urlField: UITextField!

Next to this text field, i've a UIButton, that implements this action

@IBAction func sendUrl(_ sender: Any) {
    let stringUrl = urlField.text
    let url = URL (string: stringUrl!)
    openPageWithSession(url: url!)
}

and the function openPageWithSession, where i use the ephemeral session

func openPageWithSession(url: URL){
        let request = URLRequest(url: url)
        let ephemeralConfiguration = URLSessionConfiguration.ephemeral
        let ephemeralSession = URLSession(configuration: ephemeralConfiguration)

        let task = ephemeralSession.dataTask(with: request) { (data, response, error) in
            if error == nil {
                self.webPage.loadRequest(request)
            } else {
                print("ERROR: \(error)")
            }

        }

        task.resume()

    }

The loading of the web page it's ok. But if i've a login on Twitter and after that i kill the app, if i reopen the app and navigate Twitter again, i'm already logged in!

Why? The ephemeral session is not invalidate after a kill?

Upvotes: 0

Views: 2115

Answers (1)

paulvs
paulvs

Reputation: 12053

From the comments above.

Instead of self.webPage.loadRequest(request), you should use:

self.webView.load(data, mimeType: "text/html", textEncodingName: "", baseURL: url).

This way you'll be using the ephemeral session you created. Otherwise you're just loading the web view normally, without the ephemeral session at all.

Upvotes: 1

Related Questions