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

标签 ios swift wkwebview

我正在尝试编写一个应用程序,它接受用户输入的 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/

相关文章:

ios - 禁用 MFMessageComposeViewController 的自定义外观

ios - 导出时 CALayer 未根据视频上的拖动位置正确设置

ios - 如何在 Objective-C 中使用 Swift 文件?

ios - 为什么 iPhone 6 及更高版本 wkwebview 不接受底部点击?

ios - 尝试弹出到不存在的 View Controller swift 5.1 Xcode iOS

ios - Bundle ID 是否影响搜索结果

ios - 如何使 NSMutableAttributedString 响应设置应用程序中的动态类型文本

swift - 在 SwiftUI 中使用另一个 @State 属性值初始化 @State 属性

ios - iOS 应用程序中的 WKWebView 需要很长时间才能加载

ios9 - 不推荐使用 mediaPlaybackRequiresUserAction 和 iOS 9