Clear iPhone simulator cookies
Asked Answered
C

7

7

I have an app that makes request to a REST service. Authentication is done using cookies. This already works.

What I have problems with is to test the case when the cookie is no longer valid and my code has to reauthenticate. To test this I have to wait until the cookie is invalid, which could take some time. To accelerate this I figured that if I delete the cookie it would have the same effect.

How to delete all cookies of an app on the iPhone simulator?

I already tried the following:

Deleting <app-dir>/Library/Cookies/Cookies.binarycookies doesn't work. It seems that my cookies are never written to this file.

Deleting all cookies in NSHTTPCookieStorage on app startup doesn't work either.

Cyrano answered 13/12, 2010 at 8:57 Comment(1)
Is there some reason resetting the simulator won't work for you?Stringer
A
9

The cookies are located at:

/Users/<YourUsername>/Library/Application Support/iPhone Simulator/<iOSversion>/Library/Cookies/Cookies.binarycookies

You may need to quit out of Safari (in the fast app switching area) and then delete them so Safari won't have them in memory.

Alright answered 13/12, 2010 at 9:14 Comment(8)
That seemed to work only for SDKs prior 4.x. I have that file but it wasn't updated since spring.Cyrano
Deleting Cookies/Cookies.binarycookies definitely works for me, se above.Alright
Thx Matthew, you got me a step further. Your directories only work for Safari in my own app these dirs are .../4.2/Applications/<app hash>/Library/Cookies/. I deleted the .dat files that I didn't see before I reseted my simulator. After that my app doesn't save cookies between app starts anymore. Kind of what I need for testing. But it is definitely a strange behavior.Cyrano
Ah, interesting, and definitely strange. Glad you found a solution, anyway.Alright
The cookie file can be found at: > /Users/<username>/Library/Application\ Support/iPhone\ Simulator/4.2/Applications/<app_id>/Library/Cookies/Cookies.binarycookiesOrelle
@Balint Right, that's what my answer says.Alright
There is no iPhone Simulator folder in Application Support. Where else could it be?Ploughboy
@modiX This answer was from 2010. A lot has changed since then. Resetting the simulator is likely your best bet. If you need to delete single cookies while keeping other things in place, you should be able to find them in Library/Developer/CoreSimulator/Devices/DEVICEIDENTIFIERHERE/data/Library/Cookies. Replace "DEVICEIDENTIFIERHERE" with the 37-character UUID directory name for that device. You'll be able to spot the right one if you just sort by "Date Modified" in the Finder.Alright
C
22

YOU CAN RESET THE SIMULATOR

  • Launch the simulator.
  • Click the FIRST item on the "menu bar". It says "iOS Simulator"
  • A menu will appear. Go down three items to "Reset Contents and Settings"
  • Click "Reset" on the dialog which appears

iPhone Simulator Reset Cookies

Cuffs answered 13/12, 2010 at 9:12 Comment(4)
Resetting it is indeed likely the easiest route unless you don't want to reset everything else in the simulator.Alright
That is not an option, I need to have the rest of my content not deleted. Though I could copy it back after reset.Cyrano
No I have various files (e.g. sqlite dbs) that save user data which are necessary for my tests.Cyrano
This answer seems to be out of date. See #2764233Ossie
A
14

You'll probably find better luck doing this in the SDK code rather than modifying file systems. Try:

    //Delete previous cookies
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *each in [[[cookieStorage cookiesForURL:YOUR_URL] copy] autorelease]) {
        [cookieStorage deleteCookie:each];
    }
Aruwimi answered 13/12, 2010 at 13:29 Comment(1)
Works like charm. You save my day!!Booster
A
9

The cookies are located at:

/Users/<YourUsername>/Library/Application Support/iPhone Simulator/<iOSversion>/Library/Cookies/Cookies.binarycookies

You may need to quit out of Safari (in the fast app switching area) and then delete them so Safari won't have them in memory.

Alright answered 13/12, 2010 at 9:14 Comment(8)
That seemed to work only for SDKs prior 4.x. I have that file but it wasn't updated since spring.Cyrano
Deleting Cookies/Cookies.binarycookies definitely works for me, se above.Alright
Thx Matthew, you got me a step further. Your directories only work for Safari in my own app these dirs are .../4.2/Applications/<app hash>/Library/Cookies/. I deleted the .dat files that I didn't see before I reseted my simulator. After that my app doesn't save cookies between app starts anymore. Kind of what I need for testing. But it is definitely a strange behavior.Cyrano
Ah, interesting, and definitely strange. Glad you found a solution, anyway.Alright
The cookie file can be found at: > /Users/<username>/Library/Application\ Support/iPhone\ Simulator/4.2/Applications/<app_id>/Library/Cookies/Cookies.binarycookiesOrelle
@Balint Right, that's what my answer says.Alright
There is no iPhone Simulator folder in Application Support. Where else could it be?Ploughboy
@modiX This answer was from 2010. A lot has changed since then. Resetting the simulator is likely your best bet. If you need to delete single cookies while keeping other things in place, you should be able to find them in Library/Developer/CoreSimulator/Devices/DEVICEIDENTIFIERHERE/data/Library/Cookies. Replace "DEVICEIDENTIFIERHERE" with the 37-character UUID directory name for that device. You'll be able to spot the right one if you just sort by "Date Modified" in the Finder.Alright
F
2

I just used fseventer to inspect what happens to the iPhone Simulator filesystem when the "Clear Cookies and Data" button is tapped. These commands replicate that behavior, however, there is a trick:

rm -rf "$HOME/Library/Application Support/iPhone Simulator/5.0/Library/Cookies"
rm -rf "$HOME/Library/Application Support/iPhone Simulator/5.0/Library/Caches/Snapshots"
rm -rf "$HOME/Library/Application Support/iPhone Simulator/5.0/Library/Caches/com.apple.mobilesafari"
rm -rf "$HOME/Library/Application Support/iPhone Simulator/5.0/Library/WebKit"
rm -rf "$HOME/Library/Application Support/iPhone Simulator/5.0/Library/Safari"

The Simulator needs to be restarted. So, before manipulating the filesystem, I run this:

killall "iPhone Simulator"
Foregoing answered 3/3, 2012 at 0:50 Comment(0)
A
0

You can go to the home screen, then the settings app. Tap Safari, then scroll down to Clear Cookies. I'm not sure where the cookie file is on the filesystem, somewhere under /Developer/Platforms/iPhoneSimulator.platform I'd expect (see Matthew Frederick's answer).

Avouch answered 13/12, 2010 at 9:6 Comment(1)
This was my thought as well, but it doesn't seem to actually clear the cookies when I do it.Alright
T
0

Resetting the simulator in newer MacOS versions:

Launch the simulator.

  • Click "Device" on the "menu bar".
  • A menu will appear. Go down two items to "Erase All Contents and Settings..."
  • Click "Reset" on the dialog which appears

how to reset ios simulator

Tatouay answered 12/1, 2023 at 13:10 Comment(0)
O
-1

Why not force cookie expiration to a low value for testing?

That is exactly the same then.

Oriel answered 13/12, 2010 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.