{"id":938,"date":"2018-03-05T19:32:28","date_gmt":"2018-03-05T16:32:28","guid":{"rendered":"http:\/\/enisnecipoglu.com\/?p=938"},"modified":"2018-03-06T12:06:54","modified_gmt":"2018-03-06T09:06:54","slug":"xamarin-forms-reading-contacts","status":"publish","type":"post","link":"https:\/\/enisnecipoglu.com\/en\/xamarin-forms-reading-contacts\/","title":{"rendered":"Xamarin Forms Reading Contacts"},"content":{"rendered":"<p><\/p>\n<h1>Reading Contacts in Xamarin Forms<\/h1>\n<p>In this article, I&#8217;ll present to you how to read contacts from Portable Layer in Xamarin Forms. I&#8217;ve already created codes and you&#8217;ll use only one line code like this:<\/p>\n<div class=\"codecolorer-container csharp default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"csharp codecolorer\"><span class=\"kw1\">var<\/span> contacts <span class=\"sy0\">=<\/span> <span class=\"kw1\">await<\/span> Plugin<span class=\"sy0\">.<\/span><span class=\"me1\">ContactService<\/span><span class=\"sy0\">.<\/span><span class=\"me1\">CrossContactService<\/span><span class=\"sy0\">.<\/span><span class=\"me1\">Current<\/span><span class=\"sy0\">.<\/span><span class=\"me1\">GetContactListAsync<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<h2>Sample Project<\/h2>\n<p>Just create a new project and add <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms.Contacts\/\"><strong>Xamarin.Forms.Contacts<\/strong><\/a> NuGet package to all Layers<\/p>\n<figure id=\"attachment_944\" aria-describedby=\"caption-attachment-944\" style=\"width: 939px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" src=\"http:\/\/enisnecipoglu.com\/wp-content\/uploads\/2018\/03\/nugetContacts.png\" alt=\"Xamarin Forms Contacts Enis Necipoglu Android iOS Read Contacts\" width=\"939\" height=\"469\" class=\"size-full wp-image-944\" srcset=\"https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2018\/03\/nugetContacts.png 939w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2018\/03\/nugetContacts-300x150.png 300w, https:\/\/enisnecipoglu.com\/wp-content\/uploads\/2018\/03\/nugetContacts-768x384.png 768w\" sizes=\"(max-width: 939px) 100vw, 939px\" \/><figcaption id=\"caption-attachment-944\" class=\"wp-caption-text\">Xamarin Forms Contacts Enis Necipoglu Android iOS Read Contacts<\/figcaption><\/figure>\n<p>We&#8217;ll use MainPage which comes from new project as default. Firsly add a listview to <strong>MainPage.XAML<\/strong> and go <strong>MainPage.Cs<\/strong> and add reading code which I read at top of article and bind it<\/p>\n<h3>PERMISSIONS<\/h3>\n<p>Most important part is Permissions. Don&#8217;t forget to set permissions for each platform.<\/p>\n<p>For Android, you can add permission to <strong>AndroidManifes.xml<\/strong> manually but my suggestion is, right click the Android project and click <strong>Properties<\/strong> than go <strong>Android Manifest<\/strong> tab, you&#8217;ll see permissions windows that allows you set permissons via checkboxes. Just check <strong>READ_CONTACTS<\/strong> permission.<\/p>\n<p>For iOS, Right click the <strong>Info.plist<\/strong> and choose Open With opiton. Select XML Editor from there and add <strong>NSContactsUsageDescription<\/strong> as key and add a description between string tags why you want this permisson below the key into <strong>dict<\/strong> tags. (<em>Sample is below.<\/em>)  <\/p>\n<hr\/>\n<p><script src=\"https:\/\/gist.github.com\/enisn\/25fd0a63a849854fb6103aa681be9963.js\"><\/script><\/p>\n<hr\/>\n<h2>PROJECT FILES<\/h2>\n<ul>\n<li> <img src=\"http:\/\/enisnecipoglu.com\/Plugins\/Contacts.png\" width=\"20\" \/> <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms.Contacts\">NuGet Package &#8211; Xamarin.Forms.Contacts<\/a> <\/li>\n<li>\n<a href=\"https:\/\/github.com\/enisn\/Xamarin.Forms.Contacts\/tree\/master\/Sample.ContactService\"> Sample PROJECT<\/a>\n<\/li>\n<li><a href=\"https:\/\/github.com\/enisn\/Xamarin.Forms.Contacts\">Source Codes &#8211; Git<\/a><\/li>\n<\/ul>\n<hr\/>\n<blockquote>\n<h2>Possible Mistakes:<\/h2>\n<ul>\n<li> Over Android 5.0 versions you need to request Runtime Permissons to reach contact data.\n<\/li>\n<li>Users won&#8217;t give permission for contacts. You can use <a href=\"https:\/\/github.com\/jamesmontemagno\/PermissionsPlugin\">Permission Plugin<\/a> to avoid unexpected situations.\n<\/li>\n<\/ul>\n<\/blockquote>\n<p><\/p>","protected":false},"excerpt":{"rendered":"Reading Contacts in Xamarin Forms In this article, I&#8217;ll present to you how to read contacts from Portable Layer in Xamarin Forms. I&#8217;ve already created codes and you&#8217;ll use only one line code like this: var contacts = await Plugin.ContactService.CrossContactService.Current.GetContactListAsync&#40;&#41;; Sample Project Just create a new project and add Xamarin.Forms.Contacts NuGet package to all Layers We&#8217;ll use MainPage which comes from new project as default. Firsly add a listview to MainPage.XAML and go MainPage.Cs and add reading code which I read at top of article and bind it PERMISSIONS Most important part is Permissions. Don&#8217;t forget to set permissions for each platform. For Android, you can add permission to AndroidManifes.xml manually but my suggestion is, right click the Android project and click Properties than go Android Manifest tab, you&#8217;ll see permissions windows that allows you set permissons via checkboxes. Just check READ_CONTACTS permission. For iOS, Right click the Info.plist and choose Open With opiton. Select XML Editor from there and add NSContactsUsageDescription as key and add a description between string tags why you want this permisson below the key into dict tags. (Sample is below.) PROJECT FILES NuGet Package &#8211; Xamarin.Forms.Contacts Sample PROJECT Source Codes &#8211; Git Possible Mistakes: Over Android 5.0 versions you need to request Runtime Permissons to reach contact data. Users won&#8217;t give permission for contacts. You can use Permission Plugin to avoid unexpected situations.","protected":false},"author":1,"featured_media":952,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,15,6],"tags":[43,40,44,41,42,28,29],"_links":{"self":[{"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/posts\/938"}],"collection":[{"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/comments?post=938"}],"version-history":[{"count":13,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/posts\/938\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/posts\/938\/revisions\/954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/media\/952"}],"wp:attachment":[{"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/media?parent=938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/categories?post=938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enisnecipoglu.com\/en\/wp-json\/wp\/v2\/tags?post=938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}