{"id":1847,"date":"2016-04-15T23:48:35","date_gmt":"2016-04-15T14:48:35","guid":{"rendered":"https:\/\/ecpplus.net\/weblog\/?p=1847"},"modified":"2020-06-26T01:34:48","modified_gmt":"2020-06-25T16:34:48","slug":"wkwebview%e3%81%a7networkactivityindicatorvisible%e3%82%92%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/ecpplus.net\/weblog\/wkwebview%e3%81%a7networkactivityindicatorvisible%e3%82%92%e8%a1%a8%e7%a4%ba\/","title":{"rendered":"WKWebView\u3067networkActivityIndicatorVisible\u3092\u8868\u793a"},"content":{"rendered":"<p>networkActivityIndicatorVisible (iPhone\u306e\u4e0a\u90e8\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u30d0\u30fc\u3067\u8aad\u8fbc\u4e2d\u306b\u3050\u308b\u3050\u308b\u3055\u305b\u308b\u3084\u3064) \u3092\u8868\u793a\u3059\u308b\u65b9\u6cd5\u3002UIWebView \u306f\u3001html,js,css,\u753b\u50cf\u306a\u3069\u306e\u533a\u5225\u304c\u3064\u304b\u306a\u304f\u3066\u7d50\u69cb\u5927\u5909\u3060\u3063\u305f\u3051\u3069\u3001\u305d\u3046\u3044\u3046\u306e\u6c17\u306b\u305b\u305a\u3060\u3044\u3076\u697d\u306b\u5b9f\u88c5\u3067\u304d\u308b\u3002<\/p>\n<p><!--more--><\/p>\n<h1>Controller \u306b\u76f4\u63a5\u66f8\u304f\u30d1\u30bf\u30fc\u30f3<\/h1>\n<h2>KVO\u3092\u76e3\u8996\u3059\u308b<\/h2>\n<p>Controller \u306b\u76f4\u63a5\u66f8\u304f\u5834\u5408\u306f\u3001viewDidLoad \u3068\u304b\u3067\u3001addObserver \u3059\u308b\u3002<\/p>\n<pre><code class=\"language-swift \">var webView : WKWebView!\n<\/code><\/pre>\n<p>\u7684\u306a\u30d7\u30ed\u30d1\u30c6\u30a3\u304c\u8a2d\u5b9a\u3057\u3066\u3042\u308b\u3068\u3044\u3046\u524d\u63d0\u3067<\/p>\n<pre><code class=\"language-swift \">webView.addObserver(self, forKeyPath: \"loading\", options: NSKeyValueObservingOptions.New, context: nil)\n<\/code><\/pre>\n<p>\u3068\u3059\u308b\u3002<\/p>\n<p>controller \u304c\u306a\u304f\u306a\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u3067<\/p>\n<pre><code class=\"language-swift \">webView.removeObserver(self, forKeyPath: \"loading\")\n<\/code><\/pre>\n<p>\u3092\u547c\u3076\u306e\u3092\u5fd8\u308c\u305a\u306b\u3002<\/p>\n<h2>KVO\u306e\u5909\u5316\u306b\u30d5\u30c3\u30af\u3055\u305b\u308b<\/h2>\n<pre><code class=\"language-swift \">override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer&lt;Void&gt;) {\n    if keyPath == \"loading\" {\n        UIApplication.sharedApplication().networkActivityIndicatorVisible = webView.loading\n    }\n}\n<\/code><\/pre>\n<p>loading\u72b6\u614b\u304c\u5909\u308f\u308b\u3068\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3070\u308c\u308b\u306e\u3067\u3001\u305d\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067 <code>webView.loading (BOOL)<\/code> \u304c\u3069\u3046\u306a\u3063\u3066\u3044\u308b\u306e\u304b\u3092\u898b\u308b\u3060\u3051\u3067\u826f\u3044\u3002<\/p>\n<h1>WKWebView \u306e\u30b5\u30d6\u30af\u30e9\u30b9\u3092\u4f5c\u308b\u30d1\u30bf\u30fc\u30f3<\/h1>\n<p>WKWebView \u3092\u4f7f\u3063\u3066\u3001\u7279\u306b\u4f55\u3082\u305b\u305a networkActivityIndicatorVisible \u3060\u3051\u3092\u5909\u3048\u305f\u3044\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30b5\u30d6\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066 WKWebView \u3092\u547c\u3093\u3067\u308b\u3068\u3053\u308d\u306b\u7f6e\u304d\u63db\u3048\u308c\u3070\u826f\u3044\u3002<\/p>\n<pre><code class=\"language-swift \">import UIKit\nimport WebKit\n\nclass CustomWebView: WKWebView {\n    init() {\n        super.init(frame: CGRectZero, configuration: WKWebViewConfiguration())\n        self.translatesAutoresizingMaskIntoConstraints = false\n\n        addObserver(self, forKeyPath: \"loading\", options: NSKeyValueObservingOptions.New, context: nil)\n    }\n\n    deinit {\n        removeObserver(self, forKeyPath: \"loading\")\n    }\n\n    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer&lt;Void&gt;) {\n        if keyPath == \"loading\" {\n            UIApplication.sharedApplication().networkActivityIndicatorVisible = loading\n        }\n    }\n}\n<\/code><\/pre>\n<h2>\u4f7f\u3044\u65b9<\/h2>\n<p>\u5168\u753b\u9762\u8868\u793a\u3067\u3001\u7279\u5b9a\u306eURL\u3092\u8aad\u307f\u8fbc\u3080\u3068\u3044\u3046\u3082\u306e\u3002<\/p>\n<pre><code class=\"language-swift \">let webView = CustomWebView()\n\nview.addSubview(webView)\nview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(\"V:|[webView]|\", options: NSLayoutFormatOptions(), metrics: nil, views: [\"webView\": webView]))\nview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(\"H:|[webView]|\", options: NSLayoutFormatOptions(), metrics: nil, views: [\"webView\": webView]))\n\nwebView.loadRequest(NSURL(string : \"https:\/\/memo.ecp.plus\/\"))\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>networkActivityIndicatorVisible (iPhone\u306e\u4e0a\u90e8\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u30d0\u30fc\u3067\u8aad\u8fbc\u4e2d\u306b\u3050\u308b\u3050\u308b\u3055\u305b\u308b\u3084\u3064) \u3092\u8868\u793a\u3059\u308b\u65b9\u6cd5\u3002UIWebView \u306f\u3001html,js,css,\u753b\u50cf\u306a\u3069\u306e\u533a\u5225\u304c\u3064\u304b\u306a\u304f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[35],"class_list":["post-1847","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-ios"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/comments?post=1847"}],"version-history":[{"count":2,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1847\/revisions"}],"predecessor-version":[{"id":2858,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1847\/revisions\/2858"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media\/1483"}],"wp:attachment":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media?parent=1847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/categories?post=1847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/tags?post=1847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}