2012-10-17 22 views
1

我已經寫在手機上的差距文檔以下代碼無法接取接觸website` 與PhoneGap的應用

// Wait for Cordova to load 
// 
document.addEventListener("deviceready", onDeviceReady, false); 

// Cordova is ready 
// 
function onDeviceReady() { 
    // find all contacts 
    var options = new ContactFindOptions(); 

    var fields = ["displayName", "name"]; 
    navigator.contacts.find(fields, onSuccess, onError, options); 
} 

// onSuccess: Get a snapshot of the current contacts 
// 
function onSuccess(contacts) { 
    for (var i=0; i<contacts.length; i++) 
    { 
     console.log("Display Name = " + contacts[i].displayName); 
     var element = document.getElementById('cont'); 

     element.innerHTML='<li><a href="#">' + contacts[i].displayName + '</a></li>'; 

    } 
} 

// onError: Failed to get the contacts 
// 
function onError(contactError) { 
    alert('onError!'); 
} 

</script>` 

它是工作在安卓模擬器罰款,但是當我安裝該應用程序的三星Galaxy手機就返回「空值」。請幫助。

+0

什麼返回null?的onSuccess? –

+0

成功則返回null。 –

+0

什麼是空 - 聯繫人? –

回答

2

奇怪......我也得到了null ...(使用PhoneGap的版本2.0)

我建議你使用類似contacts[i].name.formatted,而不是contacts[i].displayName


因此,例如,通過使用contacts[i].name.formatted,你的代碼看起來應該是這樣的:

// Wait for Cordova to load 
// 
document.addEventListener("deviceready", onDeviceReady, false); 

// Cordova is ready 
// 
function onDeviceReady() { 
    // find all contacts 
    var options = new ContactFindOptions(); 

    var fields = ["displayName", "name"]; 
    navigator.contacts.find(fields, onSuccess, onError, options); 
} 

// onSuccess: Get a snapshot of the current contacts 
// 
function onSuccess(contacts) { 

    var element = document.getElementById('cont'); 
    for (var i=0; i<contacts.length; i++) 
    { 
     console.log("Display Name = " + contacts[i].name.formatted);     
     element.innerHTML='<li><a href="#">' + contacts[i].name.formatted + '</a></li>';  
    } 
} 

// onError: Failed to get the contacts 
// 
function onError(contactError) { 
    alert('onError!'); 
} 


這裏有不同的信息,您可以用contacts[i].name獲得:

  • contacts[i].name.givenName:提供給定名稱

  • contacts[i].name.formatted:提供格式的名稱(名+姓)

  • contacts[i].name.middleName:提供中間名

  • contacts[i].name.familyName:提供(名+姓)

  • contacts[i].name.honorificPrefix:提供敬語前綴

  • contacts[i].name.honorificSuffix:提供敬語後綴

希望這有助於。

1

這是一個簡單解決方案的複雜問題。僅僅只增加了代碼

options.filter=""; 
    options.multiple=true; 
    var filter = ["*"]; 

這是完整的一行代碼是

<script type="text/javascript" charset="utf-8"> 

// Wait for Cordova to load 
// 
document.addEventListener("deviceready", onDeviceReady, false); 

// Cordova is ready 
// 
function onDeviceReady() { 
    // find all contacts 
    var options = new ContactFindOptions(); 
    options.filter=""; 
    options.multiple=true; 
    var filter = ["*"]; 
    navigator.contacts.find(filter, onSuccess, onError, options); 


} 

// onSuccess: Get a snapshot of the current contacts 
// 
function onSuccess(contacts) { 
    var ele = document.getElementById("cont"); 
    var str = '<ul data-role="listview" >'; 
    for (var i=0; i<contacts.length; i++) { 


     str=str + '<li>' + contacts[i].displayName + '</li>'; 
    } 
    str = str + '</ul>'; 

    // ele.innerHTML = str; 
} 

// onError: Failed to get the contacts 
// 
function onError(contactError) { 
    alert('onError!'); 
} 

而且它的工作,順便也顯示了一些空值作爲每個聯繫人條目,但它工作正常。