iOS Remove All Cookies

Source: Internet
Author: User
First, delete all cookies three words to fix:
Nshttpcookie *cookie;
    Nshttpcookiestorage *storage = [Nshttpcookiestorage sharedhttpcookiestorage];
    for (cookie in [storage cookies])
    {
        [storage Deletecookie:cookie];
    }
    Cache Web Cleanup
    [[Nsurlcache Sharedurlcache] removeallcachedresponses];
Second, save cookies

In web development, we often use cookies to track and record some user information, such as token, user preferences, and so on. As in iOS, cookies can be used to record some of the user's choices or settings, such as: Users choose an interested industry, he hopes that the next time he open the app can directly see the industry-related information, then you can use cookies to record the user's choice, as shown:

Using cookies to record user selection


Industry_cat on behalf of an industry of interest chosen by the user, the client automatically takes the cookies when they make a request, and the benefit is obvious that no additional changes are required for the interface, such as adding a field to represent the industry. We just need to ensure that cookies are present and that they are not expired. But notice here is not the same as the Web browser is: iOS, application exit, the end of the session, cookies are discarded by default, and the browser by default is saved. So we also need to do some simple things for cookies:

nshttpcookiestorage* cookiestorage = [Nshttpcookiestorage sharedhttpcookiestorage];
    Nsarray<nshttpcookie *> *cookies = [Cookiestorage cookiesforurl:[nsurl Urlwithstring:baseurl]];
    Nsmutablearray<nsdictionary *> *propertieslist = [[Nsmutablearray alloc] init]; [Cookies enumerateobjectsusingblock:^ (Nshttpcookie * _nonnull cookies, Nsuinteger idx, BOOL * _nonnull stop)
        {Nsmutabledictionary *properties = [[Cookie properties] mutablecopy];
        Set the cookie expiration to one year later nsdate *expiresdate = [NSDate datewithtimeintervalsincenow:3600*24*30*12];
        Properties[nshttpcookieexpires] = expiresdate;
         The following line is key, delete the Discard field of the cookies, apply the exit, and continue to retain the cookies at the end of the session [Properties Removeobjectforkey:nshttpcookiediscard];
    Reset the modified cookies [Cookiestorage Setcookie:[nshttpcookie cookiewithproperties:properties]]; }];

That's what we're aiming for.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.