Question

I am currently creating a UIWebView where I load a NSString that I get from my server service. its just a simple HTML string, then I load it into a UIWebView like this:

urlAddress = [[NSURL alloc] initWithString:URLString];

//try removing cache so new stylesheet shows everytime- not sure if this is working yet
[[NSURLCache sharedURLCache] removeAllCachedResponses];


[InfoWebview loadHTMLString:returnedHtmlString baseURL:urlAddress];

Then for some reason the images are not showing and the style sheet is not working.

For instance my baseURL looks like this

https://myserveraddress/

and then the code for the CSS looks like this

<link rel="stylesheet" type="text/css" href="Data/iPhone/my.css">

My questions are this How does baseURL prefix itself onto that href? dose it request that css as the loadHTMLString loads onto the WebView? or something different

And How can I output the infoWebView source to NSString? to see if baseURL is prefixing itself onto those href links? or dose it now work like that?

Any help would be greatly appreciated.

Était-ce utile?

La solution

How does baseURL work?

Here are some helpful examples from the AFNetworking documentation:

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
[NSURL URLWithString:@"foo" relativeToURL:baseURL];                  // http://example.com/v1/foo
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];          // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:@"/foo" relativeToURL:baseURL];                 // http://example.com/foo
[NSURL URLWithString:@"foo/" relativeToURL:baseURL];                 // http://example.com/v1/foo
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL];                // http://example.com/foo/
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/

Basically, any files in your HTML will be assumed to be in the location specified in the Base URL. If the HTML specifies files beginning with /, the domain name will stay the same but the path of the Base URL will be ignored. If the HTML specifies files beginning with http(s)://, the entire Base URL will be ignored.

In your example, the URL would resolve to https://myserveraddress/Data/iPhone/my.css.

How can I output the infoWebView source to NSString?

A simple solution is:

NSString *html = [InfoWebview stringByEvaluatingJavaScriptFromString: 
                                         @"document.body.innerHTML"];

More info and discussion on this topic is available on Stack Overflow here.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top