PHP cookie没有在uiwebview中保持设置 [英] PHP cookie is not staying set in uiwebview

查看:88
本文介绍了PHP cookie没有在uiwebview中保持设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有登录名的UIWebView。登录名是一个PHP脚本,并具有setcookie()函数。当我登录时(在Webview中)但当我关闭应用程序(使用Webview)并重新打开它时,将设置cookie。 PHP cookie被取消设置,我必须再次登录。

I have a UIWebView which has a login. The login is a PHP script and has a setcookie() function. The cookies are set when I login (in the webview) but when I close out the app (with the webview) and reopen it. The PHP cookies get unset and I must login again.

这是PHP代码

setcookie($_SESSION['id'], $_SESSION['user_name'], time() + (86400 * 3), "/"); 
setcookie($_SESSION['pro_pic'], $_SESSION['status'], time() + (86400 * 3), "/"); 
setcookie($_SESSION['pro_pic'], $_SESSION['email'], time() + (86400 * 3), "/"); 

UIWebivew的代码

Code for UIWebivew

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];

    NSString *url=@"http://server.bithumor.co/bitpicks/index1.php";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];

    [webview loadRequest:nsrequest];

    webview.scrollView.bounces = NO;

    [self.view addSubview:webview];
    [self.view bringSubviewToFront:webview];
}

在普通的Safari浏览器中,cookie保持不变并可以正常工作。但是在UIWebView中不起作用。

On the normal safari web browser, the cookies stay set and work perfectly. But it doesn't work in the UIWebView.

我使用哪些Objective-c(仅)代码来保持PHP cookie的设置,这样我就不必再次登录?

推荐答案

如此处指出保持UIWebView中丢失php会话cookie 我更新了您的代码:

as pointed here Keep losing php session cookie in UIWebView I updated your code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];

    NSString *url=@"http://server.bithumor.co/bitpicks/index1.php";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSMutableURLRequest *nsrequest=[NSMutableURLRequest requestWithURL:nsurl];

    NSString *cookiesSaved = [[NSUserDefaults standardUserDefaults] objectForKey:@"cookies"];

    NSMutableString *cookieStringToSet = (cookiesSaved ? [NSMutableString stringWithString:cookiesSaved] : [NSMutableString new]);

    NSArray *cookiesToSet = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:nsurl];
    for (NSHTTPCookie *cookie in cookiesToSet) {

        if ([cookieStringToSet rangeOfString:cookie.name].location == NSNotFound)
        {
            [cookieStringToSet appendFormat:@"%@=%@;", cookie.name, cookie.value];
        }
    }

    [[NSUserDefaults standardUserDefaults] setObject:cookieStringToSet forKey:@"cookies"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [webview loadRequest:nsrequest];

    webview.scrollView.bounces = NO;

    [self.view addSubview:webview];
    [self.view bringSubviewToFront:webview];
}

这篇关于PHP cookie没有在uiwebview中保持设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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