在 swift 中向 URL 添加变量

Add variable to URL in swift

我目前正在 Xcode 中使用 Swift 开发应用程序。该应用程序的一般前提是,当应用程序首次加载时,系统会要求用户输入 'mobile linking code',这实际上将作为变量添加到 url 的末尾,作为他们的变量登录该站点,其余的身份验证在服务器端完成。然后当用户之后每次登录应用程序时,这个变量将自动应用到 URL 所以基本上他们总是自动登录。

我为应用程序设置了代码,并且 UIAlertController 加载了一个文本字段,我正在努力寻找如何附加 'mobile linking code'(用户将在文本字段中键入)到第一次加载时 URL 的末尾,然后如何将其附加到之后每次加载的 URL。

我的代码如下

在我的顶部 WebViewController.swift

var webviewurl = "https://mywebsite.co.uk/loginarea.php?link="(我需要将移动 link 代码附加到那个 url 的末尾)

在我的代码中,我有第一个 运行 对话框,我在其中添加了一个 UIAlertController。这将是 运行 在第一次打开应用程序时,用户将只在其中输入他们的 'mobile link code',点击提交后,webview 应该被重定向到 url 中的数据附加到末尾的文本字段。

    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)
        }
}
}

如果有人能帮助我,我将永远感激不已。

TIA

你的意思是这样的吗?

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
    }
}

要在多个应用程序会话之间使用 mobileLink,您需要在用户首次输入后将其保存在某个地方。

假设我们将其保存在 UserDefaults 中。然后你可以像这样相应地获取它的值,

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))
}

您应该将此 mobile linking code 存放在安全的地方。你可以使用 keychain 来存储它。

要回答您的问题,您可以连接字符串并形成一个 URL,如下所示:

let url = URL(string: "https://mywebsite.co.uk/loginarea.php?link=\(answer)")!

这里answer是一个变量。