How to iteratively create PhoneGap contacts

I started a new project today to load the contacts from my personal database into my iPhone.

cd ~/Documents/pcrepo/aman-importer
phonegap create phonegap-test
cd phonegap-test
phonegap platform add ios
phonegap plugin add org.apache.cordova.contacts

There is documentation for the Contacts API.

The trouble I got into was iteratively creating contacts.

Code like this doesn’t work:

for ( var i in contact_data_list ) {
  var contact_data = contact_data_list[ i ];
  var contact = navigator.contacts.create();
  var name = new ContactName();
  name.givenName = contact_data.first_name;
  name.familyName = contact_data.last_name;
  contact.name = name;
  contact.save(
    function() { alert( 'saved!' ); },
    function() { alert( 'failed!' ); }
  );
}

The behavior is that contact.save fails and neither of the success or failure callbacks are invoked.

The way to iterate over the input data is like this:

var index = 0;
var saver = function() {
  if ( index === contact_data_list.length ) {
    // we're finished enumerating the array, we can report and exit:
    navigator.contacts.find( ['*'], report_contacts );
    return;
  }
  var contact_data = contact_data_list[ index++ ];
  var contact = navigator.contacts.create();
  var name = new ContactName();
  name.givenName = contact_data.first_name;
  name.familyName = contact_data.last_name;
  contact.name = name;
  contact.save(
    saver,
    function() { alert( 'failed!' ); }
  );
};
saver();

You can see it for real over here

Installing Subversion on Mac OS X with WebDAV support (serf library)

cd ~/Development/svn-install
wget http://prdownloads.sourceforge.net/scons/scons-2.3.4.tar.gz
wget http://mirror.ventraip.net.au/apache/apr/apr-1.5.1.tar.gz
wget http://mirror.ventraip.net.au/apache/apr/apr-util-1.5.4.tar.gz
wget http://mirror.ventraip.net.au/apache/subversion/subversion-1.8.10.tar.gz
tar xzf scons-2.3.4.tar.gz
tar xzf apr-1.5.1.tar.gz 
tar xzf apr-util-1.5.4.tar.gz
tar xzf subversion-1.8.10.tar.gz
cd scons-2.3.4
sudo python setup.py install
cd ../apr-1.5.1
./configure --prefix=/usr/local
make
sudo make install
cd ../apr-util-1.5.4
./configure --with-apr=/usr/local --prefix=/usr/local
make
sudo make install
cd ../subversion-1.8.10
sh get-deps.sh serf
cd serf
scons APR=/usr/local APU=/usr/local OPENSSL=/usr/local PREFIX=/usr/local
sudo scons PREFIX=/usr/local install
cd ..
./configure --with-openssl --with-serf=/usr/local --prefix=/usr/local
make
sudo make install

Nuff said!

Oh, wait… there’s a problem with misconfigured CA certificates…

cd ~/Development/svn-install
wget https://distfiles.macports.org/MacPorts/MacPorts-2.3.3-10.10-Yosemite.pkg

Run the MackPorts*.pkg…

xcode-select --install

Then…

sudo /opt/local/bin/port install curl-ca-bundle

Then…

sudo -s
cd /System/Library/OpenSSL/certs/
ln -s /opt/local/etc/openssl/cert.pem cert.pem
cd /usr/local/etc/openssl/certs
ln -s /opt/local/etc/openssl/cert.pem cert.pem

Testing:

openssl s_client -connect www.progclub.org:443 -CApath /opt/local/etc/openssl/

Using MacPorts OpenSSL:

sudo port install openssl
cd ~/Development/svn-install/subversion-1.8.10/serf/
scons APR=/usr/local APU=/usr/local OPENSSL=/opt/local PREFIX=/usr/local
sudo scons PREFIX=/usr/local install

Done!