웹뷰 webview
//
// ViewController.swift
// WebView@
//
// Created by MacBookPro on 2017. 12. 14..
// Copyright © 2017년 MacBookPro. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UIWebViewDelegate {
@IBOutlet weak var urlTextFild: UITextField!
@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var indicator: UIActivityIndicatorView!
func loadWebPage(_ url:String){
let myUrl = URL(string: url)
let myRequest = URLRequest(url:myUrl!)
webView.loadRequest(myRequest)
}
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
loadWebPage("http://www.creativeshake.co.kr")
}
//찾기
@IBAction func search(_ sender: UIButton) {
let url = urlTextFild.text!
let checkedUrl = checkUrl(url)
urlTextFild.text = ""
loadWebPage(checkedUrl)
}
func checkUrl(_ urlCheck: String) -> String{
var strUrl = urlCheck
let flag = strUrl.hasPrefix("http://")
if !flag {
strUrl = "http://" + strUrl
}
return strUrl
}
//site1
@IBAction func site1(_ sender: UIButton) {
loadWebPage("http://abc1211.tistory.com/")
}
//site2
@IBAction func site2(_ sender: UIButton) {
loadWebPage("http://www.naver.com")
}
//html
@IBAction func html(_ sender: UIButton) {
let htmlString = "<h1>에이치티엠엘 테스트</h1><p>변수를 이용한 웹페이지</p><p><a href=\"http://naver.com\">네이버</a>로 이동</p>"
webView.loadHTMLString(htmlString, baseURL: nil)
}
//file
@IBAction func file(_ sender: UIButton) {
let myHtmlBundle = Bundle.main
let filePath = myHtmlBundle.path(forResource: "test", ofType: "html")
loadWebPage(filePath!)
}
//stop
@IBAction func stop(_ sender: UIBarButtonItem) {
webView.stopLoading()
}
//refresh
@IBAction func refresh(_ sender: UIBarButtonItem) {
webView.reload()
}
//back
@IBAction func back(_ sender: UIBarButtonItem) {
webView.goBack()
}
//forward
@IBAction func forward(_ sender: UIBarButtonItem) {
webView.goForward()
}
//인디케이터 시작
func webViewDidStartLoad(_ webView: UIWebView) {
indicator.startAnimating()
}
//인디케이터 끝
func webViewDidFinishLoad(_ webView: UIWebView) {
indicator.stopAnimating()
}
}
'ios 뽀개기 > ios 응용해보기' 카테고리의 다른 글
탭뷰 (0) | 2017.12.15 |
---|---|
맵뷰 map view (0) | 2017.12.14 |
데이트 피커뷰와 alert이용해서 간단한 알람시계 구현 (0) | 2017.12.14 |
두개의 피커 뷰 이용하기 (0) | 2017.12.13 |
데이트 픽커뷰 이용해서 알람시계 구현 (0) | 2017.12.13 |
댓글