gpt4 book ai didi

javascript - cordova 如果电话号码不存在则保存联系人

转载 作者:行者123 更新时间:2023-11-29 00:03:15 24 4
gpt4 key购买 nike

我正在尝试保存不存在的电话簿(以电话号码作为主要索引)联系人。

我已经尝试检查与导航器的联系。联系人。找到 ()`,并检查长度是否为 0(如果没有联系,保存联系)但似乎不起作用。

有时我的 ionic 应用程序会在电话簿中保存双重(或超过 2 个)联系人,并且它会在我的电话簿中保存 103 个相同的电话号码。

代码:

var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
onDeviceReady: function() {
this.receivedEvent('deviceready');
document.getElementById("call").addEventListener("click", ayam);
cordova.plugins.backgroundMode.enable();
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');

console.log('Received Event: ' + id);
}
};

app.initialize();
function dialogAlert() {
var message = "Sync Android Contact has been activated!";
var title = "Info";
var buttonName = "Close";
navigator.notification.alert(message, alertCallback, title, buttonName);
function alertCallback() {
console.log("Alert is Dismissed!");
}
}

function ayam() {
if($(".aba").val().length > 3) {
setInterval(function(){
kambing();
}, 5000); // recheck server every 5 second
dialogAlert();
}
else {
alert("Write your API Url !");
}
}

function kambing(){
var url = $(".aba").val();
$.ajax({
type : "GET",
url : url + "/save",
dataType : 'html',
success: function(response){
var hp = response
, anu = hp.split(",");
anu.forEach(function(v){
save_contact(v);
})
},
error : function() {
alert("Failed to fetch url");
}
});
}

function save_contact(xs){
var pn = xs.replace(/\D/g,'');
if(pn.length > 3) {
var options = new ContactFindOptions();
options.filter = xs;
options.multiple = true;
options.desiredFields = [navigator.contacts.fieldType.id];
options.hasPhoneNumber = true;
var fields = [navigator.contacts.fieldType.phoneNumbers];
navigator.contacts.find(fields, onSuccess, onError, options);

function onSuccess(contacts) {
if(contacts.length < 1) {
var myContact = navigator.contacts.create({"displayName": "The New Contact"});
var name = new ContactName();
name.givenName = xs;
myContact.name = name;

var phoneNumbers = [];
//phoneNumbers[0] = new ContactField('work', '212-555-1234', false);
phoneNumbers[0] = new ContactField('mobile', pn, true); // preferred number
//phoneNumbers[2] = new ContactField('home', '203-555-7890', false);
myContact.phoneNumbers = phoneNumbers;
myContact.note = "Helo";
myContact.save(sukses, elor);
function sukses(contact) {
//alert("saved")
$.ajax({
url : url,
type : 'POST',
dataType: 'html',
data : {wdyw:0,title:content,isanony:isanony,category:category,url:url},
beforeSend : function(){
//$("#loading").show();
},
success : function(result){

$('[name="content"]').val('');
$('[name="content"]').val('Pertanyaan anda telah terkirim :)');
setTimeout(function(){$('[name="content"]').val('');},500);
}
});
};
function elor(contactError) {};
}
};

function onError(contactError) {
return false;
};
}
}

最佳答案

重写为ionic,(因为更稳定)

关于javascript - cordova 如果电话号码不存在则保存联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48110525/

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