我正在关注Xamarin如何使用Call Directory Extension(full example here)在iOS 10的应用程序中阻止电话号码的示例。从数据库或其他来源检索电话号码到AddBlockingEntry
以下是我的代码添加一个电话号码,以阻止(基于Xamarin的示例):
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;
}
为了该应用,所述延伸部分之间进行通信,我已成立的应用程序组的共享目录。在这里我有一个SQLite数据库,应用程序和扩展都可以成功写入。例如,我使用它进行日志记录,因为我无法直接调试扩展。
在这个数据库中,我有我想阻止的电话号码。
这是我从数据库中检索电话号码的方法。我正在使用NuGet包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;
}
到目前为止,我只设法阻止电话号码,如果我将它们硬编码到AddBlockingPhoneNumbers方法。
有没有人有任何运气从外部来源检索电话号码?数据库,文件或其他东西?