Skip to content

Instantly share code, notes, and snippets.

@arabdevteam
Created November 29, 2017 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arabdevteam/816d770fc253236ca10c7b461008a4e5 to your computer and use it in GitHub Desktop.
Save arabdevteam/816d770fc253236ca10c7b461008a4e5 to your computer and use it in GitHub Desktop.
Simple website in swift
//
// ViewController.swift
// myWebBrowser
//
// Created by arabdevteam on 8/10/16.
// Copyright © 2016 arabdevteam. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIWebViewDelegate {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var myWeb: UIWebView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self // UITextField Delegation
myWeb.delegate = self // webView Delegation
let url = URL(string: "https://twitter.com/arabdevteam")!
let request = URLRequest(url: url)
myWeb.loadRequest(request)
textField.text = url.relativeString // updating TextField with url string
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let url = URL(string: "http://\(textField.text!)") // assigning textField value to url & enable user to write url without the need to "http://"
let request = URLRequest(url: url!) // making a request based on textField input text
myWeb.loadRequest(request) // loading webView with the request
textField.text = url?.relativeString // updating TextField with url string
textField.resignFirstResponder() // keyboard dismissal upon clicking search key
return true // search key as a search button
}
// start animation when webView to load a request
func webViewDidStartLoad(_ webView: UIWebView) {
activityIndicator.startAnimating()
}
// stop animation when a webView has finished request loading
func webViewDidFinishLoad(_ webView: UIWebView) {
activityIndicator.stopAnimating()
}
// stop animation when there is an error and webView is unable to load the request; such as no signal at all or server issue.. Note: you can figure a warning message in this function but we prefer not to use it for shortcut..
func webView(_ webView: UIWebView, didFailLoadWithError error: NSError?) {
activityIndicator.stopAnimating()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment