LoginSignup
8
7

More than 5 years have passed since last update.

【Swift】WKWebViewでCookieを全削除する方法

Posted at

背景

iPhoneでデモアプリを作成している際に、Cookieをすべて削除する必要が出てきました。

let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in storage.cookies!{
    storage.deleteCookie(cookie)
}

こんな感じのことも試したんですが、そもそもCookie作ってるのに、存在していない模様。いろいろ調べたんですがObjective-Cの記事ばかりだったので、それを参考にして解決しました。

解決方法

let libraryPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
let cookiesFolderPath = libraryPath.stringByAppendingString("/Cookies")
do {
   try NSFileManager.defaultManager().removeItemAtPath(cookiesFolderPath)
}catch {
   // error handling
}

Cookieはシミュレータ上では、Mac上のディレクトリに保存されているようです。ですので、そのディレクトリのパスを取得し、その中身をすべて消すという方でとりあえず解決しました。

実機でやったらどうなるんでしょうか、やってみた方がいらっしゃればコメントいただけると幸いです。

8
7
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
8
7