Skip to content

Commit

Permalink
TIMOB-17467 - Custom headers form WebView (iOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manoj Kumar committed Apr 3, 2016
1 parent f0ff8bf commit 091149c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions iphone/Classes/TiUIWebView.m
Expand Up @@ -657,6 +657,35 @@ -(CGFloat)contentWidthForWidth:(CGFloat)value
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL * newUrl = [request URL];

NSDictionary *headers = [self.proxy valueForKey:@"headers"];
if(headers && ![headers isEqual:[NSNull null]])
{
NSEnumerator *enumerator = [headers keyEnumerator];
id firstKey = [enumerator nextObject];
BOOL headerIsPresent = [[request allHTTPHeaderFields] objectForKey:firstKey] != nil;
if(!headerIsPresent)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

//first key
[request addValue:[TiUtils stringValue:firstKey properties:headers def:@""] forHTTPHeaderField:firstKey];

//rest of key-value pairs in object (if any)
id key;
while ((key = [enumerator nextObject])) {
[request addValue:[TiUtils stringValue:key properties:headers def:@""] forHTTPHeaderField:key];
}

[[self webview] loadRequest:request];
});
});
return NO;
}
}

if ([self.proxy _hasListeners:@"beforeload"])
{
Expand Down

2 comments on commit 091149c

@nuno
Copy link

@nuno nuno commented on 091149c Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like referrer URL ?

@manojdcoder
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this relates to referrer URL. This is same as Titanium.Network.HTTPClient.setRequestHeader but with WebView.

Please sign in to comment.