Sunday, June 16, 2013

NSHTTPCookieStorage NSHTTPCookieAcceptPolicyNever example in Objective C (iOS).


NSHTTPCookieStorage NSHTTPCookieAcceptPolicyNever

NSHTTPCookieAcceptPolicy
NSHTTPCookieAcceptPolicy specifies the cookie acceptance policies implemented by the NSHTTPCookieStorage class.

typedef enum {
NSHTTPCookieAcceptPolicyAlways,
NSHTTPCookieAcceptPolicyNever,
NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain
} NSHTTPCookieAcceptPolicy;

Constants
NSHTTPCookieAcceptPolicyAlways
Accept all cookies. This is the default cookie accept policy.
NSHTTPCookieAcceptPolicyNever
Reject all cookies.
NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain
Accept cookies only from the main document domain.

NSHTTPCookieStorage NSHTTPCookieAcceptPolicyNever example.
/Create a NSURLConnection and start it
-(void) begin {
    NSURL* url = [NSURL URLWithString@"https://some.domain.com/some/path/?some=query"];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    if ([NSURLConnection canHandleRequest:request]) {
        NSURLConnection* connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
        hasSeenResponse = NO;
        [connection start];
    }
}    

End of NSHTTPCookieStorage NSHTTPCookieAcceptPolicyNever example article.