Search code examples
c#xamarinios10callkit

Retrieving phone numbers from database or other source to AddBlockingEntry


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?


Solution

  • I haven't been able to figure out why, but my app extension cannot read data from my sqlite database, only write to it. My wild guess is that it is because an extension have much stricter limitations to what it is allowed to do than an app.

    However, if I replace the database with a plain text file, it works, if the file is small enough.

    I'm dividing my list of phone numbers into separate files with 1000 phone numbers in each. It seems to work.