๊ณต๋ถ€ ์—ฐ์Šต์žฅ :-)

TIL_20180625 ~ 20180629

|

์›น๋ทฐ์•ฑ ๋ฏธ์…˜๊ณผ WKWebKit์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๊ณ  ๊ณต๋ถ€ํ•œ ์ผ์ฃผ์ผ..์›น์„ ๋ชจ๋ฅด๋Š” ์ƒํƒœ์—์„œ WK๋ญ”๊ฐ€ ๋‚ ์งœ๋ณ„๋กœ ๋‚˜๋ˆ ์„œ ์ •๋ฆฌํ•ด์„œ ๋‚จ๊ฒจ๋†“์€๊ฒŒ ์—†์–ด์„œ ๊ทธ๋ƒฅ ์ผ์ฃผ์ผ๋™์•ˆ ๊ณต๋ถ€ํ•œ ํ”์ ์œผ๋กœ TIL๋Œ€์ฒด.. :(

๋ Œ๋”๋ง: ์š”์ฒญ๋ฐ›์€ ์ผ์„ ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ํ‘œ์‹œ

func webView(webView: WKWebView!, decidePolicyForNavigationAction navigationAction: WKNavigationAction!, decisionHandler: ((WKNavigationActionPolicy) -> Void)!)

  • The above is a WKNavigationDelegate protocol method that gets called several times during the page load.
  • One of its parameters is an WKNavigationAction object which contains information that can help you decide whether to continue page load or not.
    • WKNavigationAction๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›น๋ทฐ๋ฅผ ๊ณ„์† ๋กœ๋“œํ• ์ง€ ๋ง์ง€ ๊ฒฐ์ •ํ•˜๋ฉฐ, ์ด๋ฅผ ์œ„ํ•ด์„œ WKNavigationAction์˜ ์•„๋ž˜ ์†์„ฑ๋“ค์„ ์ด์šฉํ•œ๋‹ค.
    • request
    • sourceFrame
    • targetFrame
  • WKNavigationActionPolicy
    • .cancel
    • .allow: the page loads as usual and renders in the web view

    func safariViewController(SFSafariViewController, didCompleteInitialLoad: Bool) > ์ด๊ฒŒ ํ˜ธ์ถœ๋˜๋ฉด ์ปดํ”Œ๋ฆฌ์…˜ํ•ธ๋“ค๋Ÿฌ ํ˜ธ์ถœ๋˜๋„๋ก..?

    func safariViewControllerDidFinish(SFSafariViewController) Tells the delegate that the user dismissed the view. > ์‚ฌํŒŒ๋ฆฌ ๋ทฐ์ปจ ๋„๊ธฐ

ํ™”๋ฉด์ด ๋‹ค ๋œฐ๋•Œ๊นŒ์ง€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” postMessage๋ฅผ ํ•œ๋‹ค. load๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ๋กœ๋”ฉ์ด ์ง„ํ–‰๋˜๋Š” ์ค‘์— navigation์ด๋‚˜ scriptmessageHandler๋Š” ํ™œ์„ฑํ™” ๋˜์„œ ๊ณ„์† ๊ทธ ๋™์ž‘์ด ์žˆ๋Š”์ง€ ์ฐพ์Œ ๋กœ๋”ฉ์ด ์ง„ํ–‰๋˜๋Š” ์ค‘์— js๊ฐ€ ๊ณ„์† ๋ฉ”์‹œ์ง€๋ฅผ postํ•˜๋Š”๊ฑฐ๊ฐ™์Œ ์™„์ „ํžˆ ๋กœ๋“œ๋˜๋ฉด postMessage๋ฅผ ํ•˜์ง€ ์•Š์Œ

์ปดํ”Œ๋ฆฌ์…˜ ํ•ธ๋“ค๋Ÿฌ ๋‘๋ฒˆํ˜ธ์ถœ๋˜๋ฉด ์—๋Ÿฌ๋‚จ navigationType์˜ allow์™€ cancel

URL: https://m.baeminchan.com/
search failed!
URL: about:blank
search failed!
URL: https://m.baeminchan.com/search.php
search success!
2018-06-25 14:39:09.808320+0900 WebViewApp[94065:10947861] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[WebViewApp.ViewController webView:decidePolicyForNavigationAction:decisionHandler:] was called more than once'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000111bd11e6 __exceptionPreprocess + 294
	1   libobjc.A.dylib                     0x000000010d6d5031 objc_exception_throw + 48
	2   CoreFoundation                      0x0000000111c46975 +[NSException raise:format:] + 197
	3   WebKit                              0x000000011014d897 _ZNK6WebKit28CompletionHandlerCallChecker30completionHandlerHasBeenCalledEv + 203
	4   WebKit                              0x000000011018b462 _ZZN6WebKit15NavigationState16NavigationClient31decidePolicyForNavigationActionERNS_12WebPageProxyEON3WTF3RefIN3API16NavigationActionENS4_13DumbPtrTraitsIS7_EEEEONS5_INS_27WebFramePolicyListenerProxyENS8_ISC_EEEEPNS6_6ObjectEEN3$_1clE24WKNavigationActionPolicyP18_WKWebsitePolicies + 36
	5   WebViewApp                          0x000000010cdc8429 _T0SC24WKNavigationActionPolicyOIeyBy_ABIegy_TR + 25
	6   WebViewApp                          0x000000010cdc849d _T0SC24WKNavigationActionPolicyOIeyBy_ABIegy_TRTA + 13
	7   WebViewApp                          0x000000010cdc8303 _T010WebViewApp0B10ControllerC03webB0ySo05WKWebB0C_So18WKNavigationActionC15decidePolicyForySC0ghJ0Oc15decisionHandlertF + 1795
	8   WebViewApp                          0x000000010cdc83fc _T010WebViewApp0B10ControllerC03webB0ySo05WKWebB0C_So18WKNavigationActionC15decidePolicyForySC0ghJ0Oc15decisionHandlertFTo + 156
	9   WebKit                              0x000000011018886a _ZN6WebKit15NavigationState16NavigationClient31decidePolicyForNavigationActionERNS_12WebPageProxyEON3WTF3RefIN3API16NavigationActionENS4_13DumbPtrTraitsIS7_EEEEONS5_INS_27WebFramePolicyListenerProxyENS8_ISC_EEEEPNS6_6ObjectE + 752
	10  WebKit                              0x000000011033fec0 _ZN6WebKit12WebPageProxy31decidePolicyForNavigationActionEyRKN7WebCore18SecurityOriginDataEyONS_20NavigationActionDataERKNS_13FrameInfoDataEyRKNS1_15ResourceRequestEOSA_yRKNS_8UserDataERbRyRNS1_12PolicyActionERNS_10DownloadIDERSt8optionalINS_19WebsitePoliciesDataEE + 1286
	11  WebKit                              0x000000011037076a _ZN3IPC22callMemberFunctionImplIN6WebKit12WebPageProxyEMS2_FvyRKN7WebCore18SecurityOriginDataEyONS1_20NavigationActionDataERKNS1_13FrameInfoDataEyRKNS3_15ResourceRequestEOSC_yRKNS1_8UserDataERbRyRNS3_12PolicyActionERNS1_10DownloadIDERSt8optionalINS1_19WebsitePoliciesDataEEENSt3__15tupleIJyS4_yS7_S9_ySC_SC_ySG_EEEJLm0ELm1ELm2ELm3ELm4ELm5ELm6ELm7ELm8ELm9EENSW_IJbySL_SN_SR_EEEJLm0ELm1ELm2ELm3ELm4EEEEvPT_T0_OT1_RT3_NSV_16integer_sequenceImJXspT2_EEEENS16_ImJXspT4_EEEE + 139
	12  WebKit                              0x0000000110365da2 _ZN3IPC13handleMessageIN8Messages12WebPageProxy31DecidePolicyForNavigationActionEN6WebKit12WebPageProxyEMS5_FvyRKN7WebCore18SecurityOriginDataEyONS4_20NavigationActionDataERKNS4_13FrameInfoDataEyRKNS6_15ResourceRequestEOSF_yRKNS4_8UserDataERbRyRNS6_12PolicyActionERNS4_10DownloadIDERSt8optionalINS4_19WebsitePoliciesDataEEEEEvRNS_7DecoderERNS_7EncoderEPT0_T1_ + 322
	13  WebKit                              0x0000000110185d7d _ZN3IPC18MessageReceiverMap19dispatchSyncMessageERNS_10ConnectionERNS_7DecoderERNSt3__110unique_ptrINS_7EncoderENS5_14default_deleteIS7_EEEE + 141
	14  WebKit                              0x00000001103bc4d2 _ZN6WebKit15WebProcessProxy21didReceiveSyncMessageERN3IPC10ConnectionERNS1_7DecoderERNSt3__110unique_ptrINS1_7EncoderENS6_14default_deleteIS8_EEEE + 28
	15  WebKit                              0x0000000110150343 _ZN3IPC10Connection19dispatchSyncMessageERNS_7DecoderE + 209
	16  WebKit                              0x000000011014dcba _ZN3IPC10Connection15dispatchMessageENSt3__110unique_ptrINS_7DecoderENS1_14default_deleteIS3_EEEE + 104
	17  WebKit                              0x000000011015056b _ZN3IPC10Connection18dispatchOneMessageEv + 177
	18  JavaScriptCore                      0x000000011a9b33ac _ZN3WTF7RunLoop11performWorkEv + 236
	19  JavaScriptCore                      0x000000011a9b3652 _ZN3WTF7RunLoop11performWorkEPv + 34
	20  CoreFoundation                      0x0000000111b73bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	21  CoreFoundation                      0x0000000111b584af __CFRunLoopDoSources0 + 271
	22  CoreFoundation                      0x0000000111b57a6f __CFRunLoopRun + 1263
	23  CoreFoundation                      0x0000000111b5730b CFRunLoopRunSpecific + 635
	24  GraphicsServices                    0x00000001144c8a73 GSEventRunModal + 62
	25  UIKit                               0x000000010df71057 UIApplicationMain + 159
	26  WebViewApp                          0x000000010cdc9ec7 main + 55
	27  libdyld.dylib                       0x0000000112db2955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

์›น๋ทฐ ๋งˆ์ง€๋ง‰ ๋งˆ๋ฌด๋ฆฌ ์•ฑ ๋‚ด์—์„œ ์›น๋ทฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์›น์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ , ๋™์‹œ์— ์›น์˜ ๋™์ž‘์„ ํ†ต์ œํ•  ์ˆ˜๋„ ์žˆ์Œ WKScriptMessageHandler- ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์›นํŽ˜์ด์ง€๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ๋ณด๋‚ด๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋™์ž‘์„ ํ•˜๋Š” ํ”„๋กœํ† ์ฝœ WKUserContentController- ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์›น๋ทฐ์— ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•˜๊ณ  user script๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ WKNavigationDelegate- ์›น์˜ ํƒ์ƒ‰ ๊ณผ์ •์ค‘ ํŠน์ •ํ•œ ํƒ€์ด๋ฐ์— ๋งž๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ์‹œํ‚ด. ์ด์— ๋งž์ถฐ์„œ ์ ์ ˆํ•œ custom๋™์ž‘์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. SFSafariViewController- ์•ฑ ๋‚ด์—์„œ ์›น์„ ์‚ฌํŒŒ๋ฆฌ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋„์šฐ๊ณ  ๊ด€๋ฆฌํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‚ฌํŒŒ๋ฆฌ์˜ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•˜๊ณ ์žˆ๋‹ค.

์ฐธ๊ณ ํ–ˆ๋˜ ๋งํฌ๋“ค : https://www.appcoda.com/webkit-framework-tutorial/

Comments