UIWebView 中的 cookie 是否被接受?

Are cookies in UIWebView accepted?(UIWebView 中的 cookie 是否被接受?)

本文介绍了UIWebView 中的 cookie 是否被接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问你.

1:我在我的 iPhone 应用程序中使用 UIWebViews.我不希望用户能够在新闻中添加评论.但是,要发表评论,他们必须登录.

1 : I'm using UIWebViews in my iPhone App. I wan't the users be able to add comments in the news. But, to comment they have to log-in.

如果没有,我如何在 UIWebViews 中接受 cookie?

If not, how can I accept cookies in UIWebViews ?

2:在 UIWebView 上创建的 cookie 在其他 UIWebView 中是否可用?

2 : Are the cookies created in on UIWebView available in others UIWebView in an other View ?

例如:我有我的 LoginViewController,带有嵌入式 UIWebView,我的用户可以在其中登录/注销.如果他们在此视图中登录,该 cookie 仍将在 CommentViewController 中可用?

Ex : I have my LoginViewController, with an embedded UIWebView, where my user can login/logout. If they log-in in this view, the cookie will be still available in the CommentViewController ?

如果没有,我怎样才能做到这一点?

If not, how can I make this possible ?

提前致谢!

推荐答案

UIWebView 会自动将cookies 存储在[NSHTTPCookieStorage sharedHTTPCookieStorage] 集合中,并且应该可用在您的应用内的所有其他 UIWebView 中,在同一个应用启动期间.但是,UIWebView 类不会自动为在应用启动之间加载的页面存储 cookie.您需要在应用移入后台时手动存储 cookie,并在应用移回前台时重新加载这些值.

The UIWebView will automatically store the cookies in the [NSHTTPCookieStorage sharedHTTPCookieStorage] collection, and should be available in all other UIWebViews within your app, during the same app launch. However the UIWebView class does not automatically store cookies for the pages that are loaded between app launches. You need to manually store cookies when the app is moved into the background and reload the values when the app is brought back into the foreground.

将以下代码放入您的 AppDelegate 类中:

Place the following code in your AppDelegate class:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Other existing code

    [self loadHTTPCookies];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //Other existing code

    [self saveHTTPCookies];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self loadHTTPCookies];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    //Other existing code
    [self saveHTTPCookies];
}

-(void)loadHTTPCookies
{
    NSMutableArray* cookieDictionary = [[NSUserDefaults standardUserDefaults] valueForKey:@"cookieArray"];

    for (int i=0; i < cookieDictionary.count; i++)
    {
        NSMutableDictionary* cookieDictionary1 = [[NSUserDefaults standardUserDefaults] valueForKey:[cookieDictionary objectAtIndex:i]];
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDictionary1];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
}

-(void)saveHTTPCookies
{
    NSMutableArray *cookieArray = [[NSMutableArray alloc] init];
    for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [cookieArray addObject:cookie.name];
        NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
        [cookieProperties setObject:cookie.name forKey:NSHTTPCookieName];
        [cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue];
        [cookieProperties setObject:cookie.domain forKey:NSHTTPCookieDomain];
        [cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath];
        [cookieProperties setObject:[NSNumber numberWithUnsignedInteger:cookie.version] forKey:NSHTTPCookieVersion];
        [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

        [[NSUserDefaults standardUserDefaults] setValue:cookieProperties forKey:cookie.name];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }

    [[NSUserDefaults standardUserDefaults] setValue:cookieArray forKey:@"cookieArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

这篇关于UIWebView 中的 cookie 是否被接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIWebView 中的 cookie 是否被接受?

基础教程推荐