Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

KeithsPuma

macrumors member
Original poster
Jul 12, 2004
40
190
Hi
I have added a HTML file to my project so that it's embedded within the application.
With the following code I can view the HTML file in the iPhone simulator.

Code:
NSBundle *bundle   = [ NSBundle mainBundle ];
NSString *path     = [ bundle bundlePath ];
NSString *fullPath = [ NSBundle pathForResource:@"TestHTML" ofType:@"html" inDirectory:path ];
NSString *params   = @"?Fred=1.2&Bert=-2.2&Harry=3";
NSString *fullURL  = [ NSString stringWithFormat:@"%@%@", fullPath, params ];
// fullPath = "/Users/keith/Library/Application Support/iPhone Simulator/User/Applications/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/TestApp.app/TestHTML.html"
[ self loadRequest:[ NSURLRequest requestWithURL:[ NSURL fileURLWithPath:fullPath ] ] ];

Now this works as it can Find the Embedded HTML file "TestHTML.html"

My question is how do I pass parameters into the HTML file ie "Fred=1 Bert=-2.2 Harry=3" so that the java script can use them.
If I try the usual method by appending "?Fred=1.2&Bert=-2.2&Harry=3" to the URL then the application does not work, I suspect its trying to find the file "TestHTML.html?Fred=1.2&Bert=-2.2&Harry=3"

Code:
// fullURL = "/Users/keith/Library/Application Support/iPhone Simulator/User/Applications/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/TestApp.app/TestHTML.html?Fred=1.2&Bert=-2.2&Harry=3"
[ self loadRequest:[ NSURLRequest requestWithURL:[ NSURL fileURLWithPath:fullURL ] ] ];

This does NOT work

any suggestions please

Keith
 

pashik

macrumors member
Jul 16, 2008
43
0
U can add this parameters as hidden fields into html
<input type="hidden" name="Fred" value="1">
Javascript can work with elements in DOM without probelms
 

KeithsPuma

macrumors member
Original poster
Jul 12, 2004
40
190
U can add this parameters as hidden fields into html
<input type="hidden" name="Fred" value="1">
Javascript can work with elements in DOM without probelms

Thanks for your reply, but I don't think I explained the problem properly.
I have a HTML file that works when I pass variable into it by adding "?fred=1" etc. after the html extension.

I want the application to pass these variable to the HTML so that the javascript can use these values.
The way I usually do this is by doing "test.html?Fred=1"
Then the java script processes this as a parameter and creates the variable accordingly.

I need to pass these variable values into the HTML file, but because I've EMBEDDED the HTML file within the application, it can't find the HTML file in its Bundle if a append anything after the ".html" even a space.

Thanks
Keith
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Are you sure the NSURL object is valid on the iPhone? I'd put it in a separate variable so you can check it specifically.

Also I would use this code for getting the path - a bit cleaner:
Code:
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"TestHTML.html" ofType:nil];
 

KeithsPuma

macrumors member
Original poster
Jul 12, 2004
40
190
Are you sure the NSURL object is valid on the iPhone? I'd put it in a separate variable so you can check it specifically.
I'm pretty sure as if I don't append parameters it works. if i just an a single space to the URL then it doesn't.
I have used 2 variable above initial post the testing ( fullpath and fullURL ) if I use the fullpath in the call "requestWithURL" it works but not if I call "requestWithURL" with the fullURL variable.
I've tested it with fullpath and just added a space to it and that doen't.

Also I would use this code for getting the path - a bit cleaner:
Code:
..@"TestHTML.html" ofType:nil];
agreed that does look neater, thanks for that.
Thanks for the help. I just wonder if its a bug or by design ?

Keith
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I bet it's not working because fileURLWithPath expects a valid file, but you are appending the URL parameters to the file path. I would try using fileURLWithPath with the original .html file name (without the parameters), then on the URL call absoluteString to get the full URL as a string (which would be file://something, and then append to that the URL parameters.
 

KeithsPuma

macrumors member
Original poster
Jul 12, 2004
40
190
Thanks

Thanks to all who tried to help.
I've given up with trying to pass variables.
I now dynamically create the web page.
For those who are interested this seems to work ;-)
Code:
float fred = 1.234;
NSError *error;
// Create the webpage
NSString * webPage = @"";
webPage = [ webPage stringByAppendingString:@"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" ];
webPage = [ webPage stringByAppendingString:@"<html\">\n" ];
webPage = [ webPage stringByAppendingString:@"  <head>\n" ];
webPage = [ webPage stringByAppendingString:@"    <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\n" ];
webPage = [ webPage stringByAppendingString:@"    <title>iPhone - Test WebPage</title>\n" ];
webPage = [ webPage stringByAppendingString:@"    <script type=\"text/javascript\">\n" ];
webPage = [ webPage stringByAppendingString:@"      var fred =" ];
webPage = [ webPage stringByAppendingString: [ NSString stringWithFormat:@"%f", fred ] ];
webPage = [ webPage stringByAppendingString:@";" ];
.....
webPage = [ webPage stringByAppendingString:@"</html>\n" ];
		
// write to file
if ( [ webPage writeToFile:@"webpage.html" atomically:YES encoding:NSUTF8StringEncoding error:&error ] )
{
  NSLog( @"SUCCESS IN WRITING FILE" );
  // Now try to display the WebPage
  [ self loadRequest:[ NSURLRequest requestWithURL:[ NSURL fileURLWithPath:@"webpage.html" ] ] ];
}
else
{
  NSLog( @"FAILED TO WRITE FILE" );
}

NSLog( webPage );
regards Keith
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Did you try what I wrote above? I just tested and it works :)
Code:
NSString *htmPath = [[NSBundle mainBundle] pathForResource:@"params.htm" ofType:nil];
NSString *fileURLString = [[NSURL fileURLWithPath:htmPath] absoluteString];
NSString *params = @"?name=bob";
NSURL *fileURL = [NSURL URLWithString:[fileURLString stringByAppendingString:params]];
[webView loadRequest:[NSURLRequest requestWithURL:fileURL]];

The HTML I used:
HTML:
<html>
<meta name="viewport" content="minimum-scale=1.0, width=device-width, maximum-scale=1">
<script language="javascript">
// from http://netlobo.com/url_query_string_javascript.html
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function get_name()
{
  return gup('name');
}
</script>
<body>
Your name is: <script language="javascript">document.write(get_name());</script>
</body>
</html>
 

KeithsPuma

macrumors member
Original poster
Jul 12, 2004
40
190
Did you try what I wrote above? I just tested and it works :)
I did try but i misunderstood what you meant, I just put "file://" at the beginning of the string. Anyway Just tried your method and it works a treat. All is not lost as a newbee I have been learning xcode. As a seasoned windows "delphi" programmer its a big culture shock in the differences between Object C and Pascal.
Thanks
Keith
 

svjim

macrumors newbie
Aug 27, 2008
11
0
referencing bundled javascript files

This thread has been a tremendous help to me, however I have an additional question.

I am using a UIWebView and I am able to load an html file that is in the resources directory, but it references javascript libraries that are also in the resource directory.

<script language="javascript" type="text/javascript" src="foo.js"></script>

Since foo.js in in the in the same directory, I thought it would load, but not so. Any suggestion on what I need to do to make this work would be appreciated.

Thanks,

Jim
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I would suggest using tokens. For exame, replace foo.js with $foo.js$, then you could use NSMutableString to replace $foo.js$ with the full path, which you can find using NSBundle's pathForResource:eek:fType:. So the first time you launch the app, read in the HTML from your app's resources folder, replace the tokens, and then write it out to your app's documents folder.
 

svjim

macrumors newbie
Aug 27, 2008
11
0
relative URLS with UIWebView

Again thanks for all the help. I ended up doing the following.



Code:
// get the path to the main bundle

	NSString *bundlePath = [[NSBundle mainBundle]bundlePath];
	
	// get the path to the  the html file
	
	NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"tutorial2.html" ofType:nil];
	
	// read the html page into a string
	NSString *htmlText = [NSString stringWithContentsOfFile:fullPath];
	
	// Load the page as a string with the base URL set to the bundlePath 
            -- Now relative references in the page work
	
	[webView loadHTMLString:htmlText baseURL:[NSURL fileURLWithPath:bundlePath]];


I am still going to use tokens in the HTML, but not for relative URL addresses. I hope this might help someone else facing a similar problem.

Jim
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.