stringByEvaluatingJavaScriptFromString没有调用javascript函数 [英] stringByEvaluatingJavaScriptFromString not calling javascript function
问题描述
我正在研究手机缺口iOS应用程序.我需要将在 didRegisterForRemoteNotificationsWithDeviceToken
中收到的APNs令牌传递给我的Java脚本函数.
I am working on a phone gap iOS application. I need to pass the APNs token received in didRegisterForRemoteNotificationsWithDeviceToken
to my java script function.
didRegisterFoRmoteNotification
如下:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0)
{
NSString* token = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""] ;
NSLog(@"Device_Token -----> %@\n",token);
[self.viewController.webView loadHTMLString:@"<script type=text/javascript src=js/test.js></script>" baseURL:nil];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:@"myFunc('skdjch')"];
}
和javascript函数是:
and javascript function is:
function myFunc(str){
alert(str)
};
但是, myFunc
没有被调用.如果我从其他Java脚本文件中调用 myFunc
,则它将正常工作并显示警告.谁能解释我在做什么错?
but, myFunc
is not getting called.
If I call myFunc
from some other java script file then it works properly and displays he alert.
can anyone explain me what is wrong I am doing?
推荐答案
嘿,您不能像 function myFunc(str){那样调用praneti patidar {警报(str)};
希望您刚接触Phonegap,您需要bridge(Plugin)将值从本机iOS传递到Phongap Javascript函数
Hey praneti patidar you can't invoke like function myFunc(str){
alert(str)
};
hope you are new to Phone gap u need bridge(Plugin) to pass the values from native iOS to your Phongap Javascript function
您的js应该是这样
`function onDeviceReady() {
cordova.exec(function(winParam) {
if(winParam[2]=="8.0")
{
$rootScope.ios7subheadermargin="0%"
$rootScope.infotop="-13px"
$rootScope.infoleft="13%"
}
else
{
$rootScope.ios7subheadermargin="1%"
$rootScope.infotop="-16px"
$rootScope.infoleft="15%"
}
}, function(error) {
//alert(error)
}, "Pdfconverter","get_location",["",true]);
}`
此处Pdfconverter是本机iOS类(插件),其中get_location是方法,并且是这样
Here Pdfconverter is native iOS class(plugin) where get_location is the method and which is to be like this
-(void)get_location:(CDVInvokedUrlCommand *)command
{
cmds = command;
NSLog(@"The string is:%@",[command.arguments objectAtIndex:0]);
CDVPluginResult *result;
NSString *str_lattitude=[NSString stringWithFormat:@"%@",kUserlattitude];
NSString *str_longitude=[NSString stringWithFormat:@"%@",kUserlongitude];
NSString *str_Osversion=[NSString stringWithFormat:@"%@",kUserVersion];
NSArray *arr_values=[[NSArray alloc]initWithObjects:str_lattitude,str_longitude,str_Osversion, nil];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:arr_values];
[self.commandDelegate sendPluginResult:result callbackId:cmds.callbackId];
}
在上述相同的条件下,我将以数组的形式发送纬度,经度和操作系统版本,在您的情况下,您将发送设备令牌...
on the above same i am sending my lattitude,longitude and OS version as array and in your case you will be sending device token...
winParam上面是数组值...
Here above winParam holds the array values...
希望这对您有帮助...:)
Hope this helps...:)
这篇关于stringByEvaluatingJavaScriptFromString没有调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!