I'm following Xamarin's example of how to block phone numbers in an app for iOS 10, using a Call Directory Extension (full example here).
The following is my code to add a phone number to block (based on Xamarin's example):
bool AddBlockingPhoneNumbers(CXCallDirectoryExtensionContext context)
{
// This logging works, log written to database.
_logService.Log("Start adding numbers");
// Hardcoding phone numbers to add works.
var phones = new List<Phone> {
new Phone {
PhoneNumber = 14085555555,
CompanyName = "Bjarte" }
};
// When I uncomment the following line, the
// extension crashes here, I never get to the
// logging below.
//List<Phone> phones = _phoneService.GetPhones();
foreach (var phone in phones)
{
context.AddBlockingEntry(phone.PhoneNumber);
}
_logService.Log("Finished adding numbers");
return true;
}
To communicate between the app and the extension, I have set up an app group with a shared directory. Here I have an SQLite database, that both the app and the extension can successfully write to. For example, I use it for logging, since I cannot debug the extension directly.
In this database, I have the phone numbers I want to block.
Here is my method to retrieve phone numbers from the database. I'm using NuGet package sqlite-net.
public List<Phone> GetPhones()
{
var phones = new List<Phone>();
using (var db = new SQLiteConnection(DbHelper.DbPath()))
{
var phoneTable = db.Table<Phone>().OrderBy(x => x.PhoneNumber);
foreach (var phone in phoneTable)
{
phones.Add(new Phone
{
PhoneNumber = phone.PhoneNumber,
CompanyName = phone.CompanyName
});
}
}
return phones;
}
So far I have only managed to get to block phone numbers if I hardcode them to the AddBlockingPhoneNumbers method.
Has anyone had any luck retrieving phone numbers from an external source? Database, file or something else?