0
votes

I have to open a URL in UIWebview with a cookie. I have to send a cookie to UIWebView. Only if the cookie value will correct then webview will load.

Here is my setup:

In my appDelegate's method

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

   }

This is how I am creating cookie and loading request in ViewController's viewDidLoad method:

//Set Cookie
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"username" forKey:NSHTTPCookieName];
[cookieProperties setObject:cookieValue forKey:NSHTTPCookieValue];
[cookieProperties setObject:@".xyz.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];

[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

//Create Reqeust
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:completeURL]];

[_webView loadRequest:request];

Issue I am facing

It only works in my simulator. I tested in device and on other mac's it is not working. And sometimes it is not even works in my system.

I am unable to find what is the reason behind it?

@PrissyEve I wrote this by using same link as a reference.Rahul