gpt4 book ai didi

ios - 使用字段的输出作为 url

转载 作者:行者123 更新时间:2023-11-28 14:29:13 25 4
gpt4 key购买 nike

我正在尝试编写一个应用程序,它接受用户输入的 URL,然后移动到下一个 View 并使用该 URL 在浏览器中加载。

我有输入页面,我认为我已经保存了它,但不确定如何在最终 View 中使用该数据并将其加载到浏览器中 - 我尝试的方法只是让屏幕变白 - 我已经包括一个我希望输入 url 去的 url - 请看下面的代码 -

//
// ViewController.swift
// webView
//
// Created by Yash Patel on 18/10/17.
// Copyright © 2017 Yash Patel. All rights reserved.
//

import UIKit
import WebKit


class lockdownfield {
weak var lockdowntest: UITextField? {

func viewDidLoad() {
UserDefaults.standard.setValue(lockdownfield?.self, forKey: "lockdowntest")
}
return nil }
}
class ViewController: UIViewController {

@IBOutlet weak var webview: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://tablet.brainsplurge.co.uk/video-playlists/lancome-selfridges-oxford-st-1/")
let request = URLRequest(url: url!)
webview.load(request)
}

override var prefersStatusBarHidden: Bool {
return true
}
}

最佳答案

这是我的解决方案,有点类似于 SAXENA 的,但更完整。

View Controller 1:

class ViewController1: UIViewController {

@IBOutlet var urlTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

//If user is opening the app the second time, then we just redirect him directly to the webView with the saved URL
let secondLoad = UserDefaults.standard.bool(forKey: "secondLoad")
if secondLoad == true {
let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
vc.userURL = UserDefaults.standard.url(forKey: "userURL") as! URL
self.present(vc, animated: false, completion: nil)
}
}

@IBAction func goToNextScreen(_ sender: UIButton) {

let vc = storyboard?.instantiateViewController(withIdentifier:
"ViewController2") as! ViewController2
vc.userURL = URL(string: urlTextField.text!)
//Save the URL for further usage
UserDefaults.standard.set(userURL, forKey: "userURL")
self.navigationController?.pushViewController(vc, animated: true)
// if you don't have a navigationController embeded into your ViewController use this function instead
//self.present(vc, animated: true, completion: nil)
}
}

ViewController2:

class ViewController2: UIViewController {

@IBOutlet var webView: UIWebView!
var userURL: URL!

override func viewDidLoad() {
super.viewDidLoad()

let urlRequest = URLRequest(url: userURL)
webView.loadRequest(urlRequest)
UserDefaults.standard.set(true, forKey: "secondLoad")

}

TODO://在将用户转到下一页之前,您需要检查用户是否输入了有效的 URL。这是一个可以为您做到这一点的 pod: https://github.com/adamwaite/Validator

如果它不是有效的 URL,显示错误弹出窗口并让用户输入正确的 URL,否则不要将他发送到 ViewController2。

关于ios - 使用字段的输出作为 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51359030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com