如何清除iOS中特定网域的Cookie? [英] How to clear cookies for specific domain in iOS?

查看:173
本文介绍了如何清除iOS中特定网域的Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了几乎所有关于StackOverflow的问题的答案我的问题。我没有找到任何有用的链接或教程说哪种方式是最好的清除特定域的cookie。
所以如果有人可以帮助我。

I have searched almost all questions on StackOverflow for answer to my question. I haven't found any useful link or tutorial saying which way is best to clear cookies for particular domain. So please if someone could help me.

推荐答案


如果你想删除你的UIWebView中的整个cookie做这个。

I found solution myself. If you wish to delete entire cookies in your UIWebView do this.

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSHTTPCookie *cookie;
for (cookie in [storage cookies]) {
  NSLog(@"%@", cookie); // Print the deleted cookie.
  [storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];

如果您要删除特定于某个网站或网域的Cookie,请执行此操作。

If you wish to delete cookies specific to one site or domain do this.

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSHTTPCookie *cookie;
for(cookie in [storage cookies]) {
   if([[cookie domain] rangeOfString:@"siteName(or)domainName"].location != NSNotFound) {
     NSLog(@"cookie to be deleted:%@", cookie);
     [storage deleteCookie:cookie];
   }
 }



在上面的代码中,我使用了siteName请将其替换为您要删除Cookie的网站。您必须知道每个域都有子域。如果你在那个地方给gobal域名,它不会删除subDomains的cookie。
对于示例LinkedIn,它有许多子域,如in.linkedIn.com,api.linkedIn.com等。如果我给 http://www.linkedin.com 这是gobal域名,它不会删除子域的cookie。

In above code i have used siteName (or) DomainName replace it with site for which you want to remove cookies. You must know that every domain has subdomain. If you give gobal domain name in that place it wont delete cookies for subDomains. For Example LinkedIn, it has many subDomains like in.linkedIn.com, api.linkedIn.com etc. If i give http://www.linkedin.com which is gobal domain name it wont delete cookies for subdomains.

NSHTTPCookie有域属性,所以使用它来获取所有域名,并使用NSString的rangeOfString:方法获取具有你的String(例如linkedin)的域。

NSHTTPCookie has domain property so use that to get all domain names and using rangeOfString: method of NSString get domains which has your String(for example linkedin). Than do deletion on it will delete cookies for all its domain.

我没有人帮助我希望至少我的帖子会帮助未来的人。

I had no one for helping i hope at least my post will help someone in future.

这篇关于如何清除iOS中特定网域的Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆