t1w
t1w

Reputation: 1428

ios - how to change string in .html file from objective-c and update webview?

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>
<html>
    <head>
        <script type='text/javascript'>
            var serviceDataURL = "http://xx.xx.xxx.xxx:yy/get_item_data_ios?generic={%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',
                            buttons:[
                                {
                                    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
                            setTimeout(function(){
                                $('input.highcharts-range-selector').attr('readonly',1); // burda webviewı engelledik
                                $('input.highcharts-range-selector', $('#'+chart.options.chart.renderTo))
                            },0)
                        });
                });
            });
        </script>
    </head>
    <body>
        <div id="container" style="height: 500px; min-width: 500px;"></div>
    </body>
</html>

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:@"http://xx.xx.xxx.xxx:yy/get_item_data_ios?generic=%@",[Tools urlFriendly:jsonString]];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:dataUrl]];

    NSLog(@"\n\n\n\n\n\n\n\n\n\n");
    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];
}



-(void)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];
}


-(IBAction)goButton:(id)sender{

    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];

}

Upvotes: 1

Views: 1065

Answers (1)

Pancho
Pancho

Reputation: 4143

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

Upvotes: 1

Related Questions