Get UIWebView response header
I have looked into ways to get response header from UIWebview response.
This SO question discusses it. But I am unsure if this is allowed by apple. I will have a webview showing a loaded login page and need to get the response headers after a successful login. Also this
does something to get status code. But it create a duplicate NSUrlConnection request. Is there any way by which I can achieve this? I would appreciate any information on this.
ios objective-c uiwebview header
add a comment |
I have looked into ways to get response header from UIWebview response.
This SO question discusses it. But I am unsure if this is allowed by apple. I will have a webview showing a loaded login page and need to get the response headers after a successful login. Also this
does something to get status code. But it create a duplicate NSUrlConnection request. Is there any way by which I can achieve this? I would appreciate any information on this.
ios objective-c uiwebview header
1
As the creator of that answer, I personally can't say if it would pass inspection, although I have used similar techniques in apps that I have put on the store which have passed just fine. YMMV, the only way to know for sure is to submit the app for review.
– Richard J. Ross III
Mar 11 '14 at 13:47
add a comment |
I have looked into ways to get response header from UIWebview response.
This SO question discusses it. But I am unsure if this is allowed by apple. I will have a webview showing a loaded login page and need to get the response headers after a successful login. Also this
does something to get status code. But it create a duplicate NSUrlConnection request. Is there any way by which I can achieve this? I would appreciate any information on this.
ios objective-c uiwebview header
I have looked into ways to get response header from UIWebview response.
This SO question discusses it. But I am unsure if this is allowed by apple. I will have a webview showing a loaded login page and need to get the response headers after a successful login. Also this
does something to get status code. But it create a duplicate NSUrlConnection request. Is there any way by which I can achieve this? I would appreciate any information on this.
ios objective-c uiwebview header
ios objective-c uiwebview header
edited May 23 '17 at 12:09
Community♦
11
11
asked Mar 11 '14 at 12:30
rawatmrawatm
1621316
1621316
1
As the creator of that answer, I personally can't say if it would pass inspection, although I have used similar techniques in apps that I have put on the store which have passed just fine. YMMV, the only way to know for sure is to submit the app for review.
– Richard J. Ross III
Mar 11 '14 at 13:47
add a comment |
1
As the creator of that answer, I personally can't say if it would pass inspection, although I have used similar techniques in apps that I have put on the store which have passed just fine. YMMV, the only way to know for sure is to submit the app for review.
– Richard J. Ross III
Mar 11 '14 at 13:47
1
1
As the creator of that answer, I personally can't say if it would pass inspection, although I have used similar techniques in apps that I have put on the store which have passed just fine. YMMV, the only way to know for sure is to submit the app for review.
– Richard J. Ross III
Mar 11 '14 at 13:47
As the creator of that answer, I personally can't say if it would pass inspection, although I have used similar techniques in apps that I have put on the store which have passed just fine. YMMV, the only way to know for sure is to submit the app for review.
– Richard J. Ross III
Mar 11 '14 at 13:47
add a comment |
5 Answers
5
active
oldest
votes
In addition to the answer provided by DBD, you will need to ensure that
The containing UIViewController is marked as a UIWebViewDelegate in the .h file:
@interface VIMAuthenticationViewController : UIViewController <UIWebViewDelegate>
The UIWebView's delegate is set to the containing UIViewController. This can be done directly in the Interface Building or by linking the web view and adding the following in view did load in .m fie:
[self.WebView setDelegate:self];
Add the code as provided by DBD:
(void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
5
Holy crap. Additional context provided in a mobile app answer. That has to be a sign of the apocalypse and worthy of more +1s than I can sadly give.
– Erik Reppen
Jan 22 '16 at 23:46
add a comment |
This should do it for you.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
6
sometimes it return nil?
– neobie
Feb 24 '16 at 13:27
6
what if the cached response is null ? how do you handle that situation ?
– lifemoveson
Mar 24 '16 at 4:27
It can benil
. Make sure to setNSURLRequestUseProtocolCachePolicy
when creating your request.
– Rivera
Aug 8 '18 at 17:53
add a comment |
Swift 4
func webViewDidFinishLoad(_ webView: UIWebView) {
let headers = webView.request?.allHTTPHeaderFields
for (key,value) in headers! {
print("key (key) value (value)")
}
}
add a comment |
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
this function sometimes it returns nil,
I looked it up, If the file size exceeds 50kb, NSURLConnection does not call storeCachedResponse: forRequest;
add a comment |
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let request = webView.request {
let response = URLCache.shared.cachedResponse(for: request)
// ...
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22325818%2fget-uiwebview-response-header%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
In addition to the answer provided by DBD, you will need to ensure that
The containing UIViewController is marked as a UIWebViewDelegate in the .h file:
@interface VIMAuthenticationViewController : UIViewController <UIWebViewDelegate>
The UIWebView's delegate is set to the containing UIViewController. This can be done directly in the Interface Building or by linking the web view and adding the following in view did load in .m fie:
[self.WebView setDelegate:self];
Add the code as provided by DBD:
(void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
5
Holy crap. Additional context provided in a mobile app answer. That has to be a sign of the apocalypse and worthy of more +1s than I can sadly give.
– Erik Reppen
Jan 22 '16 at 23:46
add a comment |
In addition to the answer provided by DBD, you will need to ensure that
The containing UIViewController is marked as a UIWebViewDelegate in the .h file:
@interface VIMAuthenticationViewController : UIViewController <UIWebViewDelegate>
The UIWebView's delegate is set to the containing UIViewController. This can be done directly in the Interface Building or by linking the web view and adding the following in view did load in .m fie:
[self.WebView setDelegate:self];
Add the code as provided by DBD:
(void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
5
Holy crap. Additional context provided in a mobile app answer. That has to be a sign of the apocalypse and worthy of more +1s than I can sadly give.
– Erik Reppen
Jan 22 '16 at 23:46
add a comment |
In addition to the answer provided by DBD, you will need to ensure that
The containing UIViewController is marked as a UIWebViewDelegate in the .h file:
@interface VIMAuthenticationViewController : UIViewController <UIWebViewDelegate>
The UIWebView's delegate is set to the containing UIViewController. This can be done directly in the Interface Building or by linking the web view and adding the following in view did load in .m fie:
[self.WebView setDelegate:self];
Add the code as provided by DBD:
(void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
In addition to the answer provided by DBD, you will need to ensure that
The containing UIViewController is marked as a UIWebViewDelegate in the .h file:
@interface VIMAuthenticationViewController : UIViewController <UIWebViewDelegate>
The UIWebView's delegate is set to the containing UIViewController. This can be done directly in the Interface Building or by linking the web view and adding the following in view did load in .m fie:
[self.WebView setDelegate:self];
Add the code as provided by DBD:
(void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
edited Dec 30 '15 at 20:08
skypjack
34.2k1357135
34.2k1357135
answered Dec 30 '15 at 19:47
MagicFlowMagicFlow
350214
350214
5
Holy crap. Additional context provided in a mobile app answer. That has to be a sign of the apocalypse and worthy of more +1s than I can sadly give.
– Erik Reppen
Jan 22 '16 at 23:46
add a comment |
5
Holy crap. Additional context provided in a mobile app answer. That has to be a sign of the apocalypse and worthy of more +1s than I can sadly give.
– Erik Reppen
Jan 22 '16 at 23:46
5
5
Holy crap. Additional context provided in a mobile app answer. That has to be a sign of the apocalypse and worthy of more +1s than I can sadly give.
– Erik Reppen
Jan 22 '16 at 23:46
Holy crap. Additional context provided in a mobile app answer. That has to be a sign of the apocalypse and worthy of more +1s than I can sadly give.
– Erik Reppen
Jan 22 '16 at 23:46
add a comment |
This should do it for you.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
6
sometimes it return nil?
– neobie
Feb 24 '16 at 13:27
6
what if the cached response is null ? how do you handle that situation ?
– lifemoveson
Mar 24 '16 at 4:27
It can benil
. Make sure to setNSURLRequestUseProtocolCachePolicy
when creating your request.
– Rivera
Aug 8 '18 at 17:53
add a comment |
This should do it for you.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
6
sometimes it return nil?
– neobie
Feb 24 '16 at 13:27
6
what if the cached response is null ? how do you handle that situation ?
– lifemoveson
Mar 24 '16 at 4:27
It can benil
. Make sure to setNSURLRequestUseProtocolCachePolicy
when creating your request.
– Rivera
Aug 8 '18 at 17:53
add a comment |
This should do it for you.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
This should do it for you.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
answered Aug 5 '14 at 15:22
DBDDBD
14.4k125280
14.4k125280
6
sometimes it return nil?
– neobie
Feb 24 '16 at 13:27
6
what if the cached response is null ? how do you handle that situation ?
– lifemoveson
Mar 24 '16 at 4:27
It can benil
. Make sure to setNSURLRequestUseProtocolCachePolicy
when creating your request.
– Rivera
Aug 8 '18 at 17:53
add a comment |
6
sometimes it return nil?
– neobie
Feb 24 '16 at 13:27
6
what if the cached response is null ? how do you handle that situation ?
– lifemoveson
Mar 24 '16 at 4:27
It can benil
. Make sure to setNSURLRequestUseProtocolCachePolicy
when creating your request.
– Rivera
Aug 8 '18 at 17:53
6
6
sometimes it return nil?
– neobie
Feb 24 '16 at 13:27
sometimes it return nil?
– neobie
Feb 24 '16 at 13:27
6
6
what if the cached response is null ? how do you handle that situation ?
– lifemoveson
Mar 24 '16 at 4:27
what if the cached response is null ? how do you handle that situation ?
– lifemoveson
Mar 24 '16 at 4:27
It can be
nil
. Make sure to set NSURLRequestUseProtocolCachePolicy
when creating your request.– Rivera
Aug 8 '18 at 17:53
It can be
nil
. Make sure to set NSURLRequestUseProtocolCachePolicy
when creating your request.– Rivera
Aug 8 '18 at 17:53
add a comment |
Swift 4
func webViewDidFinishLoad(_ webView: UIWebView) {
let headers = webView.request?.allHTTPHeaderFields
for (key,value) in headers! {
print("key (key) value (value)")
}
}
add a comment |
Swift 4
func webViewDidFinishLoad(_ webView: UIWebView) {
let headers = webView.request?.allHTTPHeaderFields
for (key,value) in headers! {
print("key (key) value (value)")
}
}
add a comment |
Swift 4
func webViewDidFinishLoad(_ webView: UIWebView) {
let headers = webView.request?.allHTTPHeaderFields
for (key,value) in headers! {
print("key (key) value (value)")
}
}
Swift 4
func webViewDidFinishLoad(_ webView: UIWebView) {
let headers = webView.request?.allHTTPHeaderFields
for (key,value) in headers! {
print("key (key) value (value)")
}
}
answered Nov 22 '18 at 10:01
Charlie SeligmanCharlie Seligman
2,33723262
2,33723262
add a comment |
add a comment |
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
this function sometimes it returns nil,
I looked it up, If the file size exceeds 50kb, NSURLConnection does not call storeCachedResponse: forRequest;
add a comment |
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
this function sometimes it returns nil,
I looked it up, If the file size exceeds 50kb, NSURLConnection does not call storeCachedResponse: forRequest;
add a comment |
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
this function sometimes it returns nil,
I looked it up, If the file size exceeds 50kb, NSURLConnection does not call storeCachedResponse: forRequest;
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
this function sometimes it returns nil,
I looked it up, If the file size exceeds 50kb, NSURLConnection does not call storeCachedResponse: forRequest;
edited Dec 29 '18 at 8:56


dkb
1,43411625
1,43411625
answered Dec 29 '18 at 5:22
toby-yingtoby-ying
1
1
add a comment |
add a comment |
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let request = webView.request {
let response = URLCache.shared.cachedResponse(for: request)
// ...
}
}
add a comment |
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let request = webView.request {
let response = URLCache.shared.cachedResponse(for: request)
// ...
}
}
add a comment |
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let request = webView.request {
let response = URLCache.shared.cachedResponse(for: request)
// ...
}
}
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if let request = webView.request {
let response = URLCache.shared.cachedResponse(for: request)
// ...
}
}
answered Nov 18 '18 at 16:45


AnthonyRAnthonyR
958626
958626
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22325818%2fget-uiwebview-response-header%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
As the creator of that answer, I personally can't say if it would pass inspection, although I have used similar techniques in apps that I have put on the store which have passed just fine. YMMV, the only way to know for sure is to submit the app for review.
– Richard J. Ross III
Mar 11 '14 at 13:47