iPhone应用Cookie延迟 [英] iPhone App Cookie Delay

查看:186
本文介绍了iPhone应用Cookie延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程式有一个UIWebView载入包含设定Cookie的JavaScript的网页。似乎如果我设置一个cookie,并在10-15秒内退出应用程序,那么该cookie不会被保存,但是如果我设置了cookie,请等待10-15秒退出应用程序,该cookie被保存。

My iPhone app has a UIWebView that loads a page containing javascript that sets a cookie. It seems that if i set a cookie and exit the app within 10-15 seconds then the cookie is never saved, however if i set the cookie, wait 10-15 seconds THEN exit the app, the cookie is saved.

任何人都知道为什么他们是延迟,以及如何立即保存cookies。

Anyone have any info about why their is a delay and how to go about having the cookies saved immediately.

推荐答案

我唯一的解决方法是能够想出的是在应用程序终止之前将Cookie保存到用户默认值。当应用程序打开时,通过用户默认值,拉出cookie,并将其重写到cookie存储。

The only workaround i was able to come up with is to save the cookies to the user defaults right before the app terminates. When the app is opened, go through the user defaults, pull out the cookies, and rewrite them to the cookie storage. It works but if your app is forcefully terminated then it doesnt really.

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

    // Load the saved cookies
    NSDate *currentDate = [NSDate date];
    NSTimeInterval expirationAmount = 5 * 365 * 24 * 60 * 60;
    NSDate *expirationDate = [currentDate dateByAddingTimeInterval:expirationAmount];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    for (id theKey in [defaults dictionaryRepresentation]) {
        if ([theKey hasPrefix:@"cookie:"]) {
            [self setCookie:[theKey substringFromIndex:7] value:[defaults objectForKey:theKey] expiration:[expirationDate description] domain:urlDomain];
        }
    }
}

- (void)applicationWillTerminate:(UIApplication *)application {
  // Save the cookies to the user defaults
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  NSArray* theCookies = [cookieStorage cookies];
 for(NSHTTPCookie *myStr in theCookies) {
    [defaults setValue:[myStr value] forKey:[NSString stringWithFormat:@"cookie:%@", [myStr name]]];
     }
     [[NSUserDefaults standardUserDefaults] synchronize];
   }

这篇关于iPhone应用Cookie延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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