gpt4 book ai didi

javascript - ios - 如何从 objective-c 更改 .html 文件中的字符串并更新 webview?

转载 作者:行者123 更新时间:2023-11-29 12:44:51 24 4
gpt4 key购买 nike

我正在尝试在 highcharts javascript 图形库上显示我使用 objective-c 从我的网络服务中获取的一些数据。

为了从 Web 服务获取特定数据,我使用了根据用户与 UI 的交互而形成的查询字符串。

我不能做的是,将生成的 url 发送到我的 html 文件,因此当 url 更改时,来自 webservice 的结果数据将更改。它将显示新图形。

下面你可以看到我的 html 文件和 objective-c 代码。我怎样才能做到这一点?我如何将 dataURL 变量从 objective-c 发送到 .html 文件中的 serviceDataURL?

我的 html 文件:(这里没有包含所有 js 文件导入以使其更具可读性)

<!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>

我的 Objective-C 代码:

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

}

最佳答案

你可以在 UIWebView 中通过调用你的 JS 函数来评估 JS

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

或者你可以像这样加载 HTML 字符串

[webView loadHTMLString:htmlString baseURL:nil];

如果您需要从 JS 回调到 ObjC,您应该使用 UIWebView 委托(delegate)方法

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

}

要了解更详细的实现,您应该谷歌UIWebView JavaScript 桥接示例

关于javascript - ios - 如何从 objective-c 更改 .html 文件中的字符串并更新 webview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com