Reputation: 33
I am currently developing an app in Xcode with Swift. The general premise of the app is that when the app first loads, the user will be asked to enter a 'mobile linking code' this will essentially be added onto the end of the url as a variable which will serve as their login to the site, the rest of the authentication is done server side. Then when the user logs into the app each time after that, this variable will be auto applied to the URL so that essentially they are always auto logged in.
I have my code setup for the app and the UIAlertController loads with a text field, I am struggling on how to find out how to append the 'mobile linking code' (which the user will type into the text field) to the end of the URL on first load and then also how to append this to the URL that loads each time after that.
The code I have is as follows
At the top of my WebViewController.swift
var webviewurl = "https://mywebsite.co.uk/loginarea.php?link="
(I need to append the mobile link code to the end of that url)
Further down in my code I have my first run dialog, in which I have added a UIAlertController. This will be ran on the very first time opening the app only in which the user will input their 'mobile link code', upon clicking Submit, the webview should be redirected to the url with the data in the text field appended to the end.
if activatefirstrundialog == "true" {
if !user.bool(forKey: "firstrun")
{
user.set("1", forKey: "firstrun")
user.synchronize()
webView.stopLoading()
let ac = UIAlertController(title: "Enter Mobile Link Code", message: "Enter the mobile link code found in your Client Area", preferredStyle: .alert)
ac.addTextField()
let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned ac] _ in
let answer = ac.textFields![0]
// do something interesting with "answer" here
let url = URL(string: "https://mywebsite.co.uk/loginarea.php?link=")!
self.webView.load(URLRequest(url: url + answer))
}
ac.addAction(submitAction)
present(ac, animated: true)
}
}
}
I would be eternally grateful if someone could help me with this.
TIA
Upvotes: 3
Views: 2068
Reputation: 2859
You should store this mobile linking code
somewhere safe. you can use keychain
to store it.
To answer your question, you can concatenate the string and form a URL Like this:
let url = URL(string: "https://mywebsite.co.uk/loginarea.php?link=\(answer)")!
Here answer is a variable.
Upvotes: 0
Reputation: 24341
To use the mobileLink
between multiple app sessions, you need to save it somewhere after it is entered for the first time by the user.
Let's say we save it in UserDefaults
. You can then fetch its value accordingly like so,
if !user.bool(forKey: "firstrun") {
//your code...
let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned ac] _ in
if let answer = ac.textFields.first?.text {
UserDefaults.standard.set(answer, forKey: "mobileLink")
let url = URL(string: "https://mywebsite.co.uk/loginarea.php?link=\(answer)")!
self.webView.load(URLRequest(url: url))
}
}
//your code...
} else if let mobileLink = UserDefaults.standard.string(forKey: "mobileLink") {
let url = URL(string: "https://mywebsite.co.uk/loginarea.php?link=\(mobileLink)")!
self.webView.load(URLRequest(url: url))
}
Upvotes: 1
Reputation: 780
Did you mean something like this?
let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned ac] _ in
let answer = ac.textFields![0]
// do something interesting with "answer" here
if let answerText = answer.text, answerText.count > 0 {
if let percentageEncodedString = "https://mywebsite.co.uk/loginarea.php?link=\(answerText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let url = URL(string:percentageEncodedString) {
self.webView.load(URLRequest(url: url))
} else {
//Handle error here, url is not valid
}
} else {
//Handle error here, url cannot be encoded
}
} else {
//Handle error here, text field's text is nil or text is empty
}
}
Upvotes: 1