0
votes

I need to load an HTML inside my IOS app but i need it to be an editable HTML so my aproach was the following:

I created a NSString with format so i could make a String with the HTML code depending on some variables:

NSString *googleGraphCode = [NSString stringWithFormat:@""<"html>"<"body>"<"div id=\"%@\">"<"/div>"<"script type=\"%@\" src=\"%@\"> google.load('visualization', '1.0', {'packages':['corechart']}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(); %@ data.addRows([%@]); var options = {'title':'%@', 'width':%i, 'height':%i}; var chart = new google.visualization.PieChart(document.getElementById('%@')); chart.draw(data, options); }"<"/script>"<"/body>"<"/html>" , chartID, javaScriptCall, AJAX_Api, javaScriptCall, columns, rows, title, widht, height, chartID];

Then I Loaded the webView with the HTMLString:

[_graphHostView loadHTMLString:googleGraphCode baseURL:nil];

I get the code in the NSLog and i get this:

"<"html>"<"body>"<"div id="chart_div">"<"/div>"<"script type="text/javascript" src="https://www.google.com/jsapi">"<"script type="text/javascript"> google.load('visualization', '1.0', {'packages':['corechart']}); google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('string', 'Tipo'); data.addColumn('string', 'CantidadGasto'); data.addRows([['Gastos', 30000], ['Ingresos', 50000]]); var options = {'title':'Su gráfica de balance:', 'width':320, 'height':300}; var chart = new google.visualization.PieChart(document.getElementById('chart_div')); chart.draw(data, options); }"<"/script>"<"/body>"<"/html>

But the webView on the device doesn't show anything.

Note: I used "<" before html code so Stackoverflow shows it on the question so "<"html> = regular html begining.

1

1 Answers

0
votes

loadHTMLString is the correct way to get the webview load an html

The problem may be _graphHostView is nil and because of that the loadHTMLString does not occur.

If creating _graphHostView programmatically, maybe it is missing the alloc/init and is nil

Check _graphHostView is not nil using the debugger.

An example of a working html:

NSString *styledHTML = [NSString stringWithFormat:@"<html> \n"
                                "<head> \n"
                                "<style type=\"text/css\"> \n"
                                "body {font-family: \"%@\"; font-size: %@;\n"
                                "background-color:#EEEEEE;}\n"
                                "</style> \n"
                                "</head> \n"
                                "<body bgcolor =0xEEEEEE >%@</body> \n"
                                "</html>", @"Museo Sans", [NSNumber numberWithInt:12], helpResponse.Content];


[contentView loadHTMLString:styledHTML baseURL:nil];

The NSString could also be overusing quotes ("")