2016-11-12 19 views
0

我正在关注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方法。

有没有人有任何运气从外部来源检索电话号码?数据库,文件或其他东西?

回答

0

我一直无法弄清楚为什么,但我的应用程序扩展无法从我的sqlite数据库读取数据,只能写入它。我的猜测是,这是因为扩展比允许应用程序执行什么更严格的限制。

但是,如果我用普通文本文件替换数据库,则它可以工作,如果文件足够小。

我将我的电话号码列表划分为单独的文件,每个文件中有1000个电话号码。它似乎工作。

1

是呼叫目录扩展是极端的内存限制。我的经验是,你必须非常保守地分配内存,并且非常积极地明确释放内存。