Custom Header in post request using WkwebKit

Hello,

@rwenderlich @gdelarosa

I am implementing webView in my native application, But unable to find a suitable way to set my accessToken as an header in every call of UIWeview.

I have already try the solution which is provided on the bellow link:

But unable to implement properly, Session is not maintained properly in webview. Due to which first screen/Login screen is opening on every call of webview.

Please guide me.

Hi @aashish1aug,
with cut/paste programming there are plenty of code snips many of which could have changed or is no longer valid. So, it is always a good idea to post your code snip that you want working and isn’t.

another thing about using UIWebView, it is deprecated by Apple, and it is recommended to use WKWebView

hope that helps,

Jayant

Hi @jayantvarma,

First of all thanks for your reply !!

I am using the WKWebview in my iOS Application and try to save the cookies in NSUserDefault as bellow :

  • (void)webView:(WKWebView *)webView decidPolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) navigationResponse.response;
    NSLog(@“Response Status code: %ld”, (long)httpResponse.statusCode);

    NSArray *cookies = [Helper getDataFromNSDefaultWithKey:@“CookiesData”];
    if(cookies.count ==0){
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookiesArray = [storage cookies];
    [Helper saveDataInNSDefault:cookiesArray key:@“CookiesData”];
    }
    decisionHandler(WKNavigationResponsePolicyAllow);
    }

For WKWebview request,

  • (void)viewDidLoad
    {

    WKWebViewConfiguration *webViewConfig = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *controller = [[WKUserContentController alloc] init];

    [controller addScriptMessageHandler:self name:@""];
    [controller addScriptMessageHandler:self name:@""];
    webViewConfig.userContentController = controller;

    webView = [[WKWebView alloc] initWithFrame:viewFrame configuration:webViewConfig];//2
    webView.navigationDelegate = self;
    webView.UIDelegate=self;
    webView.scrollView.bounces=NO;
    webView.scrollView.delegate=self;
    webView.allowsBackForwardNavigationGestures=NO;

NSArray *cookies = [Helper getDataFromNSDefaultWithKey:@“CookiesData”];
if(cookies.count >0){

  _strWebUrl = [MAIN_APPLICATION_URL stringByAppendingString:[_strWebUrl stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@" \"#%/:<>?@[\\]^`{|}&"] invertedSet]]];
  
  url = [NSURL URLWithString:_strWebUrl]; //3
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //4
  [request setTimeoutInterval:ktimeoutIntervalForRequest];
  NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
  [request setAllHTTPHeaderFields:headers];
  [request setHTTPShouldHandleCookies:YES];
  [webView loadRequest:request];
  [Helper showProgressHUD:nil];
}
else{

  //-- This code will execute only for first time when user open the webview page. 

In this we are using the return url concept from login to respective page url by using the accessToken which user get during the Native Login.

  _strWebUrl = [MAIN_WEBVIEW_URL stringByAppendingString:[_strWebUrl stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@" \"#%/:<>?@[\\]^`{|}&"] invertedSet]]];
  url = [NSURL URLWithString:_strWebUrl]; //3
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //4
 // [request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
  [request setTimeoutInterval:ktimeoutIntervalForRequest];
  [request setValue:accessToken forHTTPHeaderField:@"Authorization"];
  [webView loadRequest:request]; //5
  [Helper showProgressHUD:nil];
}

}

But still webview page is not opening for second time, When we are using the stored cookies in request.

Please correct me.

This topic was automatically closed after 166 days. New replies are no longer allowed.