jeudi 2 février 2017

Swift Playgrounds Custom Web Browser

I wanted to try to make a web browser in swift playgrounds for my iPad and whenever I go to a website, the URL box disappears and I do not know why. `

import UIKit import PlaygroundSupport import WebKit

class TextFieldViewController : UIViewController, UITextFieldDelegate {

  var label : UILabel!
  var textField : UITextField!
  var button : UIButton!
  var buttonLabel : UILabel!

  func goToWebsite (url:String) {
      let xURL = url
      let secure = true

      let webView = WKWebView()
      PlaygroundPage.current.liveView = webView
      var website = URL(string:"https://\(xURL)")
      if secure {
          website = URL(string:"https://\(xURL)")
      } else {
          website = URL(string:"http://\(xURL)")
      }
      let msRequest = URLRequest(url:website!)
      webView.load(msRequest)
      webView.reload()
  }

  override func loadView() {
      // UI
      let view = UIView()
      view.backgroundColor = .white

      textField = UITextField()
      textField.borderStyle = .line
      textField.text = "google.com"

      button = UIButton()

      //goToWebsite(url: textField.text!)

      view.addSubview(textField)
      view.addSubview(button)

      label = UILabel()
      buttonLabel = UILabel()
      view.addSubview(label)
      view.addSubview(buttonLabel)

      self.view = view

      // Layout
      textField.translatesAutoresizingMaskIntoConstraints = false
      button.translatesAutoresizingMaskIntoConstraints = false
      label.translatesAutoresizingMaskIntoConstraints = false
      buttonLabel.translatesAutoresizingMaskIntoConstraints = false
      let margins = view.layoutMarginsGuide
      NSLayoutConstraint.activate([
          textField.topAnchor.constraint(equalTo: margins.topAnchor, constant: 0),
          button.topAnchor.constraint(equalTo: margins.topAnchor, constant: 20),
          textField.leadingAnchor.constraint(equalTo: margins.leadingAnchor),
          button.leadingAnchor.constraint(equalTo: margins.leadingAnchor),
          textField.trailingAnchor.constraint(equalTo: margins.trailingAnchor),
          button.trailingAnchor.constraint(equalTo: margins.trailingAnchor),

          label.leadingAnchor.constraint(equalTo: textField.leadingAnchor),
          buttonLabel.leadingAnchor.constraint(equalTo: button.leadingAnchor),
          label.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 10),
          buttonLabel.topAnchor.constraint(equalTo: button.bottomAnchor, constant: 30)
        ])

      label.text = "Search"

      // Events
      //textField.addTarget(self, action: #selector(updateLabel), for: UIControlEvents.editingChanged)
      button.addTarget(self, action: #selector(search), for: UIControlEvents.allTouchEvents)

      updateLabel()
  }


  func updateLabel() {
      //self.label.text = textField.text
      //goToWebsite(url: self.label.text!)
  }

  func search() {
      goToWebsite(url: self.label.text!)
      //loadView()
  }

} PlaygroundPage.current.liveView = TextFieldViewController()

`




Aucun commentaire:

Enregistrer un commentaire