07장 웹뷰 WebView




//

//  ViewController.swift

//  Web

//

//  Created by stayfoolish on 17/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController,UIWebViewDelegate {

    @IBOutlet var txtUrl: UITextField!

    @IBOutlet var myWebView: UIWebView!

    @IBOutlet var myActivityIndicator: UIActivityIndicatorView!

    

    func loadWebPage(_ url: String){

        let myUrl = URL(string: url)

        let myRequest = URLRequest(url: myUrl!)

        myWebView.loadRequest(myRequest)

    }

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        myWebView.delegate = self

        loadWebPage("https://developer.apple.com/kr/swift")

        

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    func webViewDidStartLoad(_ webView: UIWebView) {

        myActivityIndicator.startAnimating()

    }

    

    func webViewDidFinishLoad(_ webView: UIWebView) {

        myActivityIndicator.stopAnimating()

    }

    func checkUrl(_ url: String) -> String {

        var strUrl = url

        let flag = strUrl.hasPrefix("http://")

        if !flag {

            strUrl = "http://" + strUrl

        }

        return strUrl

    }


    @IBAction func btnGotoUrl(_ sender: UIButton){

        let myUrl = checkUrl(txtUrl.text!)

        txtUrl.text = ""

        loadWebPage(myUrl)

    }

    @IBAction func btnGoSite1(_ sender: UIButton){

//        loadWebPage("http://fallinmac.tistory.com")

        loadWebPage("http://www.gurubee.net/")

        

    }

    @IBAction func btnGoSite2(_ sender: UIButton){

        loadWebPage("http://www.easyspub.co.kr/")

    }

    @IBAction func btnLoadHtmlString(_ sender: UIButton){

        let htmlString = "<h1> HTML String </h1><p> String 변수를 이용한 웹페이지 </p> <p><a href=\"https://developer.apple.com/kr/swift\">swift</a>로 이동</p>"

        myWebView.loadHTMLString(htmlString, baseURL: nil)

    }

    @IBAction func btnLoadHtmlFile(_ sender: UIButton){

        let myHtmlBundle = Bundle.main

        let filePath = myHtmlBundle.path(forResource: "htmlView", ofType: "html")

        loadWebPage(filePath!)

    }

    @IBAction func btnStop(_ sender: UIBarButtonItem){

        myWebView.stopLoading()

    }

    @IBAction func btnReload(_ sender: UIBarButtonItem){

        myWebView.reload()

    }

    @IBAction func btnGoBack(_ sender: UIBarButtonItem){

        myWebView.goBack()

    }

    @IBAction func btnGoForward(_ sender: UIBarButtonItem){

        myWebView.goForward()

    }

}




8장 MapView 맵뷰




//

//  ViewController.swift

//  Map

//

//  Created by stayfoolish on 17/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit

import MapKit




class ViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet var myMap: MKMapView!

    @IBOutlet var lblLocationInfo1: UILabel!

    @IBOutlet var lblLocationInfo2: UILabel!

    

    let locationManager = CLLocationManager()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        lblLocationInfo1.text = ""

        lblLocationInfo2.text = ""

        locationManager.delegate = self

        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        locationManager.requestWhenInUseAuthorization()

        locationManager.startUpdatingLocation()

        myMap.showsUserLocation = true

        

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    func goLocation(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span : Double) -> CLLocationCoordinate2D{

        let pLocation = CLLocationCoordinate2DMake(latitudeValue, longitudeValue)

        let spanValue = MKCoordinateSpanMake(span, span)

        let pRegion = MKCoordinateRegionMake(pLocation, spanValue)

        myMap.setRegion(pRegion, animated: true)

        return pLocation

    }

    

    func setAnnotation(latitudeValue: CLLocationDegrees, longitudeValue : CLLocationDegrees, delta span : Double, title strTitle : String, subtitle strSubtitle: String){

        let annotation = MKPointAnnotation()

        annotation.coordinate = goLocation(latitudeValue: latitudeValue, longitudeValue: longitudeValue, delta: span)

        annotation.title = strTitle

        annotation.subtitle = strSubtitle

        myMap.addAnnotation(annotation)

    }

    


    

    

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

        let pLocation = locations.last

         _ = goLocation(latitudeValue: (pLocation?.coordinate.latitude)!, longitudeValue: (pLocation?.coordinate.longitude)!, delta: 0.01)

        CLGeocoder().reverseGeocodeLocation(pLocation!, completionHandler: {

            (placemarks, error) -> Void in

            let pm = placemarks!.first

            let country = pm!.country

            var address:String = country!

            if pm!.locality != nil {

                address += " "

                address += pm!.locality!

            }

            if pm!.thoroughfare != nil {

                address += " "

                address += pm!.thoroughfare!

            }

            self.lblLocationInfo1.text = "현재 위치"

            self.lblLocationInfo2.text = address

        })

        locationManager.stopUpdatingLocation()

    }

 

    


    @IBAction func sgChangeLocation(_ sender: UISegmentedControl) {

        

        if sender.selectedSegmentIndex == 0 {

            self.lblLocationInfo1.text = ""

            self.lblLocationInfo2.text = ""

            locationManager.startUpdatingLocation()

        }else if sender.selectedSegmentIndex == 1 {

            setAnnotation(latitudeValue: 37.751853, longitudeValue: 128.87605740000004, delta: 1, title: "한국폴리텍대학 강릉캠퍼스", subtitle: "강원도 강릉시 남산초교길 121")

            self.lblLocationInfo1.text = "보고 계신 위치"

            self.lblLocationInfo2.text = "한국폴리텍대학 강릉캠퍼스"

        }else if sender.selectedSegmentIndex == 2 {

            setAnnotation(latitudeValue: 37.5307871, longitudeValue: 126.8981, delta: 0.1, title: "이지스퍼블리싱", subtitle: "서울시 영등포구 당산로 41길 11")

            self.lblLocationInfo1.text = "보고 계신 위치"

            self.lblLocationInfo2.text = "이지스퍼블리싱 출판사 "

        }

    }


}




09장 PageControl 페이지 컨트롤




//

//  ViewController.swift

//  PageControl

//

//  Created by stayfoolish on 17/10/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


var images = ["01.png","02.png","03.png","04.png","05.png","06.png"]


class ViewController: UIViewController {

    @IBOutlet var imgView: UIImageView!

    @IBOutlet var pageControl: UIPageControl!

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        pageControl.numberOfPages = images.count

        pageControl.currentPage = 0

        pageControl.pageIndicatorTintColor = UIColor.green

        pageControl.currentPageIndicatorTintColor = UIColor.red

        imgView.image = UIImage(named: images[0])

        

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }


    @IBAction func pageChanged(_ sender: UIPageControl) {

        imgView.image = UIImage(named: images[pageControl.currentPage])

    }

    

}
















+ Recent posts