0

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?

Rahul
  • 5,594
  • 7
  • 38
  • 92
  • check this link http://stackoverflow.com/questions/5954382/ios-is-it-possible-to-set-a-cookie-manually-using-sharedhttpcookiestorage-for-a @rahul – Prissy Eve Feb 07 '17 at 05:37
  • @PrissyEve I wrote this by using same link as a reference. – Rahul Feb 07 '17 at 05:43

0 Answers0