Reputation: 8079
I have this posted on PhoneGap google groups and cross-posted it here on Stack Overflow.
Anyway, PhoneGap's Contact API is giving me headache lately. To cut it short:
Environment
I have this application that scans a QR code with VCard embeded, and it will save it into user's contact once the user agree to save it down. On Android 4.1.2
, this piece of code works flawlessly without any error, and contact was saved too. On iOS 6
, however, I can't get it to work. What I have always receive is error #4
, which is, ContactError.IO_ERROR
.
The code is as below:
navigator.notification.confirm('Found a contact: "'+(vcard['N'] || '')+'". Add it into your contact list?', function(btn) {
if (btn == 1) {
var contact = navigator.contacts.create();
contact.displayName = vcard['N'] || '';
contact.emails = [new ContactField('email', vcard['EMAIL'] || '')];
var org = new ContactOrganization();
org.name = vcard['ORG'] || '';
contact.organizations = [org];
contact.save(function() {
navigator.notification.alert('Contact Saved');
}, function(err) {
navigator.notification.alert('Error on saving contact: '+err.code);
});
}
}, 'QR Code Scanner', 'OK,Cancel');
And below is the only log I have seen on XCode right after I tap on 'OK': (first callback is for the notification.confirm
)
2012-10-22 21:19:24.619 [11897:907] PluginResult:toJSONString - {"status":1,"message":1,"keepCallback":false}
2012-10-22 21:19:24.621 [11897:907] PluginResult toSuccessCallbackString: cordova.callbackSuccess('Notification12',{"status":1,"message":1,"keepCallback":false});
2012-10-22 21:19:24.738 [11897:907] PluginResult:toJSONString - {"status":9,"message":4,"keepCallback":false}
2012-10-22 21:19:24.739 [11897:907] PluginResult toErrorCallbackString: cordova.callbackError('Contacts13',{"status":9,"message":4,"keepCallback":false});
Then I will see the alert:
Error on saving contact: 4
Note
Is this the correct way of creating contact? How can you save a contact on iOS 6 using PhoneGap?
I did all the necessary migration and upgraded to v2.1.0, but the error code still emerges. Why is this?
Apparently this has something to do with the new AddressBook permissions request on iOS 6 as mentioned by Mark, but no one has answered me on how to do it properly. Spent some times on research I came to a conclusion that it's best if we can just wait for Phonegap to finish up v2.2 as I saw permission request being added in CDVContacts.m in v2.2 rc2 commit 61857860...
Upvotes: 0
Views: 1167
Reputation: 247
i've had the same problem. As it turns out, i was using phonegap version 1.9, which did not support iOS 6, therefore adding a contact gave me Error 4 message all the time. To fix this, i upgraded to phonegap version 2.1. This version supports iOS 6 as well as both armv6 and armv7 architectures. The error message was because iOS 6 requires permissions to access the address book, in the same way that using the GPS requires permission on iOS. This should solve your problem, it did for me :)
Upvotes: 0
Reputation: 121
https://issues.apache.org/jira/browse/CB-902
Currently crashes if the user does not have AddressBook permission on iOS 6. The user will get a popup dialog similar to the Geolocation permissions dialog. When creating an address book, we should handle the condition where the app does not have permission, and the address book returned is NULL.
Upvotes: 2