Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView With Django

I have a UIWebView that simply displays a website with the following code:

    let str8REDURL = URL(string: "https://str8red.com/")
    let str8REDURLRequest = URLRequest(url: str8REDURL!)
    webView.loadRequest(str8REDURLRequest)

The page is driven by Django on the backend that handles login authentication. Is it possible to retrieve the django user id from and display it as a message within the app?

I know within the HTML i can just use {{ user.id }}.

If the user is not logged in the message could just say "Not Logged In".

I am using Swift 3. Any help would be amazing.

like image 337
Alan Tingey Avatar asked Feb 17 '26 18:02

Alan Tingey


1 Answers

Django can generate the page with the data you need and store it in a JS variable:

<script>myData = {{ user.id }}</script>

Then you can access this something like that:

let myData = webView.stringByEvaluatingJavaScript(fromString: "myData")
like image 156
thedp Avatar answered Feb 20 '26 07:02

thedp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!