Wednesday, May 1, 2013

NSMutableURLRequest setMainDocumentURL example ios


setMainDocumentURL:

Sets the main document URL for the receiver.
- (void)setMainDocumentURL:(NSURL *)theURL
Parameters of [NSMutableURLRequest setMainDocumentURL]
theURL
The new main document URL. Can be nil.
Discussion of [NSMutableURLRequest setMainDocumentURL]
The caller should set the main document URL to an appropriate main document, if known. For example, when loading a web page the URL of the HTML document for the top-level frame would be appropriate. This URL will be used for the “only from same domain as main document” cookie accept policy.
Example of [NSMutableURLRequest setMainDocumentURL]
NSURL *url = [NSURL URLWithString:@"http://www.facebook.com/myapp" relativeToURL:[NSURL URLWithString:@"http://login.facebook.com"]];
    NSMutableURLRequest *request = nil;

    if(url)
        request = [NSMutableURLRequest requestWithURL:url];

    NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://login.facebook.com"]];
    NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies];
    if(request)
    {
        [request setHTTPShouldHandleCookies:YES];
        [request setMainDocumentURL:[NSURL URLWithString:@"http://login.facebook.com"]];
        [request setAllHTTPHeaderFields:headers];//login.facebook.com
        [mWebView loadRequest:request];
    }