我正在关注 Xamarin 关于如何使用呼叫目录扩展(此处为完整示例)在 iOS 10 应用程序中阻止电话号码的示例。
以下是我添加要阻止的电话号码的代码(基于 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 方法,我只能设法阻止它们。
有没有人幸运地从外部来源检索电话号码?数据库、文件还是其他?