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];
Community
  • 1
  • 1
ConfusedDeer
  • 3,335
  • 8
  • 44
  • 72
  • 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?) – race_carr Aug 29 '15 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. – ConfusedDeer Aug 29 '15 at 16:57
  • Ahh, interesting. So you want to craft a local HTML string, which has an `` 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. – race_carr Aug 29 '15 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. – ConfusedDeer Aug 29 '15 at 17:27
  • @ConfusedDeer, were you able to solve this issue ? Please share. I am having the same issue. – Partho Biswas Oct 18 '18 at 20:50
  • @ParthoBiswas This was more than 3 years ago, I hardly remember writing this, but I did mark matt's answer below as the answer, thus I'm sure it had something to do with it. Sorry. – ConfusedDeer Oct 23 '18 at 14:06

1 Answers1

2

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

matt
  • 515,959
  • 87
  • 875
  • 1,141