3

I have a PhoneGap/Corodova Project for iOS. Whilst building on the iOS simulator I used Jquery Cookies and they were fine. However, now that I have the app on my device for testing, they no longer work. I'm guessing this is just something iOS doesnt support.

Does anyone know a way to do client side cookies for iOS?

PS: My current cookies code (just in case its useful):

<script type="text/javascript" charset="utf-8" src="js/jquery.cookie.js"></script>
    <!--STARTUP SCREEN COOKIE SETTER-->
     <script type="text/javascript">
        $(function() {
        var cookiedate = new Date();
        cookiedate.setTime(cookiedate.getTime() + (60 * 60 * 1000));
        $.cookie("startupscreen", "checked", { expires: cookiedate, path: '/' })
              });
    </script>
MeltingDog
  • 14,310
  • 43
  • 165
  • 295
  • try this settings, might work - https://groups.google.com/forum/?fromgroups#!topic/phonegap/ZJE1nxX63ow – dhaval Jun 27 '12 at 06:08
  • I have the same problem. I'm trying to set cookies from an AJAX call though, and in PhoneGap 2.0 the NSHTTPCookieStorage setting is already in AppDelegate.m... but to no avail the iOS6 won't set the cookie from my .NET MVC3 application. – sonjz Oct 25 '12 at 19:22

2 Answers2

2

Why not use localStorage? ios5 supports HTML5 features like localStorage.

localStorage

weexpectedTHIS
  • 3,358
  • 1
  • 25
  • 30
  • Thanks! Thats what I ended up doing though I'm not sure if this is the best way as local storage is supposed to be discontinued by W3C – MeltingDog Oct 25 '12 at 23:43
  • localStorage will not be discontinued, it's the sql database that will be discontinued. But even if the spec is removed from html5 browsers will probably continue to support it after. – weexpectedTHIS Oct 26 '12 at 06:12
  • Only reason WebSQL spec isn't being developed further is that everybody accidentally standardized on embedded SQLite, so it was beside the point to push the standard further... It shouldn't be going anywhere. – XML Dec 19 '12 at 20:32
1

Put this here in your app delegate :

-(void)applicationDidEnterBackground:(UIApplication *)application{
    NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
    [[NSUserDefaults standardUserDefaults] setObject:cookiesData
                                              forKey:@"xapp"];

}

and in (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:

(NSDictionary*)launchOptions 
// Restore cookies
    NSData *cookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"xapp"];
    if (cookiesData) {
        NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesData];
        for (NSHTTPCookie *cookie in cookies)
        { [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; }

    }

This made cookies working as usual in Cordova 3.4.x and ios6+

xamiro
  • 1,391
  • 1
  • 16
  • 32
  • Code can be formatted by indenting by four spaces (you can select and use the `{}` button as a shortcut.) – McDowell Mar 23 '14 at 16:45