2015-05-29 82 views
0

我正在處理手機差距iOS應用程序。我需要將didRegisterForRemoteNotificationsWithDeviceToken中收到的APN令牌傳遞給我的Java腳本函數。stringByEvaluatingJavaScriptFromString不調用javascript函數

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函數爲:

function myFunc(str){ 
alert(str) 
}; 

但是,myFunc是沒有得到調用。 如果我從其他一些java腳本文件中調用myFunc,那麼它可以正常工作並顯示他的警報。 任何人都可以解釋我在做什麼錯?

回答

0

嘿praneti patidar你不能調用像function myFunc(str){ alert(str) }; 希望你是新來電話差距u需要橋(插件),以提供原生iOS的值傳遞到您的Phongap JavaScript函數

你的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是該方法並且其是這樣

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

} 

上述同一我發送我的lattitude,經度和操作系統版本爲數組,你的情況,你會被髮送設備令牌......

這裏以上winParam持有數組值...

希望這幫助... :)

+0

你好Sudhan,是的,我是新的PhoneGap,所以努力奮鬥:)。我認爲你給出的例子是從javascript調用本地iOS功能,但我需要從本機代碼調用JavaScript函數來停止設備令牌。 –

+0

praneti我只是回答了將令牌傳遞給您的javascript函數(phonegap應用程序)的方法,其次是您要求在webview上顯示警報,並將其放置在viewcontroller上?爲此使用NSNotificationCenter,以便委託使觀察者可以在視圖控制器上監聽.... – Sudhan