ios: ¿cómo cambiar la cadena en el archivo .html de Objective-C y actualizar la vista web?

I'm trying to display some data which I take from my web service with objective-c on highcharts javascript graphic library.

In order to take specific data from web service i used query string which i shape according to user's interactions with ui.

What i couldn't do is, sending resulting url to my html file, so when url is changed resulting data from webservice will change. And It will show new graphic.

Below you can see my htlm file and objective-c code. How can i do this? How can i send dataURL variable from objective-c to serviceDataURL in .html file?

My html file: (all js file importing not included here for making it more readable)

<!DOCTYPE html>
        <script type='text/javascript'>
            var serviceDataURL = "{%22line_ids%22:[],%22measure%22:%221%22,%22db_name%22:%22NPI%22,%22timeoffset%22:433112400000,%22show_total%22:true}";

            $(function() {
                $.getJSON(serviceDataURL, function(data) {
                    // Create the chart
                    window.chart = new Highcharts.StockChart({
                        chart : {
                            renderTo : 'container'
                        navigation: {
                            buttonOptions: {
                                enabled: false,
                                width: 60
                        rangeSelector : {
                            buttonSpacing: 20,
                            buttonTheme: { // styles for Q,Y,YTD,ALL buttons
                                fill: 'none',
                                stroke: 'none',
                                'stroke-width': 15,
                                style: {
                                    color: '#039',
                                    fontWeight: 'bold'
                                states: {
                                    hover: {},
                                    select: {
                                        fill: '#039',
                                            style: {
                                                color: 'white'
                            selected : 3, 
                            inputDateFormat: '%Y-%m-%d',
                            inputEditDateFormat: '%Y-%m-%d',
                                    type: 'month',
                                    count: 3,
                                    text: 'QQ'

                                    type: 'year',
                                    count: 1,
                                    text: 'YY'

                                    type: 'ytd',
                                    text: 'YTD'

                                    type: 'all',
                                    text: 'ALL'
                        title : {
                            text : 'My Total Market'
                        credits: {
                            text: " ",
                            href: " ",
                        series : [{
                            name : 'Total Market',
                            data : arr,
                            tooltip: {
                                valueDecimals: 2
                        exporting: {
                            enabled: false
                    }, function(chart){
                            // apply the date pickers
                                $('input.highcharts-range-selector').attr('readonly',1); // burda webviewı engelledik
                                $('input.highcharts-range-selector', $('#'+chart.options.chart.renderTo))
        <div id="container" style="height: 500px; min-width: 500px;"></div>

my objective-c code:

- (void) fetchChartData{

    ChartRequest *chartRequest = [ChartRequest new];
    chartRequest.measure = @"1";
    chartRequest.db_name = @"NPI";
    chartRequest.line_ids = [self fetchItemIDs];
    chartRequest.show_total = @"true";
    chartRequest.timeoffset = [NSNumber numberWithLongLong:(433112400000)];

    NSError * err;
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:chartRequest.toDictionary options:0 error:&err];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSLog(@"jsonString : %@",jsonString);

    NSString *dataUrl = [NSString stringWithFormat:@"",[Tools urlFriendly:jsonString]];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:dataUrl]];

    NSLog(@"dataUrl : %@ ",dataUrl);

    NSString *chartJsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"\n \nResponse : %@",chartJsonString);
    [_graphic stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"getJSON(%@, function", chartJsonString]];

    //[self displayGrid];
    [self displayhighcharts];

    NSString *pathOfFile=[[NSBundle mainBundle] pathForResource:@"indentificated_chart" ofType:@"html"];
    NSString *htmlText=[NSString stringWithContentsOfFile:pathOfFile encoding:NSUTF8StringEncoding error:nil];

    NSURL *baseURL = [NSURL fileURLWithPath:pathOfFile];
    [_graphic loadHTMLString:htmlText baseURL:baseURL];
    [alertconnection dismissWithClickedButtonIndex:0 animated:YES];

- (void)viewDidLoad
    [super viewDidLoad];
    [self displayhighcharts];

    _graphic.scrollView.scrollEnabled = NO;
    [self webViewDidFinishLoad:_graphic];

    [self fetchChartData];

    textfields=[[NSMutableArray alloc]initWithObjects:_firstchosen,_secondchosen,_thirdchosen,_fourthchosen,_fifthchosen, nil];

    [self hiddenfirst];


    NSString *pathOfFile;
    NSString *htmlText;
    NSURL *baseURL;

    pathOfFile=[[NSBundle mainBundle] pathForResource:@"indentificated_chart" ofType:@"html"];
    htmlText=[NSString stringWithContentsOfFile:pathOfFile encoding:NSUTF8StringEncoding error:nil];

    baseURL = [NSURL fileURLWithPath:pathOfFile];
    [_graphic loadHTMLString:htmlText baseURL:baseURL];

    [self fetchChartData];


preguntado el 28 de mayo de 14 a las 13:05

1 Respuestas

You can evaluate JS in UIWebView by calling yor JS function like

[webView stringByEvaluatingJavaScriptFromString:@"functionName()"];

Or you can load HTML string like

[webView loadHTMLString:htmlString baseURL:nil];

If you require callbacks from your JS into ObjC you should use the UIWebView delegate method

- (BOOL)webView:(UIWebView *)webView2 
          shouldStartLoadWithRequest:(NSURLRequest *)request 
          navigationType:(UIWebViewNavigationType)navigationType {

    // Create custom location change and check if it was called
    // and run your callback code here

    // or        

    // Accept this location change
    return YES;


For more detailed implementation you should Google UIWebView JavaScript bridge examples

contestado el 28 de mayo de 14 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.