iPhone Development – HTTP cookies for connections and UIWebViews

A very common requirement when connecting to a server from the iPhone or within a UIWebView is the ability to set a cookie. Cookies are often used for authentication in particular. Fortunately there is a nice way to set a global cookie for any external connections your app makes using NSHttpCookieStorage. The function below sets a global cookie with the specified domain, name and value (the path used is /):

+ (void) setCookie:(NSString *)cookieName:(NSString *)cookieValue:(NSString *)cookieDomain
{
    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                                cookieDomain, NSHTTPCookieDomain,
                                cookieName, NSHTTPCookieName,
                                cookieValue, NSHTTPCookieValue,
                                @"/", NSHTTPCookiePath,
                                nil];
    
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:[NSHTTPCookie cookieWithProperties:properties]];
}

One response to “iPhone Development – HTTP cookies for connections and UIWebViews”

  1. Alexander1 says:

    Need cheap generic VIAGRA?…

Leave a Reply

Your email address will not be published. Required fields are marked *