我正在關注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方法。
有沒有人有任何運氣從外部來源檢索電話號碼?數據庫,文件或其他東西?