Wednesday, May 1, 2013

NSURLRequest valueForHTTPHeaderField example ios


valueForHTTPHeaderField:

Returns the value of the specified HTTP header field.
- (NSString *)valueForHTTPHeaderField:(NSString *)field
Parameters of [NSURLRequest valueForHTTPHeaderField]
field
The name of the header field whose value is to be returned. In keeping with the HTTP RFC, HTTP header field names are case-insensitive.
Return Value of [NSURLRequest valueForHTTPHeaderField]
The value associated with the header field field, or nil if there is no corresponding header field.
Example of [NSURLRequest valueForHTTPHeaderField]
-(BOOL)webView:(UIWebView *)webView
 shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{  
     userAgent = [[request valueForHTTPHeaderField:@"User-Agent"] copy]; 
     NSLog(@"user-agent: %@", userAgent);

     _webView.delegate = nil; 
     [_webView release]; 

     return NO; 
}
Example of [NSURLRequest valueForHTTPHeaderField]
if ( [request valueForHTTPHeaderField:@"MyUserAgent"] == nil )
{
    NSMutableURLRequest *modRequest = [request mutableCopyWithZone:NULL];
    [modRequest setValue:@"myagent" forHTTPHeaderField:@"MyUserAgent"];
    [webViewArgument loadRequest:modRequest];
    return NO;
}