使用字段的输出作为 url
Using output from field as url
我正在尝试编写一个应用程序,该应用程序接受 URL 的用户输入,然后移动到下一个视图并使用该 URL 在浏览器中加载。
我有输入页面,我想我已经保存了它,但不确定如何在最终视图中使用该数据并将其加载到浏览器中 - 我尝试的方法只是让屏幕变白 - 我在我希望输入 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
}
}
嘿@Danny 这是解决方案。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet var txtField: UITextField!
@IBAction func NextBtn(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier:
"ViewController2") as! ViewController2
vc.passUrl = txtField.text!
self.navigationController?.pushViewController(vc, animated: true)
}
}
//下一个Class是.
class ViewController2: UIViewController {
@IBOutlet var webView: UIWebView!
var passUrl = String()
override func viewDidLoad() {
super.viewDidLoad()
print(passUrl)
let websiteUrl = URL(string: "http://\(passUrl)")
//let websiteUrl = URL(string: "http://tablet.brainsplurge.co.uk")//open to check
var urlRequest: URLRequest? = nil
if let anUrl = websiteUrl {
urlRequest = URLRequest(url: anUrl)
}
if let aRequest = urlRequest {
webView.loadRequest(aRequest)
}
}
这是我的解决方案,有点类似于 SAXENA 的,但更完整。
ViewController1:
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。
我正在尝试编写一个应用程序,该应用程序接受 URL 的用户输入,然后移动到下一个视图并使用该 URL 在浏览器中加载。
我有输入页面,我想我已经保存了它,但不确定如何在最终视图中使用该数据并将其加载到浏览器中 - 我尝试的方法只是让屏幕变白 - 我在我希望输入 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
}
}
嘿@Danny 这是解决方案。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet var txtField: UITextField!
@IBAction func NextBtn(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier:
"ViewController2") as! ViewController2
vc.passUrl = txtField.text!
self.navigationController?.pushViewController(vc, animated: true)
}
}
//下一个Class是.
class ViewController2: UIViewController {
@IBOutlet var webView: UIWebView!
var passUrl = String()
override func viewDidLoad() {
super.viewDidLoad()
print(passUrl)
let websiteUrl = URL(string: "http://\(passUrl)")
//let websiteUrl = URL(string: "http://tablet.brainsplurge.co.uk")//open to check
var urlRequest: URLRequest? = nil
if let anUrl = websiteUrl {
urlRequest = URLRequest(url: anUrl)
}
if let aRequest = urlRequest {
webView.loadRequest(aRequest)
}
}
这是我的解决方案,有点类似于 SAXENA 的,但更完整。
ViewController1:
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。