<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Filip Němeček</title>
    <description>The latest articles on DEV Community by Filip Němeček (@nemecek_f).</description>
    <link>https://dev.to/nemecek_f</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F320103%2Fef5abc18-7963-4c90-8d98-bbc941bf7d95.jpg</url>
      <title>DEV Community: Filip Němeček</title>
      <link>https://dev.to/nemecek_f</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nemecek_f"/>
    <language>en</language>
    <item>
      <title>I re-built the Instagram profile detail Compositional Layout and Diffable to help folks learn</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Mon, 18 Jan 2021 19:02:58 +0000</pubDate>
      <link>https://dev.to/nemecek_f/i-re-built-the-instagram-profile-detail-compositional-layout-and-diffable-to-help-folks-learn-46fp</link>
      <guid>https://dev.to/nemecek_f/i-re-built-the-instagram-profile-detail-compositional-layout-and-diffable-to-help-folks-learn-46fp</guid>
      <description>&lt;p&gt;As an iOS developer I am very excited about relatively new technologies &lt;strong&gt;Compositional Layout&lt;/strong&gt; and &lt;strong&gt;Diffable Data Sources&lt;/strong&gt;. Thanks to these building UI similar to App Store or Netflix became way easier and less error prone.&lt;/p&gt;

&lt;p&gt;I have previously written about these technologies in more an abstract way, focusing on the principles and how to use them. &lt;/p&gt;

&lt;p&gt;Now I decided to try something very different and "re-built" the Instagram profile page.&lt;/p&gt;

&lt;p&gt;My goal is to show newcomers to iOS development how to approach such task and how to step-by-step build the entire UI.&lt;/p&gt;

&lt;p&gt;Here is the result side-by-side 😄&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sbKqmP0t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/instantgramComparisson.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sbKqmP0t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/instantgramComparisson.png" alt="Instantgram comparisson"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have written &lt;a href="https://nemecek.be/blog/72/building-instagram-profile-screen-with-compositional-layout"&gt;detailed post&lt;/a&gt; and the entire code is available on &lt;a href="https://github.com/nemecek-filip/CompositionalDiffablePlayground.ios"&gt;GitHub&lt;/a&gt;. Check it out!&lt;/p&gt;

&lt;p&gt;Feel free to ask me any questions 🙂&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title> Learn compositional collection view layout and diffable data source with example code</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Thu, 19 Nov 2020 17:51:17 +0000</pubDate>
      <link>https://dev.to/nemecek_f/learn-compositional-collection-view-layout-and-diffable-data-source-with-example-code-3aag</link>
      <guid>https://dev.to/nemecek_f/learn-compositional-collection-view-layout-and-diffable-data-source-with-example-code-3aag</guid>
      <description>&lt;p&gt;Compositional collection view layouts and diffable data sources are great UIKit additions available for iOS 13 and up. They greatly simplify building collection views (especially those with complicated layout) and populating those collections views with data.&lt;/p&gt;

&lt;p&gt;No longer it is needed to manually specify the number of sections and rows and to keep the collection view correctly updated. These new APIs will do much of the hard work for you.&lt;br&gt;
I wanted to get a better grasp on compositional collection views and diffable data sources so I created public repository to share my experiments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UyWt4Iem--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://nemecek.be/media/images/compositionalDiffableMainScreen_fP0RG7L.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UyWt4Iem--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://nemecek.be/media/images/compositionalDiffableMainScreen_fP0RG7L.gif" alt="CDP Main screen small"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far I have one main screen with fairly complicated layout. Multiple sections where some are horizontal and items are responsive to fill the width along with screens dedicated for specifics layouts. For example list layout, grid layout with option to change grid cell size and see them animate or lazy loaded grid which is ideal when you need to implement paging of results from API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1k547eBX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://nemecek.be/media/images/CDPResizableGrid.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1k547eBX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://nemecek.be/media/images/CDPResizableGrid.gif" alt="CDP Resizable Grid Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QH80ljiv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://nemecek.be/media/images/CDPLazyGrid.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QH80ljiv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://nemecek.be/media/images/CDPLazyGrid.gif" alt="CDP Lazy Grid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am by no means an expert so please take this code and examples more like a suggestion and inspiration than best practices.&lt;/p&gt;

&lt;p&gt;Do you have layout you want to see? Let me know and I can try to implement it.&lt;/p&gt;

&lt;p&gt;The project is available on &lt;a href="https://github.com/nemecek-filip/CompositionalDiffablePlayground.ios"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>showdev</category>
      <category>swift</category>
    </item>
    <item>
      <title>How to use Core Data with Diffable Data Source - a minimal example</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Mon, 28 Sep 2020 19:44:03 +0000</pubDate>
      <link>https://dev.to/nemecek_f/minimal-example-of-using-diffable-data-source-with-core-data-19hn</link>
      <guid>https://dev.to/nemecek_f/minimal-example-of-using-diffable-data-source-with-core-data-19hn</guid>
      <description>&lt;p&gt;If you have been developing iOS apps for some time, setting up Table Views and Collection Views is likely muscle memory. You prepare the UI component, set datasource and delegate and then implement methods that tell said component how many sections and rows in each section to accept. The last needed step is to configure cell for each row.&lt;/p&gt;

&lt;p&gt;Using this old setup with Core Data means in most cases &lt;code&gt;NSFetchedResultsController&lt;/code&gt; and implementation of its delegate which tells you when data content changed and you need to update the Table/Collection View accordingly.&lt;/p&gt;

&lt;p&gt;Diffable Data Sources make this much easier. But also the process is totally different and can be a bit hard to get used to.&lt;/p&gt;

&lt;p&gt;I am writing this short example for myself and other to speed up setting up Diffable in new projects with Core Data. The result is three shorter methods that make the initial collection view work.&lt;/p&gt;

&lt;p&gt;I am using &lt;code&gt;UICollectionView&lt;/code&gt; in this example, but &lt;code&gt;UITableView&lt;/code&gt; is basically identical.&lt;/p&gt;

&lt;p&gt;Note: In your project substitute &lt;code&gt;Model&lt;/code&gt; class from my example with your own Core Data entity.&lt;/p&gt;

&lt;p&gt;Let's start with declaring properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;datasource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UICollectionViewDiffableDataSource&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;!&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;fetchedResultsController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;NSFetchedResultsController&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note: For &lt;code&gt;UITableView&lt;/code&gt; you would use &lt;code&gt;UITableViewDiffableDataSource&lt;/code&gt;. The &lt;code&gt;Int&lt;/code&gt; is used because this requires type for section. If you have just one you can use &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt; and then pass either "0" or empty string.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring UICollectionViewDiffableDataSource
&lt;/h3&gt;

&lt;p&gt;Next we configure the &lt;code&gt;datasource&lt;/code&gt; like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;configureDatasource&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;datasource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UICollectionViewDiffableDataSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;collectionView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;collectionView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;cellProvider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;collectionView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indexPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;UICollectionViewCell&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;cell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;collectionView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dequeueReusableCell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;withReuseIdentifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"ModelCell"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;indexPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as!&lt;/span&gt; &lt;span class="kt"&gt;ModelCell&lt;/span&gt;
            &lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;configure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cell&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All we need to do is to create an instance of &lt;code&gt;UICollectionViewDiffableDataSource&lt;/code&gt;. We give it our &lt;code&gt;collectionView&lt;/code&gt; and configure &lt;code&gt;cellProvider&lt;/code&gt; which is modern version of the old &lt;code&gt;cellForRowAt&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;This could be separate method we could pass in. Since this is an example and I have just a single I am using inline version. &lt;/p&gt;

&lt;h3&gt;
  
  
  NSFetchedResultsController
&lt;/h3&gt;

&lt;p&gt;Time to configure the &lt;code&gt;NSFetchedResultsController&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;initFetchedResultsController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fetchedResultsController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;NSFetchedResultsController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;fetchRequest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sortedFetchRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;managedObjectContext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Database&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shared&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;sectionNameKeyPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;cacheName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;fetchedResultsController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;
        &lt;span class="k"&gt;try!&lt;/span&gt; &lt;span class="n"&gt;fetchedResultsController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;performFetch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Animating changes
&lt;/h3&gt;

&lt;p&gt;And almost as a last step we need to implement a method that will update our &lt;code&gt;collectionView&lt;/code&gt; when data changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;NSFetchedResultsController&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;NSFetchRequestResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didChangeContentWith&lt;/span&gt; &lt;span class="nv"&gt;snapshot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;NSDiffableDataSourceSnapshotReference&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;diffableDataSourceSnapshot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;NSDiffableDataSourceSnapshot&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;diffableDataSourceSnapshot&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendSections&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;diffableDataSourceSnapshot&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fetchedResultsController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetchedObjects&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;
        &lt;span class="n"&gt;datasource&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;diffableDataSourceSnapshot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animatingDifferences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And lastly we need to call these methods in &lt;code&gt;viewDidLoad&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;configureDatasource&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;initFetchedResultsController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Of course &lt;strong&gt;this solution is not perfect&lt;/strong&gt; and is not meant to be. My goal was to show simplest working example to get started which you can tweak to your liking.&lt;/p&gt;

&lt;p&gt;I write about iOS/Swift here and on my blog &lt;a href="https://nemecek.be"&gt;nemecek.be&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>How to select random record with Django ORM</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Mon, 28 Sep 2020 19:38:46 +0000</pubDate>
      <link>https://dev.to/nemecek_f/how-to-select-random-record-with-django-orm-47ph</link>
      <guid>https://dev.to/nemecek_f/how-to-select-random-record-with-django-orm-47ph</guid>
      <description>&lt;p&gt;Recently I wanted to implement more sophisticated banners on my site to promote my &lt;a href="https://nemecek.be/pages/apps"&gt;iOS apps&lt;/a&gt;. Initially I had just one hardcoded banner in the template but that was not going to work for multiple banners.&lt;/p&gt;

&lt;p&gt;The part that took the longest was to display random banner on a page. This involves mainly getting random record (or entity if you will) from your database. &lt;em&gt;I have found quite a few solutions but they were either too complicated or relied on some preconditions like you had to have a table with no deleted records + sequential ids and then you could use something like last id or count and select random number from that.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;My solution does not have any requirements but could be problematic if you needed to select from possibly 100s of thousands or more records.&lt;/p&gt;

&lt;p&gt;Anyway, let's move to code. My &lt;code&gt;Banner&lt;/code&gt;s can be toggled off with property &lt;code&gt;is_active&lt;/code&gt; so I am using Django ORM &lt;code&gt;filter&lt;/code&gt; together with &lt;code&gt;values&lt;/code&gt; to pull out all the available ids. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;banner_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Banner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once that is done, we can check if the result is not empty and if not, utilize &lt;code&gt;choice&lt;/code&gt; form &lt;code&gt;random&lt;/code&gt; module to pick random element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;random&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;banner_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;random_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;banner_ids&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;banner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Banner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pk&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;random_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;Banner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DoesNotExist&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;banner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because the &lt;code&gt;banners_ids&lt;/code&gt; is basically "array" of key-value pairs, there is &lt;code&gt;["id"]&lt;/code&gt; subscript to pull out just the number. Once we have random id, we can use that to try to get record with the &lt;code&gt;get&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And that is all!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I think this is pretty good for my usecase and for many other assuming you don't have tons of data to work with.&lt;/p&gt;

&lt;p&gt;I have a few banners and the pages are cached so I don't almost have to even think about performance.&lt;/p&gt;

&lt;p&gt;I write about Django here and on my blog &lt;a href="https://nemecek.be"&gt;nemecek.be&lt;/a&gt;&lt;/p&gt;

</description>
      <category>django</category>
    </item>
    <item>
      <title>Fresh app ideas for iOS 14</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Tue, 21 Jul 2020 09:38:21 +0000</pubDate>
      <link>https://dev.to/nemecek_f/fresh-app-ideas-for-ios-14-3coc</link>
      <guid>https://dev.to/nemecek_f/fresh-app-ideas-for-ios-14-3coc</guid>
      <description>&lt;p&gt;The next iteration of Apple's mobile operating system brings with it a ton of great new frameworks and APIs to build new kinds of apps. New advances include the Vision framework that can now track human body movement and developers can also utilize the NearbyInteraction framework with the U1 chip available in the new phones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exercise coach app
&lt;/h2&gt;

&lt;p&gt;With the new Vision APIs that precisely track human body movement it makes sense to utilize this for a fitness app. The vision could be used to automatically track reps or warn when the user has incorrect posture. This could be especially useful for newcomers to yoga.&lt;/p&gt;

&lt;p&gt;Pair this witch speech synthesis and you can create fully automatic exercise coach. Then use HealthKit framework to record activity data. &lt;br&gt;
Check these WWDC 20 session for more information and inspiration: &lt;a href="https://developer.apple.com/videos/play/wwdc2020/10043/"&gt;Build an Action Classifier with Create ML&lt;/a&gt;, &lt;a href="https://developer.apple.com/videos/play/wwdc2020/10653"&gt;Detect Body and Hand Pose with Vision&lt;/a&gt; &amp;amp; &lt;a href="https://developer.apple.com/videos/play/wwdc2020/10099"&gt;Explore the Action &amp;amp; Vision app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sample code is available to get you up to speed quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Friend finder for busy places
&lt;/h2&gt;

&lt;p&gt;Ever spent too much time finding a friend in a huge crowd? With iOS 14 and the new &lt;code&gt;NearbyInteraction&lt;/code&gt; framework this problem could be solved with the powerful U1 chip in new iPhones.&lt;/p&gt;

&lt;p&gt;Once you establish session between two devices you get precise distance and direction information. You can use this to guide the user via arrow to their friend and save a lot of frustration.&lt;/p&gt;

&lt;p&gt;The session needs to be initialized before you can use the U1 chip so you could try creating a simple iMessage app or possibly your own server do facilitate this initial connections.&lt;/p&gt;

&lt;p&gt;Check out the session &lt;a href="https://developer.apple.com/videos/play/wwdc2020/10668/"&gt;Meet Nearby Interaction&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/nemecek-filip/App-ideas"&gt;You can find even more ideas in my GitHub repo!&lt;/a&gt; Stars appreacited 🙂
&lt;/h3&gt;

</description>
      <category>ios</category>
      <category>appideas</category>
      <category>ideas</category>
    </item>
    <item>
      <title>How to create RSS feed with HTML content in Django</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Sat, 04 Jul 2020 09:59:53 +0000</pubDate>
      <link>https://dev.to/nemecek_f/how-to-create-rss-feed-with-html-content-in-django-n87</link>
      <guid>https://dev.to/nemecek_f/how-to-create-rss-feed-with-html-content-in-django-n87</guid>
      <description>&lt;p&gt;I am not feeling super comfortable writing this blog post because I don't know much about setting up a RSS feed with Django and including HTML in the content but when I was trying to do this for my own blog I could not find any up-to-date tutorial showing how to properly include HTML content.&lt;/p&gt;

&lt;p&gt;I had various issues regarding encoding of the HTML entities so my content was full of stuff like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;amp;lt;p&amp;amp;gt;Hello&amp;amp;lt;/p&amp;amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And when I finally solved this issue I noticed that RSS readers would display the HTML in an "not-rendered" form so readers would see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"code"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;World!&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Instead of properly formatted text based on the HTML provided.&lt;/p&gt;

&lt;p&gt;As of right now &lt;a href="https://nemecek.be/blog/feed/rss"&gt;my RSS feed&lt;/a&gt; should be working pretty ok and I hope this little guide will save someone a bit of time when creating their own RSS feed with HTML content and fighting the escaping.&lt;/p&gt;

&lt;p&gt;Django has pretty good &lt;a href="https://docs.djangoproject.com/en/3.0/ref/contrib/syndication"&gt;docs about RSS&lt;/a&gt; which will get you started with creating the basic structure, setting up urls etc. But there is very little about providing your custom template and using HTML.&lt;/p&gt;

&lt;p&gt;Below is shortened example of my feed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPostsFeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Feed&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Filip Němeček Blog"&lt;/span&gt;
    &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://nemecek.be/blog/swift-and-ios"&lt;/span&gt;

    &lt;span class="n"&gt;description_template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'rss_feed.html'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The most important part there is the &lt;code&gt;description_template&lt;/code&gt; which tells Django where to find template file for individual RSS entries. The alternative is the &lt;code&gt;item_description&lt;/code&gt; method but I had zero luck using it with HTML. It works fine if you have plain content and can just pass it along.&lt;/p&gt;

&lt;p&gt;And finally let's have a look at my &lt;code&gt;rss_feed.html&lt;/code&gt; template file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jinja"&gt;&lt;code&gt;&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;autoescape&lt;/span&gt; &lt;span class="nv"&gt;off&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;![&lt;/span&gt;&lt;span class="na"&gt;CDATA&lt;/span&gt;&lt;span class="err"&gt;[&lt;/span&gt;
&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;obj.title&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;

&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;obj.html_content&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nf"&gt;safe&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;
&lt;span class="err"&gt;]]&lt;/span&gt;
&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;endautoescape&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I am pretty sure this is not the best solution out there but it works fine for me and I have a RSS feed with proper HTML rendering. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K7YLnU1A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K7YLnU1A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/image.png" alt="RSS feed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;!CDATA[&lt;/code&gt; wrapper is very important for RSS to display your HTML. But of course it was getting escaped so I turned escaping. The &lt;code&gt;obj&lt;/code&gt; is the item automatically passed into the template by Django.&lt;/p&gt;

&lt;p&gt;So in my case this is my blog post which has among other stuff a &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;html_content&lt;/code&gt; attributes.&lt;/p&gt;

&lt;p&gt;Please let me know if you have any questions or improvements regarding creating RSS feeds with Django.&lt;/p&gt;

&lt;p&gt;Btw there is &lt;a href="https://fakecake.org/myfeedsucks/"&gt;handy website&lt;/a&gt; to test your RSS feed.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>rss</category>
    </item>
    <item>
      <title>iOS 14: What is new for WKWebView</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Fri, 03 Jul 2020 19:13:45 +0000</pubDate>
      <link>https://dev.to/nemecek_f/ios-14-what-is-new-for-wkwebview-b5m</link>
      <guid>https://dev.to/nemecek_f/ios-14-what-is-new-for-wkwebview-b5m</guid>
      <description>&lt;p&gt;In this post let's go over some of the interesting changes for &lt;code&gt;WKWebView&lt;/code&gt; in iOS 14.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sandboxing for JavaScript
&lt;/h3&gt;

&lt;p&gt;There is new &lt;code&gt;WKContentWorld&lt;/code&gt; type to represent the separation of website's JavaScript and JavaScript that developers inject into the page. They can now run in completely separate environments.&lt;/p&gt;

&lt;p&gt;That means mainly that you don't have to worry about accidentally interacting with some website JS code and vice versa. So no naming clashes and also protections from malicious websites that may try to influence the app via JS communication.&lt;/p&gt;

&lt;p&gt;There are two content worlds defined and these are &lt;code&gt;page&lt;/code&gt; for the website environment and &lt;code&gt;defaultClient&lt;/code&gt; which should be the first choice when injecting JS into the page.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;WKContentWorld&lt;/code&gt; is a new parameter in methods like &lt;code&gt;evaluateJavaScript&lt;/code&gt; or &lt;code&gt;callAsyncJavaScript&lt;/code&gt; which we look at later in this post. This also applies to the &lt;code&gt;WKUserContentController&lt;/code&gt; which allows you to send data from JavaScript to your app. This can now be associated with content world. So for example you have multiple instances and each within its own content world to prevent interference. &lt;/p&gt;

&lt;h3&gt;
  
  
  Better JavaScript evaluation with &lt;code&gt;callAsyncJavaScript&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This new method will likely be the most used way for running JS code in &lt;code&gt;WKWebView&lt;/code&gt;. Mainly for two reasons. You can pass &lt;code&gt;arguments&lt;/code&gt; parameter which is a &lt;code&gt;[String: Any]&lt;/code&gt; dictionary and will be mapped to JS types. So you don't have to include those in the JavaScript string and can reuse it with different arguments.&lt;/p&gt;

&lt;p&gt;What does the "async" in the method name means? It is an indication that this method can handle JS promises which are one of the option how JS handles asynchronous code.  If your JS code returns promise then the &lt;code&gt;completionHandler&lt;/code&gt; of this method will not be called immediately and somehow return this promise but it will wait and you will get the value. This means &lt;code&gt;callAsyncJavaScript&lt;/code&gt; can be trivially used with &lt;code&gt;fetch&lt;/code&gt; to download any kind of data and then access it in Swift code.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;findString&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;As the name implies this method will let us find text in a webpage. You can do a bit of configuration or just use defaults that should work pretty well. The result is then selected and &lt;code&gt;WKWebView&lt;/code&gt; automatically scrolls so the result is visible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create PDF from page
&lt;/h3&gt;

&lt;p&gt;Since iOS 11 we can take a snapshot of currently visible part of the page with the &lt;code&gt;createPDF&lt;/code&gt; page which also lets us to configure the result we want.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;pageZoom&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;We can now easily zoom pages with this new property so we don't have rely on JavaScript to change the CSS to control zoom.&lt;/p&gt;

&lt;p&gt;This was just a sort of high-level overview. For more watch the &lt;a href="https://developer.apple.com/videos/play/wwdc2020/10188/"&gt;Discover WKWebView enhancements&lt;/a&gt; session.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: Not all the new stuff is available in Xcode 12 Beta 1.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>ios14</category>
    </item>
    <item>
      <title>Checking out the new PHPickerViewController in iOS 14</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Thu, 25 Jun 2020 13:04:48 +0000</pubDate>
      <link>https://dev.to/nemecek_f/checking-out-the-new-phpickerviewcontroller-in-ios-14-5d8h</link>
      <guid>https://dev.to/nemecek_f/checking-out-the-new-phpickerviewcontroller-in-ios-14-5d8h</guid>
      <description>&lt;p&gt;In iOS 14 there is a brand new way to let users select a photo(s) or video(s) from their library to use in your app and it has some cool features and benefits. Let's see what these are and how basic &lt;code&gt;PHPickerViewController&lt;/code&gt; usage looks like.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;No permissions required&lt;/strong&gt; - you don't need to provide usage description in &lt;code&gt;Info.plist&lt;/code&gt; to present the new picker and system does not show the allow access dialog. That is because users have full control over what media they select and subsequently send to your app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modern UI consistent with Photos&lt;/strong&gt; - the new picker is much easier to use because the UI is very similar to what you get in Photos and there is also a search. This should be much better selection experience for the users than with the &lt;code&gt;UIImagePickerViewController&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multi-selection&lt;/strong&gt; - what else to say? 🙂&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage overview
&lt;/h3&gt;

&lt;p&gt;Using the new &lt;code&gt;PHPickerViewController&lt;/code&gt; is pretty straightforward. It uses the traditional delegate model that will alert you when user finishes her selection. There is just a single delegate method to support and a bit of configuration.&lt;/p&gt;

&lt;p&gt;Start by importing the &lt;code&gt;PhotosUI&lt;/code&gt; framework:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;PhotosUI&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then continue with creating the configuration object &lt;code&gt;PHPickerConfiguration&lt;/code&gt;. So far this allows to customize how many media files user can select and what kind of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;configuration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PHPickerConfiguration&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;selectionLimit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;images&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example we are setting the &lt;code&gt;selectionLimit&lt;/code&gt; to 3 (default is 1). You can also set &lt;code&gt;0&lt;/code&gt; to mean unlimited. And next with &lt;code&gt;filter&lt;/code&gt; we are saying that we want just images.&lt;/p&gt;

&lt;p&gt;You can also ask for &lt;code&gt;videos&lt;/code&gt; or &lt;code&gt;livePhotos&lt;/code&gt; and combine these with the &lt;code&gt;any(of:&lt;/code&gt; method like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;livePhotos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;images&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next create the picker itself and set yourself as its &lt;code&gt;delegate&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;picker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;PHPickerViewController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have two things left. Present the picker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The UI is different depending if multi-selection is enabled or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rYMhAe86--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/photo-picker.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rYMhAe86--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/photo-picker.png" alt="The new PHPhotoPickerViewController"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And implement the delegate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;ViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;PHPickerViewControllerDelegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;PHPickerViewController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didFinishPicking&lt;/span&gt; &lt;span class="nv"&gt;results&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;PHPickerResult&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;dismiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So far we are just dismissing the picker and checking that we have results.&lt;br&gt;
Unlike with &lt;code&gt;UIImagePickerViewController&lt;/code&gt; we won't get the URL path to the file but instead a &lt;code&gt;NSItemProvider&lt;/code&gt; that we can ask to load a specific object. For images this may look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;canLoadObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ofClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ofClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
             &lt;span class="kt"&gt;DispatchQueue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="kt"&gt;UIImage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                      &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;
                 &lt;span class="p"&gt;}&lt;/span&gt;
             &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Just to be safe we first ask if the object can be loaded and then load it. To be honest I am not that well versed with &lt;code&gt;NSItemProvider&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;UIImagePickerViewController&lt;/code&gt; there was a settings to specify the resolution for exported media which here does not appear to be a case. I plan to investigate this further and either update this post or write new one detailing how to get video or image with specific resolution.&lt;/p&gt;

&lt;p&gt;In the WWDC 20 session about this new photo picker Apple specifically mentions that we should be using this new one instead of the older &lt;code&gt;UIImagePickerViewController&lt;/code&gt;. This is not deprecated yet but presets for image/video quality are as per new documentation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T1aMv1Ob--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/Screenshot_2020-06-25_at_13.56.57.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T1aMv1Ob--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/Screenshot_2020-06-25_at_13.56.57.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>ios14</category>
    </item>
    <item>
      <title>First look at the new UIColorPickerViewController in iOS 14</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Tue, 23 Jun 2020 13:57:18 +0000</pubDate>
      <link>https://dev.to/nemecek_f/first-look-at-the-new-uicolorpickerviewcontroller-in-ios-14-4ak0</link>
      <guid>https://dev.to/nemecek_f/first-look-at-the-new-uicolorpickerviewcontroller-in-ios-14-4ak0</guid>
      <description>&lt;p&gt;With iOS 14 we can use the new &lt;code&gt;UIColorPickerViewController&lt;/code&gt; to let user select color. In this post let's briefly check how to use it. You of course need the new Xcode 12 to be able to try this example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Showing the color picker
&lt;/h3&gt;

&lt;p&gt;The new &lt;code&gt;UIColorPickerViewController&lt;/code&gt; works basically in the same way like other pickers and pre-made controllers. We create an instance, set ourselves as a &lt;code&gt;delegate&lt;/code&gt; and &lt;code&gt;present&lt;/code&gt; the picker like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;picker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIColorPickerViewController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;
&lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that you should not wrap the picker in &lt;code&gt;UINavigationController&lt;/code&gt; I tried it and got very strange behaviour with missing background. Also since the picker has its own navbar I think that is the indication it should not be used with  &lt;code&gt;UINavigationController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once you present the controller, you get UI like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qPk-tOaz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/UIColorPickerViewController.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qPk-tOaz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/UIColorPickerViewController.png" alt="UIColorPickerViewController screenshots"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are three ways how to pick a color with the option to also select opacity. This can be customized with the &lt;code&gt;supportsAlpha&lt;/code&gt; property. If set to &lt;code&gt;false&lt;/code&gt; you cannot change opacity and cannot select color with alpha component. So far that is the only customization we have.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing the delegate
&lt;/h3&gt;

&lt;p&gt;To get notified about user's color selection we need to implement the &lt;code&gt;UIColorPickerViewControllerDelegate&lt;/code&gt;. These are two methods. One informs us about new color selection and the other about user closing the picker.&lt;/p&gt;

&lt;p&gt;The basic implementation could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;colorPickerViewControllerDidFinish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;viewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIColorPickerViewController&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;dismiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;colorPickerViewControllerDidSelectColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;viewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIColorPickerViewController&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;selectedColor&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that is our first look at &lt;code&gt;UIColorPickerViewController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://apps.apple.com/app/scan-it-scan-and-export-pdf/id1509634112"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1NnGswXk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://nemecek.be/media/images/scanItWideBanner.png" alt="Scan it - wide banner"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>ios14</category>
      <category>swift</category>
    </item>
    <item>
      <title>iOS: How to save event into user's calendar</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Sat, 20 Jun 2020 15:52:21 +0000</pubDate>
      <link>https://dev.to/nemecek_f/ios-how-to-save-event-into-user-s-calendar-1e48</link>
      <guid>https://dev.to/nemecek_f/ios-how-to-save-event-into-user-s-calendar-1e48</guid>
      <description>&lt;p&gt;In this post we will use the EventKit framework to save event into user's default calendar.&lt;/p&gt;

&lt;p&gt;If your app deals with some kind of events you can make it easy for the user to save particular events into their calendar. This way they won't have to remember be and can plan accordingly.&lt;/p&gt;

&lt;p&gt;Working with calendars in iOS is pretty good thanks to the &lt;code&gt;EventKit&lt;/code&gt; framework. In this post we will focus on the minimal example to save an event into user's calendar.&lt;/p&gt;

&lt;p&gt;As with other private stuff, the first step is to configure &lt;code&gt;Info.plist&lt;/code&gt; and then requesting access.&lt;/p&gt;

&lt;p&gt;To save an event you need to add the key &lt;code&gt;NSCalendarsUsageDescription&lt;/code&gt; to &lt;code&gt;Info.plist&lt;/code&gt; with explanation why you need calendar access.&lt;/p&gt;

&lt;p&gt;Once that is done we can move to Swift code and start by importing &lt;code&gt;EventKit&lt;/code&gt; and also creating an instance of &lt;code&gt;EKEventStore&lt;/code&gt; which is used to manage events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;EventKit&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;eventStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;EKEventStore&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once &lt;code&gt;eventStore&lt;/code&gt; is created, we can move to requesting access to the calendar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;requestAccess&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;eventStore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requestAccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;granted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;granted&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// save event&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I would recommend checking access often because user can change their mind and remove access in Settings.&lt;/p&gt;

&lt;p&gt;If access was &lt;code&gt;granted&lt;/code&gt; we can save the event. In case of touching UI somehow we would need &lt;code&gt;DispatchQueue.main.async&lt;/code&gt; to make sure we are on the main thread.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;eventStore&lt;/code&gt; has property &lt;code&gt;defaultCalendarForNewEvents&lt;/code&gt; we can use to get user's default calendar. This is quite sensible default for new events and we don't need to prompt the user to choose calendar.&lt;/p&gt;

&lt;p&gt;However in the real app it would be a good idea to have some sort of fallback. At least showing an alert explaining that there is not a default calendar or better using &lt;code&gt;EKCalendarChooser&lt;/code&gt; to let user select an alternative. I have a &lt;a href="https://nemecek.be/blog/16/how-to-use-ekcalendarchooser-in-swift-to-let-user-select-calendar-in-ios"&gt;post about it also&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Assuming we have a calendar, we can create an event using our &lt;code&gt;eventStore&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;calendar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;eventStore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;defaultCalendarForNewEvents&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;       
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;EKEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;eventStore&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;eventStore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Saving an event is done via &lt;code&gt;EKEventStore&lt;/code&gt; so we need to associate it. &lt;/p&gt;

&lt;p&gt;Next step is to configure the &lt;code&gt;EKEvent&lt;/code&gt; with data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"This is my test event"&lt;/span&gt;
&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Calendar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;byAdding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isAllDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;endDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startDate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that the &lt;code&gt;endDate&lt;/code&gt; is required even when you want event for just one day. Without it you will get an error trying to save.&lt;/p&gt;

&lt;p&gt;Next we assign the calendar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;calendar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calendar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And lastly we save the event:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;eventStore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;span&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;thisEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;span&lt;/code&gt; argument is required even though it does not really make sense for non-recurring events. &lt;code&gt;commit&lt;/code&gt; means that the event will get saved immediately.&lt;/p&gt;

&lt;p&gt;Alternative would be to commit later via dedicated method. This is useful when you are saving multiple events:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;eventStore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;span&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;thisEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;eventStore&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And done! If you run the above code, check your calendar for new event 🙂&lt;/p&gt;

&lt;p&gt;If you are interested in more EventKit you can check my other posts or &lt;a href="https://github.com/nemecek-filip/EKEventKit.Example"&gt;open-source project&lt;/a&gt; showcasing working with different parts of the framework.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>iOS: How to let user select video from their library</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Fri, 19 Jun 2020 14:16:44 +0000</pubDate>
      <link>https://dev.to/nemecek_f/ios-how-to-let-user-select-video-from-their-library-8oj</link>
      <guid>https://dev.to/nemecek_f/ios-how-to-let-user-select-video-from-their-library-8oj</guid>
      <description>&lt;p&gt;Despite its name &lt;code&gt;UIImagePickerController&lt;/code&gt; can be also used to let user select video to import it to your app. It requires just a bit of configuration of the picker.&lt;/p&gt;

&lt;p&gt;I am going to split the creation and configuration into multiple parts for clarity.&lt;/p&gt;

&lt;p&gt;First we create the instance and assign &lt;code&gt;self&lt;/code&gt; as a delegate which will let us dismiss the picker once selection is done and access the video.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;picker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIImagePickerController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The delegate requires conformance to &lt;code&gt;UIImagePickerControllerDelegate&lt;/code&gt; and &lt;code&gt;UINavigationControllerDelegate&lt;/code&gt;. We will implement those methods later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Media types configuration
&lt;/h3&gt;

&lt;p&gt;Next we need to tell the picker what media types are we interested in and where it should look for them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sourceType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;photoLibrary&lt;/span&gt;
&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mediaTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIImagePickerController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;availableMediaTypes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;photoLibrary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mediaTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"public.movie"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There is &lt;code&gt;?? []&lt;/code&gt; fallback because the &lt;code&gt;UIImagePickerController.availableMediaTypes&lt;/code&gt; method returns optional string array. You may consider adding &lt;code&gt;assert&lt;/code&gt; below to check we got &lt;code&gt;mediaTypes&lt;/code&gt; just to avoid surprises in the future.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resolution configuration
&lt;/h3&gt;

&lt;p&gt;And now we specify the quality and resolution because the picker does video compression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;videoQuality&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;typeHigh&lt;/span&gt;
&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;videoExportPreset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;AVAssetExportPresetHEVC1920x1080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you specify lower &lt;code&gt;videoQuality&lt;/code&gt; then during the compression it will take that into an account. The &lt;code&gt;videoExportPreset&lt;/code&gt; is super important because otherwise we would get 720p video.&lt;/p&gt;

&lt;p&gt;The available presets are listed in the &lt;a href="https://developer.apple.com/documentation/avfoundation/avassetexportsession/export_preset_names_for_quicktime_files_of_a_given_size"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For 4K we would use &lt;code&gt;AVAssetExportPresetHEVC3840x2160&lt;/code&gt; or possibly &lt;code&gt;AVAssetExportPresetHEVCHighestQuality&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And that is basic configuration done.&lt;/p&gt;

&lt;p&gt;It may be a good idea to allow editing with this parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;allowsEditing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we just need to present the picker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  The delegate methods
&lt;/h2&gt;

&lt;p&gt;Let's start with the easier method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;imagePickerControllerDidCancel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIImagePickerController&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;dismiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is called when user cancels the selection, so we dismiss the picker.&lt;/p&gt;

&lt;p&gt;When user selects video, we get this callback:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;imagePickerController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;picker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIImagePickerController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didFinishPickingMediaWithInfo&lt;/span&gt; &lt;span class="nv"&gt;info&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;UIImagePickerController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;InfoKey&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;dismiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;movieUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mediaURL&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="kt"&gt;URL&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// work with the video URL&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once again we dismiss and then attempt to get video URL from the &lt;code&gt;info&lt;/code&gt; dictionary provided.&lt;/p&gt;

&lt;p&gt;It is a good idea to copy the video somewhere else and not hold onto the original URL.&lt;/p&gt;

&lt;p&gt;And that is basic usage of &lt;code&gt;UIImagePickerController&lt;/code&gt; to get video from user's library in Full HD or even 4K if we want to.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>Django: Add tweet button with plain HTML/CSS, no JavaScript</title>
      <dc:creator>Filip Němeček</dc:creator>
      <pubDate>Wed, 17 Jun 2020 17:34:35 +0000</pubDate>
      <link>https://dev.to/nemecek_f/django-add-tweet-button-with-plain-html-css-no-javascript-4bcf</link>
      <guid>https://dev.to/nemecek_f/django-add-tweet-button-with-plain-html-css-no-javascript-4bcf</guid>
      <description>&lt;p&gt;Do you want to have "Tweet" button on your personal site without the need to use Twitter JavaScript library? You can, and it only takes a bit of code. Twitter supports special "intent" url which can be configured to pre-populate new tweet on Twitter.com.&lt;/p&gt;

&lt;p&gt;In this post I will show you how I integrated this into my site to give the readers option to quickly tweet my posts if they choose. I am so far not using any JavaScript because I don't need it and I didn't want to change this just for Twitter.&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;p&gt;The first step is to prepare the url in Django and send it to template where it can be rendered inside &lt;code&gt;a&lt;/code&gt; tag.&lt;br&gt;
I have created this handy helper method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_tweet_intent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'https://nemecek.be{0}'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_absolute_url&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quote_plus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;tweet_intent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://twitter.com/intent/tweet?text={0}&amp;amp;url={1}&amp;amp;via=nemecek_f&amp;amp;hashtags={2}"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                                                                                         &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hashtags&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;tweet_intent&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It expects instance of &lt;code&gt;BlogPost&lt;/code&gt; which is my model class for posts and returns tweet_intent as a string.&lt;/p&gt;

&lt;p&gt;I am using the &lt;code&gt;urlib.parse.quote&lt;/code&gt; method to correctly encode the url and also the &lt;code&gt;quote_plus&lt;/code&gt; variant to encode title. The difference is that the "plus" version also encodes spaces.&lt;/p&gt;

&lt;p&gt;Then it is just a matter of constructing the url.&lt;/p&gt;

&lt;p&gt;The format is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://twitter.com/intent/tweet?text={tweet text}&amp;amp;url={url to link to}&amp;amp;via={ username or user id}&amp;amp;hashtags={ hashtags separated by comas}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also check out the official &lt;a href="https://developer.twitter.com/en/docs/twitter-for-websites/tweet-button/guides/web-intent"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you send it to the template as &lt;code&gt;tweet_intent&lt;/code&gt; or anything else you want, you can create the element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jinja"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"tweet-button"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;tweet_intent&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="na"&gt;target=&lt;/span&gt;&lt;span class="s"&gt;"_blank"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Tweet&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And possibly some styles 🙂&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.tweet-button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#1DA1F2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The background color is the "Twitter blue".&lt;/p&gt;

&lt;p&gt;And that is how you can create Tweet button with Django, HTML and CSS.&lt;/p&gt;

</description>
      <category>django</category>
      <category>html</category>
      <category>css</category>
      <category>twitter</category>
    </item>
  </channel>
</rss>
