Tuesday, January 20, 2015

UIWebView cookie setting from hosting app

It is possible to set cookies to the iOS layer so that the UIWebview displayed within the app can pick it up and apply the session value. Below code tries to set a session key to the google.com domain. 

 NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
                [cookieProperties setObject:@"authToken" forKey:NSHTTPCookieName];
                NSString *sessionID = [userdetails.sessionId stringByReplacingOccurrencesOfString:@"Wayfarer=" withString:@""];
                [cookieProperties setObject:sessionID forKey:NSHTTPCookieValue];
                [cookieProperties setObject:@"www.google.com" forKey:NSHTTPCookieDomain];
                [cookieProperties setObject:@"www.google.com" forKey:NSHTTPCookieOriginURL];
                [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
                [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
                [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
                NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
                

                [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

Even though application sets the cookie information, it is not possible that Safari or other application pick up this value in the request they make. 

No comments:

Post a Comment