iOS

[ IOS ] ์›น ์•ฑ ๋งŒ๋“ค๊ธฐ - ์›น ๋ทฐ (Web View), ํˆด๋ฐ” (Toolbar), ์•กํ‹ฐ๋น„ํ‹ฐ ์ธ๋””์ผ€์ดํ„ฐ (Activity Indicator)

Forest Yun 2021. 8. 5. 00:03
728x90

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

๋ฌธ์ œ

์›น ๋ทฐ์— HTML ํŒŒ์ผ์„ ์ฝ์–ด ๋””์Šคํ”Œ๋ ˆ์ดํ•˜๊ณ  ์ž์‹ ์˜ ๋ธ”๋กœ๊ทธ ๋งํฌ๋ฅผ ๊ฑธ์–ด ํด๋ฆญ์‹œ ์ด๋™ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณด์ž. ๊ทธ๋ฆฌ๊ณ  URL ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์›น ๋ทฐ์— ํ•ด๋‹น ์›น ํŽ˜์ด์ง€๊ฐ€ ๋œจ๋„๋ก ๊ตฌํ˜„ํ•˜๋ผ. ๋”ํ•˜์—ฌ ํˆด๋ฐ”์— ์ •์ง€, ์ƒˆ๋กœ ๊ณ ์นจ, ์ด์ „ ํŽ˜์ด์ง€ ์ด๋™, ๋‹ค์Œ ํŽ˜์ด์ง€ ์ด๋™ ๋ฒ„ํŠผ์„ ๊ตฌํ˜„ํ•˜์—ฌ ์›น ํŽ˜์ด์ง€ ๊ด€๋ จ ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•˜๋ผ.

 

 

๊ตฌํ˜„ ์ˆœ์„œ

  1. ๋ ˆ์ด์•„์›ƒ ๊ตฌ์„ฑ
  2. ์•„์šธ๋ › ๋ณ€์ˆ˜์™€ ์•ก์…˜ ํ•จ์ˆ˜ ์ƒ์„ฑ
  3. ์•ฑ ์‹œ์ž‘ ํ™”๋ฉด ์ดˆ๊ธฐํ™” - HTML ํŒŒ์ผ๋กœ ์›น ํŽ˜์ด์ง€ ์ƒ์„ฑ
  4. url ์ž…๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„
  5. ํˆด๋ฐ” ๋ฒ„ํŠผ ๊ธฐ๋Šฅ ๊ตฌํ˜„
  6. ์•กํ‹ฐ๋น„ํ‹ฐ ์ธ๋””์ผ€์ดํ„ฐ ๊ตฌํ˜„

 

์ตœ์ข… ๊ฒฐ๊ณผ

 

 

 

 

 

 

 

 


1. ๋ ˆ์ด์•„์›ƒ ๊ตฌ์„ฑ

 

 

 

1.1 ์›น ๋ทฐ Web View

: ์›น ์ฝ˜ํ…์ธ ๋ฅผ ๋ทฐ(view) ํ˜•ํƒœ๋กœ ๋ณด์—ฌ ์ฃผ๋Š” ์•ฑ. HTML๋กœ ์ž‘์„ฑ๋œ ํ™ˆํŽ˜์ด์ง€๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.  

 

WebKit View

Displays embedded web content and enables content navigation.

ํฌํ•จ๋œ ์›น ์ฝ˜ํ…์ธ ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์ฝ˜ํ…์ธ  ํƒ์ƒ‰์„ ํ™œ์„ฑํ™”ํ•œ๋‹ค.

 

WebKit View์˜ ์•„์šธ๋ › ๋ณ€์ˆ˜ ์ƒ์„ฑ ์‹œ WKWebView ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•œ๋‹ค. WKEwbView๋Š” WebKit๋ฅผ import ํ•ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

WKWebView ํด๋ž˜์Šค

An object that displays interactive web content, such as for an in-app browser.

์ธ์•ฑ ๋ธŒ๋ผ์šฐ์ €์™€ ๊ฐ™์ด ๋Œ€ํ™”ํ˜•์˜ ์›น ์ฝ˜ํ…์ธ ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๊ฐ์ฒด

 

 


 

 

 

1.2 ํˆด๋ฐ” Toolbar

 

Toolbar

displays a horizontal bar of buttons
Provides a mechanism for displaying a toolbar at the bottom of the screen and supports the display and selection of toolbar items (instances of UIToolbarItem). You can use UIToolbarDelegate to specify custom views for toolbar items.

๋ฒ„ํŠผ์ด ์žˆ๋Š” ์ˆ˜ํ‰ ๋ฐ”.

ํ™”๋ฉด ์•„๋ž˜์— ํˆด๋ฐ”๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณตํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํˆด๋ฐ” ์•„์ดํ…œ์˜ ํ‘œ์‹œ์™€ ์„ ํƒ์„ ์ง€์›ํ•œ๋‹ค. UIToolbarDelegate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํˆด๋ฐ” ์•„์ดํ…œ์— ๋Œ€ํ•œ ์ปค์Šคํ…€ ๋ทฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์šฉ๋„

ํˆด๋ฐ”์—๋Š” ํˆด๋ฐ” ์•„์ดํ…œ์ด๋ผ๊ณ  ํ•˜๋Š”  ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋ฒ„ํŠผ์ด ์žˆ๋Š”๋ฐ, ์ด ๋ฒ„ํŠผ์€ ๋ณดํ†ต ํ™”๋ฉด์˜ ํ˜„์žฌ ๋‚ด์šฉ๊ณผ ๊ด€๋ จ๋œ ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

๋ฐ” ๋ฒ„ํŠผ ์•„์ดํ…œ์€ ํˆด๋ฐ”๋‚˜ ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋ฐ”์— ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•ด ํŠนํ™”๋œ ๋ฒ„ํŠผ์ด๋ฏ€๋กœ ๋ฒ„ํŠผ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

System Item ์†์„ฑ์„ ํ†ตํ•ด ๊ธฐ๋Šฅ์— ๋งž๊ฒŒ ์•„์ด์ฝ˜์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ) ์ •์ง€(Stop), ์ƒˆ๋กœ ๊ณ ์นจ(Refresh), ๋˜๊ฐ๊ธฐ(Rewind), ์•ž์œผ๋กœ ๊ฐ๊ธฐ(FastFoward)

 

 

ํ”Œ๋ ‰์„œ๋ธ” ์ŠคํŽ˜์ด์Šค ๋ฐ” ๋ฒ„ํŠผ ์•„์ดํ…œ  Flexible Space Bar Button Item

ํˆด๋ฐ”์˜ ๊ณต๊ฐ„์„ ๊ฐ™์€ ๊ฐ„๊ฒฉ์œผ๋กœ ๋‚˜๋ˆ  ๋ฐ” ๋ฒ„ํŠผ ์•„์ดํ…œ์„ ๋ฐฐ์น˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 Flexible   Item   Flexible   Item   Flexible   Item   Flexible   Item   Flexible 

 

 

 

 

 


 

 

1.4 ์•กํ‹ฐ๋น„ํ‹ฐ ์ธ๋””์ผ€์ดํ„ฐ ๋ทฐ

 

์•กํ‹ฐ๋น„ํ‹ฐ ์ธ๋””์ผ€์ดํ„ฐ ๋ทฐ Activity Indicator View

A view that shows that a task is in progress.

์ผ์˜ ์ง„ํ–‰ ์ •๋„๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ทฐ. ์ฆ‰ ๋กœ๋”ฉ์„ ํ‘œ์‹œํ•ด์ฃผ๋Š” ๋ทฐ

Hides When Stopped ์†์„ฑ์„ true๋กœ ํ•˜์—ฌ ๋™์ž‘์„ ๋ฉˆ์ถ”๋ฉด ์•กํ‹ฐ๋น„ํ‹ฐ ์ธ๋””์ผ€์ดํ„ฐ ๋ทฐ๊ฐ€ ๋ณด์ด์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.

 

 

 

 

 


2. ์•„์šธ๋ › ๋ณ€์ˆ˜์™€ ์•ก์…˜ ํ•จ์ˆ˜ ์ƒ์„ฑ

  • ์•„์šธ๋ › ๋ณ€์ˆ˜: ๊ฐ์ฒด์˜ ๊ฐ’์„ ์ด์šฉํ•˜๊ฑฐ๋‚˜ ์†์„ฑ์„ ์ œ์–ดํ•ด์•ผ ํ•  ๋•Œ ์ƒ์„ฑ. ์˜ˆ) ํ…์ŠคํŠธ ํ•„๋“œ์˜ text๋ฅผ ๋ฐ›์•„์˜ฌ ๋•Œ text ์†์„ฑ ์‚ฌ์šฉ
  • ์•ก์…˜ ํ•จ์ˆ˜: ๊ฐ์ฒด๊ฐ€ ์„ ํƒ๋˜์—ˆ์„ ๋•Œ ์–ด๋–ค ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์ƒ์„ฑ

 

import UIKit
import WebKit

class WebMissionViewController: UIViewController {
    @IBOutlet var txtUrl: UITextField! 
    @IBOutlet var myWebView: WKWebView!
    @IBOutlet var MyActivityIndicator: UIActivityIndicatorView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func btnGotoUrl(_ sender: UIButton) {
    }
    
    @IBAction func btnBarStop(_ sender: UIBarButtonItem) {
    }
    
    @IBAction func btnBarReload(_ sender: UIBarButtonItem) {
    }
    
    @IBAction func btnBarGoBack(_ sender: UIBarButtonItem) {
    }
    
    @IBAction func btnBarGoForward(_ sender: UIBarButtonItem) {
    }
}

 

 

 

 

 

 

 


3. ์•ฑ ์‹œ์ž‘ ํ™”๋ฉด ์ดˆ๊ธฐํ™” - HTML ํŒŒ์ผ๋กœ ์›น ํŽ˜์ด์ง€ ์ƒ์„ฑ

 

3.1 Info.plist ํŒŒ์ผ ์ˆ˜์ •ํ•˜๊ธฐ

Information property list์˜ ์•ฝ์ž์ด๋ฉฐ, ํ‚ค-๊ฐ’ ์Œ์˜ ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๋‹ค. ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ๋“ค๊ณผ ๊ด€๋ จ๋œ ๋ฆฌ์†Œ์Šค ์„ค์ •์ด ๋“ค์–ด์žˆ๋‹ค.

์›น ๋ทฐ์— ์›น ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋˜๊ฒŒ ํ•˜๋ ค๋ฉด ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

 

  1. Information Property List์— App Transport Security Settings์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
  2. App Transport Security Settings์— AllowArbitrary Loads(์ž„์˜ ๋กœ๋“œ ํ—ˆ์šฉ)๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
  3. AllowArbitrary Loads์˜ ๊ฐ’์„ Yes๋กœ ๋ฐ”๊พผ๋‹ค. ์ฆ‰, ์ž„์˜๋กœ ์›น ํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

 

 


 

 

 

3.2 loadWebPage ํ•จ์ˆ˜ ๊ตฌํ˜„

:์›น ๋ทฐ์— ์›นํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ ์ฃผ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜

 

//์›น ๋ทฐ์— ์›น ํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
    func loadWebPage(_ url: String) {
        let myUrl = URL(string: url)
        let myRequest = URLRequest(url: myUrl!)
        myWebView.load(myRequest)
    }

 

URL

A value that identifies the location of a resource, such as an item on a remote server or the path to a local file

๋กœ์ปฌ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ ๋˜๋Š” ์›๊ฒฉ ์„œ๋ฒ„์— ๋Œ€ํ•œ ์•„์ดํ…œ๊ฐ™์ด ๋ฆฌ์†Œ์Šค์˜ ์œ„์น˜๋ฅผ ์‹๋ณ„ํ•˜๋Š” ๊ฐ’

 

 

 

URLRequest

A value that identifies the location of a resource, such as an item on a remote server or the path to a local file

ํ”„๋กœํ† ์ฝœ ๋˜๋Š” URL scheme์˜ ๋…๋ฆฝ์ ์ธ URL ๋กœ๋“œ ์š”์ฒญ

 

 

 

WKWebView.load(_ request: URLRequest)

Loads the web content referenced by the specified URL request object and navigates to it.

์ง€์ •๋œ URL ์š”์ฒญ ๊ฐ์ฒด์—์„œ ์ฐธ์กฐ๋œ ์›น ์ฝ˜ํ…์ธ ๋ฅผ ๋กœ๋“œํ•˜๊ณ , ํ•ด๋‹น ๊ฐ์ฒด๋กœ ์ด๋™ํ•œ๋‹ค.

 

 


 

 

3.3 HTML ํŒŒ์ผ ์ƒ์„ฑ

HTML ๋ฌธ๋ฒ•์— ๋งž๊ฒŒ ์ž‘์„ฑ๋œ ํŒŒ์ผ์„ ์›น ๋ทฐ์— ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

  1. File/New/File... ์„ ํƒ
  2. Other์˜ Empty ์„ ํƒ
  3. ํŒŒ์ผ๋ช….html ์ƒ์„ฑ
  4. ์ƒ์„ฑ๋œ ํŒŒ์ผ์— HTML ๋ฌธ๋ฒ•์— ๋งž๋„๋ก ์ฝ”๋“œ ์ž‘์„ฑ
<html>
    <head>
        <meta charset = "utf-8">
    </head>
    <body>
        <h1> HTML ํŒŒ์ผ </h1>
        html ํŒŒ์ผ์„ ์ด์šฉํ•œ ์›น ํŽ˜์ด์ง€<br>
        <p>๋‚˜์˜ ๋ธ”๋กœ๊ทธ <a href="https://88yhtserof.tistory.com">88yhtserof</a>๋กœ ์ด๋™</p>
    </body>
</html>

 

 


 

 

 

3.4 ์›น ๋ทฐ์— HTML ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•œ ์›น ํŽ˜์ด์ง€ ๋œจ๊ฒŒ ํ•˜๊ธฐ

override func viewDidLoad() {
        super.viewDidLoad()
        
        //์ดˆ๊ธฐ ํ™”๋ฉด ์„ค์ •
        let filePath = Bundle.main.path(forResource: "htmlView", ofType: "html")
        let fileUrl = URL(fileURLWithPath: filePath!)
        let fileRequest = URLRequest(url: fileUrl)
        myWebView.load(fileRequest)
    }

 

 

 

 

 

 

 

 

 

 

 


4. URL ์ž…๋ ฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„

ํ…์ŠคํŠธ ํ•„๋“œ์— ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  'Go' ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์›น ๋ทฐ์— ํ•ด๋‹น ์ฃผ์†Œ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋œจ๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค.

์ ‘๋‘์‚ฌ๋กœ ํ”„๋กœํ† ์ฝœ, ์ฆ‰ 'https://'๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์—†์œผ๋ฉด ๋ถ™์—ฌ์„œ ์›น ํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•œ๋‹ค.

 

//์ฃผ์†Œ์˜ ํ”„๋กœํ† ์ฝœ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
    func checkUrl(_ url: String) -> String {
        var strUrl:String = url
        let flag = strUrl.hasPrefix("https://")
        
        if !flag {
            strUrl = "https://" + strUrl
        }
        return strUrl
    }
    
    @IBAction func btnGotoUrl(_ sender: UIButton) {
        let myUrl = checkUrl(txtUrl.text!)
        txtUrl.text = ""
        
        loadWebPage(myUrl)
    }

 

 

 

 

 

 

 


5. ํˆด๋ฐ” ์•„์ดํ…œ ๋ฒ„ํŠผ ๊ตฌํ˜„ํ•˜๊ธฐ - ์ •์ง€, ์žฌ๋กœ๋”ฉ, ์ด์ „ ํŽ˜์ด์ง€, ๋‹ค์Œ ํŽ˜์ด์ง€

	@IBAction func btnBarStop(_ sender: UIBarButtonItem) {
        myWebView.stopLoading()
    }
    
    @IBAction func btnBarReload(_ sender: UIBarButtonItem) {
        myWebView.reload()
    }
    
    @IBAction func btnBarGoBack(_ sender: UIBarButtonItem) {
        myWebView.goBack()
    }
    
    @IBAction func btnBarGoForward(_ sender: UIBarButtonItem) {
        myWebView.goForward()
    }

 

๋ฉ”์„œ๋“œ ๋ช… ์„ค๋ช…
stopLoading() Stops loading all resources on the current page.
ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค ๋กœ๋”ฉ์„ ์ค‘๋‹จํ•œ๋‹ค.
reload() Reloads the current webpage.
ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ์žฌ๋กœ๋”ฉํ•œ๋‹ค. (์›น ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ)
goBack() Navigates to the back item in the back-forward list.
์•ž, ๋’ค ๋ชฉ๋ก ์ค‘ ๋’ค ํ•ญ๋ชฉ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.(์ด์ „ ์›น ํŽ˜์ด์ง€๋กœ ์ด๋™)
goForward() Navigates to the forward item in the back-forward list.
์•ž, ๋’ค ๋ชฉ๋ก ์ค‘ ์•ž ํ•ญ๋ชฉ์œผ๋กœ ์ด๋™ํ•œ๋‹ค. (๋‹ค์Œ ์›น ํŽ˜์ด์ง€๋กœ ์ด๋™)

 

 

 

 

 

 

 

 

 

 

 


6. ์•กํ‹ฐ๋น„ํ‹ฐ ์ธ๋””์ผ€์ดํ„ฐ ๊ตฌํ˜„

์•กํ‹ฐ๋น„ํ‹ฐ ์ธ๋””์ผ€์ดํ„ฐ Activity Indicator

: ์•ฑ์ด ๋™์ž‘ ์ค‘์ž„์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ ์ค€๋‹ค. ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋กœ๋”ฉ, ์™„๋ฃŒ, ์‹คํŒจ์— ๋Œ€ํ•œ ๋™์ž‘์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ž€?

https://88yhtserof.tistory.com/23

 

[ IOS ] ํ”ผ์ปค ๋ทฐ ๋งŒ๋“ค๊ธฐ ( ํ”„๋กœํ† ์ฝœ, ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ )

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‘ ๊ฐœ์ธ ํ”ผ์ปค ๋ทฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. ์™ผ์ชฝ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ ํŒŒ์ผ ๋ช…์ด ๋‚˜ํƒ€๋‚˜๋„๋ก, ์˜ค๋ฅธ์ชฝ ํ”ผ์ปค ๋ทฐ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ ์ด๋ฏธ์ง€ ๋ทฐ์— ์ด๋ฏธ์ง€๋ฅผ ์ถœ

88yhtserof.tistory.com

 

 

 

 

WKNavigationDelegate ํ”„๋กœํ† ์ฝœ

Methods for accepting or rejecting navigation changes, and for tracking the progress of navigation requests.
Implement the methods of the WKNavigationDelegate protocol in the object you use to coordinate changes in your web view’s main frame
For example, you might use these methods to restrict navigation from specific links within your content. You might also use them to track the progress of requests, and to respond to errors and authentication challenges.

์ด๋™ ๋ณ€ํ™”๋ฅผ ์ˆ˜๋ฝ ๋˜๋Š” ๊ฑฐ๋ถ€ํ•˜๊ณ , ์ด๋™ ์š”์ฒญ์˜ ์ง„ํ–‰ ์‚ฌํ•ญ์„ ์ถ”์ ํ•˜๋Š” ๋ฉ”์„œ๋“œ

์›น ๋ทฐ์˜ ๋ฉ”์ธ ํ”„๋ ˆ์ž„์˜ ๋ณ€ํ™”๋ฅผ ์กฐ์ •ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์— WKNavigationDelegate ํ”„๋กœํ† ์ฝœ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ง€์ •๋œ ๋งํฌ๋กœ ์ด๋™์„ ์ œํ•œํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋˜ ์š”์ฒญ์˜ ์ง„ํ–‰ ์ƒํ•ญ์„ ์ถ”์ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ , ์˜ค๋ฅ˜ ๋ฐ ์ธ์ฆ ๋ฌธ์ œ์— ์‘๋‹ตํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

 

didCommit๋ฅผ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ

Tells the delegate that the web view has started to receive content for the main frame.

์›น ๋ทฐ๊ฐ€ ๋ฉ”์ธ ํ”„๋ ˆ์ž„์— ๋Œ€ํ•œ ์ฝ˜ํ…์ธ ๋ฅผ ์ˆ˜์‹ ํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์—๊ฒŒ ์•Œ๋ฆฐ๋‹ค.

๋กœ๋”ฉ ์ค‘์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ

 

//didCommit์„ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ๋ฉ”์„œ๋“œ
    //๋กœ๋”ฉ ์ค‘์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        MyActivityIndicator.startAnimating()
        MyActivityIndicator.isHidden = false
    }
navigation The navigation object that uniquely identifies the load request.
๋กœ๋“œ ์š”์ฒญ์„ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๋Š” ํƒ์ƒ‰ ๊ฐ์ฒด

 

 

 

 

didFinish๋ฅผ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ

Tells the delegate that navigation is complete.

ํƒ์ƒ‰์ด ์™„๋ฃŒ๋˜์—ˆ์Œ์„ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์—๊ฒŒ ์•Œ๋ฆฐ๋‹ค.

๋กœ๋”ฉ์ด ์™„๋ฃŒ๋˜์—ˆ์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ

 

//didFinish๋ฅผ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ๋ฉ”์„œ๋“œ
    //๋กœ๋”ฉ์ด ๋๋‚ฌ์Œ์„ ์•Œ๋ฆฌ๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        MyActivityIndicator.stopAnimating()
        MyActivityIndicator.isHidden = true
    }
navigation The navigation object that finished.
์™„๋ฃŒ๋œ ํƒ์ƒ‰ ๊ฐ์ฒด

 

 

 

didFail๊ณผ withError์„ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ

Tells the delegate that an error occurred during navigation.

ํƒ์ƒ‰ํ•˜๋Š” ๋™์•ˆ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ์—๊ฒŒ ์•Œ๋ฆฐ๋‹ค.

๋กœ๋”ฉ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ

 

    //didFail๊ณผ withError์„ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    //๋กœ๋”ฉ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Œ์„ ์•Œ๋ฆฌ๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ๋ฉ”์„œ๋“œ
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        MyActivityIndicator.stopAnimating()
        MyActivityIndicator.isHidden = true
    }

 

navigation The navigation object for the operation. This object corresponds to a WKNavigationobject that WebKit returned when the load operation began. You use it to track the progress of that operation. 
์ž‘์—…์— ๋Œ€ํ•œ ํƒ์ƒ‰ ๊ฐ์ฒด. ์ด ๊ฐ์ฒด๋Š” WebKit์ด ๋กœ๋”ฉ ์ž‘์—…์„ ์‹œ์ž‘ํ•  ๋•Œ ๋ฐ˜ํ™˜ํ–ˆ๋˜ WKNavigationobject์— ํ•ด๋‹นํ•œ๋‹ค. ์ž‘์—…์˜ ์ง„ํ–‰์ƒํ™ฉ์„ ์ถ”์ ํ•  ๋•Œ ์ด ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
error The error that occurred.
๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜

 

 

 

 

 

 

 

 

 

Do It! ์Šค์œ„ํ”„ํŠธ๋กœ ์•„์ดํฐ ์•ฑ ๋งŒ๋“ค๊ธฐ ๊ฐœ์ • 5ํŒ
SWIFT ๋ฌธ์„œ

 

 

 

728x90