0

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.

Here's the PHP code

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), "/"); 

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];
}

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

What objective-c (only) code do i use to keep the PHP cookies set so I won't have to sign in again?

PHP Web Dev 101
  • 535
  • 2
  • 9
  • 21

1 Answers1

0

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];
}
Community
  • 1
  • 1
Roberto Ferraz
  • 2,511
  • 24
  • 43
  • Did you mean - cookiesToSet – PHP Web Dev 101 Sep 24 '15 at 16:20
  • I did not receive any errors. When I opened the app and logged in, the cookies set. But when I closed the app and reopened it, the cookies did not stay set. – PHP Web Dev 101 Sep 24 '15 at 16:26
  • Your cookies won't be persisted between executions, you would need to save it to disk.. – Roberto Ferraz Sep 24 '15 at 16:28
  • http://stackoverflow.com/questions/2662530/iphone-nshttpcookie-is-not-saved-across-app-restarts – Roberto Ferraz Sep 24 '15 at 16:28
  • I receive errors when I add it to the code, how do i implement it? – PHP Web Dev 101 Sep 24 '15 at 16:40
  • Look.. I updated your code, I can't test, so I can't guarantee it will work, I recommend you to take a look in tutorials about NSUserdefaults and saving stuff to disk, because what I wrote there is not the best solution possible. You should not use stack overflow for learning, look for tutorials that it will be better. As the main problem of your question was alreadyy solved, please mark this question as answered so it won't show in the unanswered list. – Roberto Ferraz Sep 24 '15 at 16:42