1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIWebViewでユーザー選択の無効化

Last updated at Posted at 2018-07-11

ユーザー選択(↓これ)を無効にしないといけない作業がありました。
条件は「UIWebViewを利用しなければならない」というもの

mihon.png

##方法1: UIWebViewにUserInteractionEnabledをfalseに設定

let webview = UIWebView(frame: view.frame)
webview.isUserInteractionEnabled = false

##方法2: JSで禁止する(検索するとよく出てくる方法)

let webview = UIWebView(frame: view.frame)
webview.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitUserSelect='none'")
webview.stringByEvaluatingJavaScript(from: "document.documentElement.style.webkitTouchCallout='none'")

##方法3: canPerformActionをオーバーライド

UIWebViewを継承したクラスを作成し、canPerformActionをオーバーライド。
ユーザー選択全て無効にするにはfalseを戻り値にする。(有効にしたいユーザー選択があればその場合だけtrueを返してやる)

class MyWebView: UIWebView {
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?