2013-12-10 16 views
0

我正在使用PhoneGap開發iOS混合應用程序。將數組從本地傳遞到混合視圖

我的問題

我創建一個數組,我要發送到混合視圖在本機代碼數據。

這裏是我的代碼,

的index.html

function myJavascriptFunction (obj){ 
    var array = new Array(); 
    array = obj; 
    console.log(array); 
} 

objective.m

{ 
    array =[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil]; 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"NotificationCalled" object:array]; 
} 

如何從原始數據傳遞到混合動力的看法?

+0

你不能用'NSNotificationCenter'做到這一點。您必須創建自定義插件才能實現此目的。 –

+0

嗨Bhumeshwer,可以告訴我該怎麼做。我是新來的電話差距 – Siva

回答

0

請繼續進行以下步驟

  1. 編寫JavaScript方法來處理JSON陣列對象。

    function myJavascriptFunction (obj){ // Process json array object. obj.length will give you length and obj[i] will give you i'th element } 
    
  2. 寫objc代碼來創建JSON字符串

    array =[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil]; 
    NSString * jsonString = [NSString stringWithFormat:@"[ %@ ]",[array componentsJoinedByString:@","]]; 
    
  3. 然後通過jsonString的JavaScript

    [yourwebview stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"myJavascriptFunction(%@);",jsonString ]];

+0

非常感謝你..非常感謝你..它的工作中間 – Siva

相關問題