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.