1

How do I convert the following to use 'loadRequest' instead of 'loadHTMLString'?

I need to add a HTTP header and the only way I have found is to use loadRequest with a NSMutableURLRequest as explained here.

myHTMLContent = [NSString stringWithFormat:@"<head>%@</head><body>%@</body>", @"<link href='styles.css' rel='stylesheet' type='text/css'>", htmlFromPrevVar]; 

    NSString *myPath = [[NSBundle mainBundle] resourcePath];
    NSURL *myBaseURL = [NSURL fileURLWithPath:myPath];

    [self.myUIWebView loadHTMLString:myHTMLContent baseURL:myBaseURL];
6
  • Why would you need to add a HTTP header to a loadRequest: that is coming from the file system? Do you want to actually load the HTML from a network resource (i.e. a web page?) Commented Aug 29, 2015 at 16:37
  • @race_carr The variable titled htmFromPrevVar that gets concatenated to the html has a a path to an image that requires an oAuth bearer token in the authorization header. When you load the html we can see the authentication error on the server. Commented Aug 29, 2015 at 16:57
  • Ahh, interesting. So you want to craft a local HTML string, which has an <img> tag in it that requires an OAuth header? And you need some way to load the local html (via file or in-memory) via a NSURLRequest so you can add the authentication header. Commented Aug 29, 2015 at 17:20
  • @race_carr Exactly. That's the issue I'm fixing with the iOS application. The picture will not load if the authorization header with the oAuth bearer token is not included. I'm working on a solution based on what Matt said, but it's not pretty and a the bit slow. Commented Aug 29, 2015 at 17:27
  • @ConfusedDeer, were you able to solve this issue ? Please share. I am having the same issue. Commented Oct 18, 2018 at 20:50

1 Answer 1

2

Save the string to a file and give the URL of that file in your request.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please explain why we need to do that ? Is there any better way to handle such situation ? @matt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.