<?xml version="1.0" encoding="utf-8"?>





<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en" xml:base="https://www.obocat.com/">
  <title type="text">Daniel Toranzo Pérez — Blog (EN)</title>
  <subtitle type="text">Notes on iOS development, architecture, performance and the decisions behind the products I work on.</subtitle>
  <link href="https://www.obocat.com/feed-en.xml" rel="self" type="application/atom+xml"/>
  <link href="https://www.obocat.com/blog/" rel="alternate" type="text/html" hreflang="en"/>
  <link href="https://www.obocat.com/feed-es.xml" rel="alternate" type="application/atom+xml" hreflang="es"/>
  <id>https://www.obocat.com/feed-en.xml</id>
  <updated>2026-11-23T08:00:00+00:00</updated>
  <author>
    <name>Daniel Toranzo Pérez</name>
    <uri>https://www.obocat.com/</uri>
  </author>
  <rights>© 2026 Daniel Toranzo Pérez</rights>
  <logo>https://www.obocat.com/images/og-card.png?v=2</logo>
  <icon>https://www.obocat.com/favicon.png</icon>
  <generator uri="https://jekyllrb.com/">Jekyll</generator>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">autoreleasepool in Swift: when ARC is not enough</title>
    <link href="https://www.obocat.com/blog/2026/11/23/autoreleasepool-swift/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/11/23/autoreleasepool-swift/</id>
    <published>2026-11-23T08:00:00+00:00</published>
    <updated>2026-11-23T08:00:00+00:00</updated>
    <summary type="text">Why a loop using Objective-C APIs can spike memory even with ARC, and how to use autoreleasepool to bound the peak.</summary>
    
    <category term="iOS"/>
    
    <category term="Swift"/>
    
    <category term="Memory"/>
    
    <category term="ARC"/>
    
    <category term="Performance"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/11/23/autoreleasepool-swift/">&lt;div class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;

  &lt;p&gt;Imagine a folder with thousands of photos. Your code walks the files, loads each image, compresses it again, and saves the result. Each iteration is independent, so you would expect memory use to stay roughly flat.&lt;/p&gt;

  &lt;p&gt;But the opposite happens: memory keeps growing until iOS closes the app.&lt;/p&gt;

  &lt;p&gt;There is no obvious retain cycle (a cycle of strong references) and no array accumulating every image. The problem is one layer down: Objective-C’s autorelease pool.&lt;/p&gt;

  &lt;p&gt;This is the pattern that usually shows up:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;UIKit&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reencodeImagesWithoutPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;urls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;URL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;directory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;URL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urls&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;image&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;UIImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;contentsOfFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
              &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;jpegData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;compressionQuality&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;filename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;deletingPathExtension&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lastPathComponent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.jpg&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;directory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;appendingPathComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;At first glance, the code looks correct. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt; are local references; when the iteration ends, they should not stay alive. But ARC is not the only memory system at work here.&lt;/p&gt;

  &lt;h2 id=&quot;what-arc-actually-guarantees&quot;&gt;What ARC actually guarantees&lt;/h2&gt;

  &lt;p&gt;In Swift, an object starts living at initialization, and its guaranteed lifetime ends at its last use. The compiler inserts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;retain&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; operations; depending on optimization, the object may be released right after that last use or later. What it guarantees is that the object stays alive until that point, not that it is released exactly then. This is different from C++, where the closing brace marks the end of the object’s lifetime.&lt;/p&gt;

  &lt;p&gt;Apple explains this in the &lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2021/10216/&quot;&gt;ARC in Swift: Basics and beyond&lt;/a&gt; session from WWDC21. Saying that “Swift releases objects exactly when they stop being needed” is only half the story. ARC usually releases objects close to their last use, but it is not a garbage collector and it does not guarantee release at that exact moment.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--note&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Nuance:&lt;/strong&gt; ARC guarantees that an object lives at least until its last use. It does not guarantee that it dies exactly there. And objects that Objective-C sends to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autorelease&lt;/code&gt; do not follow Swift’s ARC rules.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;For pure Swift objects, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoreleasepool&lt;/code&gt; usually does nothing: there is no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autorelease&lt;/code&gt; to drain. The problem appears when the loop calls Foundation or UIKit APIs that are still Objective-C underneath.&lt;/p&gt;

  &lt;h2 id=&quot;the-objective-c-layer-that-is-still-involved&quot;&gt;The Objective-C layer that is still involved&lt;/h2&gt;

  &lt;p&gt;When an object receives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autorelease&lt;/code&gt;, its release is postponed. Instead of being released immediately, it is recorded in the current pool. When the pool drains, the object receives a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt; for every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autorelease&lt;/code&gt; it received inside that block.&lt;/p&gt;

  &lt;p&gt;UIKit and AppKit create a pool at the beginning of every run loop iteration and drain it at the end. That is why, in a normal interface action, you do not have to worry: the system is already doing the work. The problem appears when the entire loop runs inside a single run loop iteration. All those objects accumulate in the pool until the batch ends and the pool drains.&lt;/p&gt;

  &lt;p&gt;Apple’s memory guide says it explicitly: if you write a loop that creates many temporary objects, you can use a pool inside the loop to reduce the peak memory footprint. It also recommends creating one when you start a secondary thread. This is not a manual-memory-management relic: Foundation and UIKit are still largely Objective-C, and some modern APIs bridge to Objective-C depending on the OS version or the arguments they receive.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Careful:&lt;/strong&gt; there is no reliable list of which APIs use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autorelease&lt;/code&gt; internally. Even an apparently Swift API such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Data(contentsOf:)&lt;/code&gt; has behaved differently across versions. The rule is not to memorize names: it is to measure and bound the scope when you see the pattern.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;On Darwin, Swift Concurrency wraps every job (each unit of work) in a pool, and RunLoop does the same; GCD lets you configure the pool frequency per queue, including one pool per block. &lt;a href=&quot;https://forums.swift.org/t/is-autoreleasepool-still-needed-with-the-new-foundation/83224&quot;&gt;David Smith summarizes it on Swift Forums&lt;/a&gt;. That does not save you if a long job contains a loop that creates thousands of temporaries: the pool associated with that job does not drain until the job finishes.&lt;/p&gt;

  &lt;h2 id=&quot;the-solution-one-pool-per-iteration&quot;&gt;The solution: one pool per iteration&lt;/h2&gt;

  &lt;p&gt;The version with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoreleasepool&lt;/code&gt; changes very little:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;UIKit&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reencodeImages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;urls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;URL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;directory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;URL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urls&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;autoreleasepool&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;image&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;UIImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;contentsOfFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;jpegData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;compressionQuality&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;filename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;deletingPathExtension&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lastPathComponent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.jpg&quot;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;directory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;appendingPathComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;All the work for that iteration stays inside the pool. When the block ends, the autoreleased objects from that iteration receive their &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;release&lt;/code&gt;. If an image cannot be loaded or its data cannot be generated, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt; exits only the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoreleasepool&lt;/code&gt; closure; the loop continues with the next URL. If the body throws, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;try&lt;/code&gt; propagates the error; you do not need a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do/catch&lt;/code&gt; just for the pool.&lt;/p&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoreleasepool&lt;/code&gt; is not a keyword: it is a function from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ObjectiveC&lt;/code&gt; module that Foundation and UIKit re-export. In an app with UIKit it is already available; in a standalone file you need &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import Foundation&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import UIKit&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import ObjectiveC&lt;/code&gt;.&lt;/p&gt;

  &lt;p&gt;The pool goes &lt;strong&gt;inside&lt;/strong&gt; the loop, not around it. This pseudocode does not help:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;autoreleasepool&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urls&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;There, the pool drains at the end of the batch, exactly when it no longer matters. The unit you want to bound is the iteration, not the whole operation.&lt;/p&gt;

  &lt;p&gt;If you need to return a result, keep it small:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;UIKit&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;imageSizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;urls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;URL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;CGSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;urls&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compactMap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;autoreleasepool&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;UIImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;contentsOfFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;If you return the image or the large &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Data&lt;/code&gt;, the pool cannot perform magic: the value stays alive in the outer scope. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoreleasepool&lt;/code&gt; releases temporaries, not strong references that you keep.&lt;/p&gt;

  &lt;h2 id=&quot;what-autoreleasepool-does-not-fix&quot;&gt;What autoreleasepool does not fix&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;An array that accumulates every image.&lt;/strong&gt; The array is a strong reference; the peak is yours. The pool does not reduce memory for something you keep holding.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;A retain cycle.&lt;/strong&gt; The object never goes away; the pool does not break cycles.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;A cache.&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIImage(named:)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSCache&lt;/code&gt;, and friends retain objects on purpose.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;A pool around the whole loop.&lt;/strong&gt; It drains too late.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Pure Swift code.&lt;/strong&gt; If there is no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autorelease&lt;/code&gt;, there is nothing to drain.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;A real ImageIO or UIKit leak.&lt;/strong&gt; If the peak continues after the pool, you need the memory graph and Instruments.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;h2 id=&quot;summary-when-to-use-autoreleasepool&quot;&gt;Summary: when to use autoreleasepool&lt;/h2&gt;

  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Situation&lt;/th&gt;
        &lt;th&gt;What to do&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;A loop with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIImage&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSImage&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSAttributedString&lt;/code&gt;, or similar&lt;/td&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoreleasepool&lt;/code&gt; per iteration&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;A pure Swift model or struct&lt;/td&gt;
        &lt;td&gt;Nothing; ARC handles it&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;An array that retains every result&lt;/td&gt;
        &lt;td&gt;Redesign; the pool does not reduce the peak&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;A long custom thread&lt;/td&gt;
        &lt;td&gt;Pool at the start and/or per iteration&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;A retain cycle or a cache&lt;/td&gt;
        &lt;td&gt;Memory graph; the pool does not fix it&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;A peak measured in Instruments&lt;/td&gt;
        &lt;td&gt;Try the pool and measure again&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;h2 id=&quot;how-to-verify-it&quot;&gt;How to verify it&lt;/h2&gt;

  &lt;p&gt;Start with Xcode’s memory report: it shows the shape of the peak and whether it grows with the batch. To find out who is causing it, open Instruments and use the Allocations instrument. Mark generations before and after one iteration or one batch, and compare with and without the pool. What you are looking for is not only memory retained at the end, but the transient peak that can make iOS terminate the app.&lt;/p&gt;

  &lt;p&gt;If you want an automated check, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XCTMemoryMetric&lt;/code&gt; measures memory use during a block. It is useful for catching retained-memory regressions, but for transient peaks the Allocations instrument is more direct. Do not set a threshold based on a single run: memory varies across devices and OS versions.&lt;/p&gt;

  &lt;p&gt;And do not optimize blindly. Creating and draining an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoreleasepool&lt;/code&gt; has a small cost. Use it when you have many Objective-C temporary objects, not indiscriminately in every function.&lt;/p&gt;

  &lt;h2 id=&quot;the-key-takeaway&quot;&gt;The key takeaway&lt;/h2&gt;

  &lt;p&gt;Swift ARC does a great job managing memory, but it does not replace Objective-C’s memory-management mechanism.&lt;/p&gt;

  &lt;p&gt;When a loop creates many temporary objects from Objective-C APIs, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoreleasepool&lt;/code&gt; lets you limit how long they stay accumulated in memory.&lt;/p&gt;

  &lt;p&gt;The next time you see memory use grow during batch processing, do not add a pool blindly. First measure, check which objects are still alive, and look for possible strong references. If the problem is in autoreleased objects, a per-iteration pool can be the difference between processing thousands of images successfully and running out of memory.&lt;/p&gt;

  &lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2021/10216/&quot;&gt;ARC in Swift: Basics and beyond (WWDC21)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html&quot;&gt;Using Autorelease Pool Blocks&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/foundation/nsautoreleasepool&quot;&gt;NSAutoreleasePool&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/objectivec/autoreleasepool%28invoking:%29&quot;&gt;autoreleasepool(invoking:)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://forums.swift.org/t/the-role-of-autoreleasepool-in-swift-and-thoughts-about-memory-management-in-swift/52976&quot;&gt;The role of autoreleasepool in Swift (Swift Forums)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://forums.swift.org/t/is-autoreleasepool-still-needed-with-the-new-foundation/83224&quot;&gt;Is autoreleasepool still needed with the new Foundation? (Swift Forums)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://swiftrocks.com/autoreleasepool-in-swift&quot;&gt;autoreleasepool uses in Swift&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/xcode/gathering-information-about-memory-use&quot;&gt;Gathering information about memory use&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/xctest/xctmemorymetric&quot;&gt;XCTMemoryMetric&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

&lt;/div&gt;
</content>
  </entry>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">AttributedString: formatting, links, and VoiceOver in SwiftUI</title>
    <link href="https://www.obocat.com/blog/2026/11/19/attributedstring-enlaces-voiceover/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/11/19/attributedstring-enlaces-voiceover/</id>
    <published>2026-11-19T08:00:00+00:00</published>
    <updated>2026-11-19T08:00:00+00:00</updated>
    <summary type="text">How to format text with AttributedString, handle links with OpenURLAction, and make VoiceOver recognize them as links.</summary>
    
    <category term="iOS"/>
    
    <category term="SwiftUI"/>
    
    <category term="AttributedString"/>
    
    <category term="Accessibility"/>
    
    <category term="VoiceOver"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/11/19/attributedstring-enlaces-voiceover/">&lt;div class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;

  &lt;p&gt;A paragraph with bold, italic, a code fragment, and two links sounds like a solved problem. Then VoiceOver has to read it as what it is: text with emphasis and links, not one flat sentence.&lt;/p&gt;

  &lt;p&gt;The problem is not just painting the text. A link has to remain a link when VoiceOver discovers it, when the text size changes, or when the paragraph is combined with other elements.&lt;/p&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AttributedString&lt;/code&gt; does not remove every sharp edge, but it brings the pieces into one value. More importantly, it lets formatting carry meaning, not just appearance.&lt;/p&gt;

  &lt;h2 id=&quot;two-layers-how-it-looks-and-what-it-is&quot;&gt;Two layers: how it looks and what it is&lt;/h2&gt;

  &lt;p&gt;The classic mistake is treating a link as blue, underlined text. To your eyes, that is a link. To VoiceOver, it is not. What turns a run into a link is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.link&lt;/code&gt; attribute, not the color.&lt;/p&gt;

  &lt;p&gt;The same applies to bold and italic. You can paint a word with a specific font, or you can mark that the word is emphasized. The first changes appearance; the second can also change how the text is interpreted.&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SwiftUI&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ArticleText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AttributedString&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AttributedString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;AttributedString can mix formatting, links, and semantics in a single value.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;formatting&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inlinePresentationIntent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stronglyEmphasized&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;links&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;link&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;URL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;https://developer.apple.com/documentation/foundation/attributedstring&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;underlineStyle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;single&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foregroundColor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;accentColor&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;tint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;accentColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#Preview {&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;ArticleText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inlinePresentationIntent&lt;/code&gt; expresses intent — strong emphasis, emphasis, code — and SwiftUI decides how to render it. If you need a specific font, you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.font&lt;/code&gt;, but for reading text it is better to start from semantic styles such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.body&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.headline&lt;/code&gt; so Dynamic Type keeps working.&lt;/p&gt;

  &lt;p&gt;The non-negotiable attribute is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.link&lt;/code&gt;. Underline and color help people see it, but they do not make it a link. Remove &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.link&lt;/code&gt; and keep the blue color, and you have pretty text with no link at all.&lt;/p&gt;

  &lt;figure class=&quot;post-figure&quot;&gt;
  &lt;img src=&quot;https://res.cloudinary.com/lo8bck4i/image/upload/v1794900928/blog/attributedstring/jv9vtbrthajrslammpax.png&quot; alt=&quot;Screenshot of the sample app with the AttributedString title, formatted text, and an underlined link.&quot; /&gt;
  &lt;figcaption&gt;An example view with semantic formatting and a real link, on an iPhone 15 Pro.&lt;/figcaption&gt;
&lt;/figure&gt;

  &lt;h2 id=&quot;markdown-as-a-starting-point&quot;&gt;Markdown as a starting point&lt;/h2&gt;

  &lt;p&gt;If the text already arrives as Markdown, you do not need to walk ranges by hand:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Foundation&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;makeMarkdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AttributedString&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;markdown&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;**Important:** read the [AttributedString guide](https://developer.apple.com/documentation/foundation/attributedstring).&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;options&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AttributedString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;MarkdownParsingOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;interpretedSyntax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inlineOnlyPreservingWhitespace&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AttributedString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;markdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;markdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inlineOnlyPreservingWhitespace&lt;/code&gt; parses Markdown as inline text and preserves line breaks. It is useful for paragraphs that come from an API, a note, or a text field.&lt;/p&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt; also accepts Markdown directly in a string. The difference is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AttributedString&lt;/code&gt; leaves the value in your hands: you can add attributes, replace a link, combine fragments, or pass it to another layer before rendering it.&lt;/p&gt;

  &lt;h2 id=&quot;links-that-behave-like-links&quot;&gt;Links that behave like links&lt;/h2&gt;

  &lt;p&gt;In SwiftUI, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt; backed by an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AttributedString&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.link&lt;/code&gt; is already interactive. You do not need a gesture on the text or code that works out which word was tapped.&lt;/p&gt;

  &lt;p&gt;If you want to decide what happens to some links, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OpenURLAction&lt;/code&gt;:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SwiftUI&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;LinkHandlingView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AttributedString&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(\&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openURL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;OpenURLAction&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scheme&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;myapp&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;systemAction&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

                &lt;span class=&quot;c1&quot;&gt;// Open an internal screen or record the event.&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handled&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.systemAction&lt;/code&gt; lets the system open the link: Safari, Mail, or the matching app.&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.handled&lt;/code&gt; means you have already handled it.&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.discarded&lt;/code&gt; ignores it.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;For internal actions, a custom scheme such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myapp://glossary/attributedstring&lt;/code&gt; is enough. For ordinary web content, let the system do its job. Do not turn every link into an internal action just because you can.&lt;/p&gt;

  &lt;h2 id=&quot;voiceover-the-link-has-to-exist-in-the-tree&quot;&gt;VoiceOver: the link has to exist in the tree&lt;/h2&gt;

  &lt;p&gt;When &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt; finds a run with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.link&lt;/code&gt;, it exposes it as a link. VoiceOver announces it as a link and adds it to the links rotor. You do not need to add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.accessibilityAddTraits(.isLink)&lt;/code&gt; to the whole paragraph; in fact, it can backfire.&lt;/p&gt;

  &lt;p&gt;There are three common ways to break it:&lt;/p&gt;

  &lt;ol&gt;
    &lt;li&gt;Paint the text blue and underline it without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.link&lt;/code&gt;.&lt;/li&gt;
    &lt;li&gt;Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.onTapGesture&lt;/code&gt; on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt; to open a screen.&lt;/li&gt;
    &lt;li&gt;Apply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.accessibilityElement(children: .combine)&lt;/code&gt; or an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.accessibilityLabel&lt;/code&gt; to the entire paragraph.&lt;/li&gt;
  &lt;/ol&gt;

  &lt;p&gt;All three collapse the paragraph into a single stop. It still looks like a link; to VoiceOver, it is no longer one.&lt;/p&gt;

  &lt;p&gt;Link text matters too. “Here” or “more information” says nothing out of context. “AttributedString guide” does. VoiceOver lets people move through links with the rotor; if every link is called “here”, the rotor is useless.&lt;/p&gt;

  &lt;p&gt;Test with the links rotor, not just by reading the paragraph from top to bottom. In the simulator, turn on VoiceOver or use Accessibility Inspector. Check that each link appears in the rotor, that focus does not get trapped, and that the order matches the text.&lt;/p&gt;

  &lt;h2 id=&quot;accessibility-comply-but-do-not-force-people-to-relearn&quot;&gt;Accessibility: comply, but do not force people to relearn&lt;/h2&gt;

  &lt;p&gt;Accessibility is not only a product decision. The European Accessibility Act (EAA), Directive (EU) 2019/882, has applied since 28 June 2025. If your app falls within its scope — for example, because it provides or gives access to e-commerce, banking, passenger transport, electronic communications, or audiovisual media — accessibility becomes a legal requirement, not an extra. Each Member State must set effective, proportionate, and dissuasive penalties; the actual amount depends on its national transposition. Not every app is covered, and there are exemptions, so check the scope before turning this into a legal promise.&lt;/p&gt;

  &lt;p&gt;The practical argument comes before the law, though. Someone who already uses VoiceOver on their iPhone should not have to learn a new interaction model for your app. If links behave like links, buttons like buttons, and headings like headings, that person arrives with part of the work already done. The app feels predictable, initial learning is shorter, and support gets fewer “why can’t I activate this?” questions.&lt;/p&gt;

  &lt;p&gt;One more thing: an accessible interface is also a more machine-readable interface. Apple Intelligence and agents that interact with apps need semantics: labels, roles, values, and actions. If VoiceOver can handle your app, an Apple Intelligence agent is more likely to do the same. It is not a guarantee, but an app that explains itself well to VoiceOver also explains itself better to any system trying to understand it.&lt;/p&gt;

  &lt;p&gt;That does not mean accessibility is free. It means much of the value comes from using the conventions the system already provides instead of inventing gestures or labels that only work on your screen. The links rotor and focus are shared infrastructure. The more you respect them, the less someone who already knows VoiceOver has to learn.&lt;/p&gt;

  &lt;p&gt;Accessibility also reduces the risk of a feature existing only for people who can see the screen. A link that is only blue, a button that only responds to a tap, or a state communicated only by color is accessibility debt.&lt;/p&gt;

  &lt;h2 id=&quot;the-practical-decision&quot;&gt;The practical decision&lt;/h2&gt;

  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;I need&lt;/th&gt;
        &lt;th&gt;I use&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;Several links inside a SwiftUI paragraph&lt;/td&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AttributedString&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;Decide what a link does&lt;/td&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OpenURLAction&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;A single, isolated link&lt;/td&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Link&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;VoiceOver to recognize it&lt;/td&gt;
        &lt;td&gt;the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.link&lt;/code&gt; attribute, not color or underline&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;p&gt;Formatting is not decoration. A link is a navigation promise; if it only looks like one, the promise breaks for people who cannot see it. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AttributedString&lt;/code&gt; is not just a more convenient API: it is a way for text to carry its meaning with it.&lt;/p&gt;

  &lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/foundation/attributedstring&quot;&gt;AttributedString&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/foundation/attributedstring/markdownparsingoptions&quot;&gt;AttributedString.MarkdownParsingOptions&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/text&quot;&gt;Text&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/openurlaction&quot;&gt;OpenURLAction&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/environmentvalues/openurl&quot;&gt;openURL&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/accessibility/voiceover&quot;&gt;VoiceOver&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/accessibility/accessibility-inspector&quot;&gt;Accessibility Inspector&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://commission.europa.eu/strategy-and-policy/policies/justice-and-fundamental-rights/disability/european-accessibility-act-eaa_en&quot;&gt;European Accessibility Act (EAA)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://eur-lex.europa.eu/eli/dir/2019/882/oj/eng&quot;&gt;Directive (EU) 2019/882&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://www.apple.com/apple-intelligence/&quot;&gt;Apple Intelligence&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

&lt;/div&gt;
</content>
  </entry>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">Sharing a session across iOS, widgets, and Apple Watch: where Keychain fits</title>
    <link href="https://www.obocat.com/blog/2026/11/16/sesion-compartida-widgets-apple-watch-keychain/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/11/16/sesion-compartida-widgets-apple-watch-keychain/</id>
    <published>2026-11-16T00:00:00+00:00</published>
    <updated>2026-11-16T00:00:00+00:00</updated>
    <summary type="text">How to keep a session across an app, widgets, and Apple Watch with shared Keychain, WatchConnectivity, and iCloud Keychain.</summary>
    
    <category term="iOS"/>
    
    <category term="SwiftUI"/>
    
    <category term="WidgetKit"/>
    
    <category term="watchOS"/>
    
    <category term="Security"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/11/16/sesion-compartida-widgets-apple-watch-keychain/">&lt;div class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;

  &lt;p&gt;The scene is familiar: someone signs in to the app, opens the widget, and finds a “Sign in” message. They raise their wrist and, on Apple Watch, it is the same story. The tempting fix is to put the token in shared &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserDefaults&lt;/code&gt; and move on. It is convenient, but it is also a bad idea: a token is a credential and deserves the same care as a password.&lt;/p&gt;

  &lt;p&gt;Shared Keychain helps, but only up to a point. Think of two scenarios that look alike and are not: sharing a secret &lt;strong&gt;on the same device&lt;/strong&gt; and moving it &lt;strong&gt;from one device to another&lt;/strong&gt;. Mix them up and you end up expecting magic from Keychain.&lt;/p&gt;

  &lt;h2 id=&quot;two-different-problems&quot;&gt;Two different problems&lt;/h2&gt;

  &lt;p&gt;The app and the widget run on the same iPhone. If both targets declare the same &lt;em&gt;access group&lt;/em&gt;, they can read and write the same Keychain item. On the watch, the Watch app and its extensions can do the same.&lt;/p&gt;

  &lt;p&gt;The iPhone and Apple Watch are a different story. They are separate devices, and an access group does not synchronize anything between them. To get the session onto the watch, you have two options:&lt;/p&gt;

  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Need&lt;/th&gt;
        &lt;th&gt;Mechanism&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;App + widget on the same iPhone&lt;/td&gt;
        &lt;td&gt;Keychain access group&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;App + widgets/complications on the Watch&lt;/td&gt;
        &lt;td&gt;Keychain access group on watchOS&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;iPhone ↔ Apple Watch&lt;/td&gt;
        &lt;td&gt;WatchConnectivity or iCloud Keychain&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;p&gt;On watchOS 9 and later, complications are built with WidgetKit; ClockKit is deprecated. The same access group covers the app and its widget extensions.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; A Keychain access group shares items on the same device. It does not synchronize them between the iPhone and the Watch.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;It sounds like a small difference, but it explains a lot of failures. When the widget cannot find the token, it is usually a missing entitlement or an access group that does not match. When the Watch cannot find it, the code may be fine: you may be asking Keychain to do something it cannot do.&lt;/p&gt;

  &lt;h2 id=&quot;the-session-model&quot;&gt;The session model&lt;/h2&gt;

  &lt;p&gt;For this example, a small &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Codable&lt;/code&gt; structure is enough:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Foundation&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Security&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Codable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Equatable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Sendable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;accessToken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;expiresAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Date&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;userID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionStoreError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unexpectedStatus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;OSStatus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invalidData&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;In this example, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;refreshToken&lt;/code&gt; is not part of the shared model. The app keeps it in a private Keychain, and the widget and watch only receive the access token, which is what they need to render data and talk to the API. That is the least-privilege option, not an absolute prohibition: if you prefer the watch to refresh on its own, you can share the same refresh token. In that case, do not put it in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applicationContext&lt;/code&gt;; send it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sendMessage&lt;/code&gt; and store it in the watch’s Keychain. Sign-out must always revoke the refresh token on the server, whether or not you share it.&lt;/p&gt;

  &lt;p&gt;That type and the store below need to be available to the app and the widget. You can share them through an internal framework or add the same files to both targets.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--note&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; An App Group’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserDefaults&lt;/code&gt; is convenient for a user identifier, a last-sync date, or an onboarding flag. It is not the place for tokens, credentials, or secrets.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h2 id=&quot;shared-keychain-between-the-app-and-the-widget&quot;&gt;Shared Keychain between the app and the widget&lt;/h2&gt;

  &lt;p&gt;Start by enabling &lt;strong&gt;Keychain Sharing&lt;/strong&gt; in the app and the widget extension. In Xcode, add the capability to both targets and point them at the same group. The entitlements should look roughly like this:&lt;/p&gt;

  &lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;key&amp;gt;&lt;/span&gt;keychain-access-groups&lt;span class=&quot;nt&quot;&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;array&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;$(AppIdentifierPrefix)com.example.shared&lt;span class=&quot;nt&quot;&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/array&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;Watch out for the value used at runtime: Xcode substitutes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$(AppIdentifierPrefix)&lt;/code&gt; at build time, so the group already includes the Team ID. If your Team ID is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ABCDE12345&lt;/code&gt;, the real group is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ABCDE12345.com.example.shared&lt;/code&gt;. If you pass the group without the prefix, Keychain will not find it and may return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;errSecMissingEntitlement&lt;/code&gt;. You can centralize it in a constant; in production, inject it from Info.plist with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$(AppIdentifierPrefix)com.example.shared&lt;/code&gt; and read it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bundle.main&lt;/code&gt;.&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// This store only keeps the access token.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// The refreshToken lives in an app-private group.&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SharedSessionStore&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;service&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;com.example.session&quot;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;account&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;current&quot;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;accessGroup&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ABCDE12345.com.example.shared&quot;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;baseQuery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kSecClass&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kSecClassGenericPassword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kSecAttrService&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kSecAttrAccount&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kSecAttrAccessGroup&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accessGroup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kSecAttrSynchronizable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;JSONEncoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kSecValueData&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kSecAttrAccessible&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SecItemUpdate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;baseQuery&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CFDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CFDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errSecItemNotFound&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseQuery&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forEach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;addStatus&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SecItemAdd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CFDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addStatus&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errSecSuccess&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionStoreError&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;unexpectedStatus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addStatus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errSecSuccess&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionStoreError&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;unexpectedStatus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseQuery&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kSecReturnData&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kSecMatchLimit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kSecMatchLimitOne&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CFTypeRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SecItemCopyMatching&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CFDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errSecItemNotFound&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;nil&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errSecSuccess&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionStoreError&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;unexpectedStatus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as?&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Data&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionStoreError&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;invalidData&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;JSONDecoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;delete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SecItemDelete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;baseQuery&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CFDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errSecSuccess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errSecItemNotFound&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionStoreError&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;unexpectedStatus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;Two details make the difference:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kSecAttrAccessGroup&lt;/code&gt; makes the app and widget read the same item. Without it, each process searches its own space.&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly&lt;/code&gt; allows the token to be read after the first unlock, which is what a background widget needs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WhenUnlocked&lt;/code&gt; fails while the device is locked, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThisDeviceOnly&lt;/code&gt; prevents the item from syncing to iCloud.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; For a widget, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kSecAttrAccessibleWhenUnlocked&lt;/code&gt; is not a good choice. If the extension runs while the device is locked, the read can fail with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;errSecInteractionNotAllowed&lt;/code&gt;. Use an accessibility class that works after the first unlock and do not depend on the screen being unlocked.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;The other detail is about security: this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Session&lt;/code&gt; only contains the access token. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;refreshToken&lt;/code&gt; stays in an app-private Keychain. If the watch needs to refresh on its own, you can share the same refresh token, but send it only over &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sendMessage&lt;/code&gt;, store it in the watch’s Keychain, and revoke it on the server at sign-out.&lt;/p&gt;

  &lt;h2 id=&quot;the-widget-reads-the-app-refreshes&quot;&gt;The widget reads, the app refreshes&lt;/h2&gt;

  &lt;p&gt;The widget should not refresh the token. Timelines run when the system decides, they can overlap, and there is no person in front of them to handle a login. If the token expires, the widget shows a signed-out state or cached data; the app refreshes and updates Keychain.&lt;/p&gt;

  &lt;p&gt;A simple provider can read the shared session:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WidgetKit&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionEntry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;TimelineEntry&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Date&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;userID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionProvider&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;TimelineProvider&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionEntry&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;SessionEntry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;userID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;preview&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getSnapshot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;completion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;@escaping&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;SessionEntry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;completion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getTimeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;completion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;@escaping&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Timeline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;SessionEntry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Calendar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;byAdding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;completion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Timeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;entries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;after&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;entry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionEntry&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SharedSessionStore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionEntry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;userID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;userID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;When the app signs in, signs out, or renews the token, ask WidgetKit to reload the timelines:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WidgetKit&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;WidgetCenter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reloadAllTimelines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;It looks like a small detail, but it prevents the feeling that the widget “does not notice” the login. Keychain does not notify anyone: if you want the widget to update, you have to tell it.&lt;/p&gt;

  &lt;h2 id=&quot;apple-watch-keychain-does-not-cross-devices-by-itself&quot;&gt;Apple Watch: Keychain does not cross devices by itself&lt;/h2&gt;

  &lt;p&gt;The Watch app has its own Keychain. It can share it with its extensions and widgets/complications through a watchOS access group, but that group is not the same store as the iPhone’s. Even if the identifier is identical in the entitlements, the items live on different devices.&lt;/p&gt;

  &lt;p&gt;For the Watch app to use an access group, add Keychain Sharing to the Watch target and its extensions. Without that capability, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kSecAttrAccessGroup&lt;/code&gt; fails with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;errSecMissingEntitlement&lt;/code&gt;, even when the token arrives through WatchConnectivity.&lt;/p&gt;

  &lt;p&gt;In practice, you have two viable strategies.&lt;/p&gt;

  &lt;h3 id=&quot;option-1-watchconnectivity&quot;&gt;Option 1: WatchConnectivity&lt;/h3&gt;

  &lt;p&gt;For a companion app, this is the most direct option. The iPhone app sends the session to the watch, and the Watch app stores it in its Keychain. It does not depend on iCloud Keychain, and it lets you decide when and what to send.&lt;/p&gt;

  &lt;p&gt;Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;updateApplicationContext&lt;/code&gt; to keep the latest state and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sendMessage&lt;/code&gt; to deliver it sooner when the watch is reachable. Activate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WCSession&lt;/code&gt; as early as possible and treat it as a channel with deferred delivery: the system decides when to wake the watch. For a session that changes infrequently, the context is usually enough; for an interactive action, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sendMessage&lt;/code&gt; reduces the wait.&lt;/p&gt;

  &lt;h3 id=&quot;option-2-icloud-keychain&quot;&gt;Option 2: iCloud Keychain&lt;/h3&gt;

  &lt;p&gt;If you need the session to reach the Watch without a companion app, or you want to support Family Setup, Apple presents iCloud Keychain synchronization as an option. Test it on a real device before relying on it, especially for Family Setup. The key change is to store the item with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kSecAttrSynchronizable&lt;/code&gt; set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;baseQuery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kSecClass&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kSecClassGenericPassword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kSecAttrService&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kSecAttrAccount&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kSecAttrAccessGroup&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accessGroup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kSecAttrSynchronizable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kSecValueData&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kSecAttrAccessible&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kSecAttrAccessibleAfterFirstUnlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kSecAttrSynchronizable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;If the item already exists with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kSecAttrSynchronizable&lt;/code&gt; set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;, a query with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; will not find it. To migrate, read with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kSecAttrSynchronizableAny&lt;/code&gt;, save the item with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;, and delete the old one. Do not flip the attribute on the existing item blindly.&lt;/p&gt;

  &lt;p&gt;You also need to add the Keychain Sharing capability to the Watch target that will read the item. From there, iCloud Keychain syncs it across devices on the same account when it can.&lt;/p&gt;

  &lt;p&gt;The upside is that you do not need to maintain a WatchConnectivity session or keep the iPhone nearby. But there are limits you should accept from the start:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;The user can disable iCloud Keychain.&lt;/li&gt;
    &lt;li&gt;It is not available in every region.&lt;/li&gt;
    &lt;li&gt;Synchronization is not immediate; it depends on network, battery, and system conditions.&lt;/li&gt;
    &lt;li&gt;You cannot use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThisDeviceOnly&lt;/code&gt;, because the whole point is to let the item leave the device.&lt;/li&gt;
    &lt;li&gt;You are syncing an access token to every device on the account.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThisDeviceOnly&lt;/code&gt; and iCloud Keychain are incompatible for this use case. If you need synchronization, accessibility must allow it; if you need the token to stay on the device, use WatchConnectivity.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h3 id=&quot;if-you-share-the-same-refresh-token&quot;&gt;If you share the same refresh token&lt;/h3&gt;

  &lt;p&gt;You do not need per-device tokens. You can give the watch the same refresh token the iPhone uses. Server-side revocation is mandatory on every sign-out, whether or not you share the token; if you share it, it is also what cuts off the watch’s access. That said, mind these points:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;Do not persist it in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applicationContext&lt;/code&gt;:&lt;/strong&gt; that context can survive on the device. Send the refresh token with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sendMessage&lt;/code&gt; and store it in the watch’s Keychain. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applicationContext&lt;/code&gt; can still carry only the access token.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Accept the exposure window:&lt;/strong&gt; if the token leaks before logout, the server will accept it until you revoke it. Revocation narrows the window; it does not eliminate it.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Rotate the token and detect reuse:&lt;/strong&gt; if the same refresh token is used on the iPhone and the watch, a mishandled rotation can invalidate the other. Decide which process refreshes and how the new token propagates.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;On sign-out, revoke the refresh token on the server, delete the local tokens on the iPhone, widget, and watch, and send the sign-out message to the watch.&lt;/p&gt;

  &lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Session&lt;/code&gt; model above keeps the refresh token out of the shared object so the baseline is the most restrictive case. If you adopt this variant, add the refresh token to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sendMessage&lt;/code&gt; payload and the watch store; do not add it to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;updateApplicationContext&lt;/code&gt; payload.&lt;/p&gt;

  &lt;h2 id=&quot;sign-in-refresh-and-sign-out&quot;&gt;Sign-in, refresh, and sign-out&lt;/h2&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Revoking the refresh token on the server is not optional. Deleting it from the iPhone, the widget, or the watch does not invalidate it. If the refresh token is still alive, anyone who has it can keep renewing the session. Do it every time, whether or not you share the token with the watch.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;With these pieces, the lifecycle is fairly clean. After a successful sign-in:&lt;/p&gt;

  &lt;ol&gt;
    &lt;li&gt;Save the session in the shared Keychain.&lt;/li&gt;
    &lt;li&gt;Reload the widget timelines.&lt;/li&gt;
    &lt;li&gt;Send the session to the Watch if a companion app exists.&lt;/li&gt;
  &lt;/ol&gt;

  &lt;p&gt;When you renew the token, update the item and repeat steps 2 and 3. When the user signs out, revoke the refresh token on the server, delete the item, reload the widgets, and send a message to the Apple Watch. If the watch shares the same refresh token, the server-side revocation covers it too.&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WidgetKit&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SessionCoordinator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;didSignIn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SharedSessionStore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;WidgetCenter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reloadAllTimelines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Send the session to the Watch with WatchConnectivity.&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;didSignOut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SharedSessionStore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;delete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;WidgetCenter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reloadAllTimelines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Send the sign-out to the Watch with WatchConnectivity.&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;One rule saves a lot of trouble: &lt;strong&gt;a single process refreshes the token&lt;/strong&gt;. If the app, the widget, and the watch all try to refresh at the same time, you can rotate the token several times and leave one of them with an invalid credential. In a companion app, the iPhone app refreshes and distributes.&lt;/p&gt;

  &lt;p&gt;Revoking the refresh token stops renewal, but it does not automatically invalidate an access token already in circulation. To close that window, use short-lived access tokens or a server-side revocation list.&lt;/p&gt;

  &lt;h2 id=&quot;the-practical-decision&quot;&gt;The practical decision&lt;/h2&gt;

  &lt;p&gt;If your app has a widget and an Apple Watch companion app, I would start with this combination:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;A Keychain access group for the app and the widget on iPhone.&lt;/li&gt;
    &lt;li&gt;A watchOS Keychain access group for the Watch app and its extensions.&lt;/li&gt;
    &lt;li&gt;WatchConnectivity to move the session from iPhone to Watch.&lt;/li&gt;
    &lt;li&gt;iCloud Keychain only if you need synchronization without a companion or for Family Setup, accepting its limits.&lt;/li&gt;
    &lt;li&gt;Server-side refresh token revocation on sign-out.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;Shared Keychain does not solve everything by itself. It is an excellent piece for sharing secrets on the same device. To cross from one device to another, the session is transferred or synchronized, and each option has a different cost in security, latency, and complexity.&lt;/p&gt;

  &lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/security/sharing-access-to-keychain-items-among-a-collection-of-apps&quot;&gt;Sharing access to keychain items among a collection of apps&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/security/keychain-services&quot;&gt;Keychain Services&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/security/ksecattrsynchronizable&quot;&gt;kSecAttrSynchronizable&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://www.rfc-editor.org/rfc/rfc7009&quot;&gt;OAuth 2.0 Token Revocation (RFC 7009)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/watchconnectivity/wcsession&quot;&gt;WCSession&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/widgetkit&quot;&gt;WidgetKit&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2021/10003/&quot;&gt;There and back again: Data transfer on Apple Watch&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

&lt;/div&gt;
</content>
  </entry>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">ViewBuilder and applyIf: the hidden cost of a condition in SwiftUI</title>
    <link href="https://www.obocat.com/blog/2026/11/12/viewbuilder-identidad-condicional-swiftui/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/11/12/viewbuilder-identidad-condicional-swiftui/</id>
    <published>2026-11-12T00:00:00+00:00</published>
    <updated>2026-11-12T00:00:00+00:00</updated>
    <summary type="text">Why an if inside @ViewBuilder changes the view&apos;s structural identity and resets its state.</summary>
    
    <category term="iOS"/>
    
    <category term="SwiftUI"/>
    
    <category term="ViewBuilder"/>
    
    <category term="State"/>
    
    <category term="Architecture"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/11/12/viewbuilder-identidad-condicional-swiftui/">&lt;div class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;

  &lt;p&gt;There is a helper that keeps showing up in SwiftUI projects:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@ViewBuilder&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;applyIf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;condition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Content&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;condition&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;The intention is good: apply a modifier only when a condition holds. It compiles, it is convenient, and it looks harmless. But it isn’t entirely harmless.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; The problem is not that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applyIf&lt;/code&gt; fails to compile, but that every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; creates two branches with their own identity. When the condition flips, SwiftUI destroys the view’s state.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h2 id=&quot;what-viewbuilder-actually-builds&quot;&gt;What ViewBuilder actually builds&lt;/h2&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@ViewBuilder&lt;/code&gt; is not just syntactic sugar for choosing one view or another. It is a result builder: it transforms the function body into a type structure.&lt;/p&gt;

  &lt;p&gt;When we use the helper like this:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;MyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;applyIf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;the compiler produces something equivalent to:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;_ConditionalContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ConditionalContent&lt;/code&gt; is an internal SwiftUI type that represents two possible branches. In the example, one branch contains the modified view and the other contains the original view:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;_ConditionalContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;ModifiedContent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;MyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_BackgroundStyleModifier&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;MyView&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--note&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; What matters is understanding that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; is not evaluated as a condition inside a single modifier: the resulting view has two possible structural identities.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h2 id=&quot;structural-identity-not-just-values&quot;&gt;Structural identity, not just values&lt;/h2&gt;

  &lt;p&gt;SwiftUI distinguishes between a view’s value and its identity in the hierarchy. Structural identity depends on the view’s type and position in the tree. An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if/else&lt;/code&gt; inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ViewBuilder&lt;/code&gt; creates two different positions: the true branch and the false branch.&lt;/p&gt;

  &lt;p&gt;When &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;condition&lt;/code&gt; changes, SwiftUI is not changing the parameter of a single modifier. It is removing one branch and inserting the other. If there is an animation, the result is a transition —a fade, for example— instead of an interpolation between the two values.&lt;/p&gt;

  &lt;p&gt;This also affects state. SwiftUI associates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@State&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@StateObject&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@FocusState&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@GestureState&lt;/code&gt; with the view’s lifetime and identity. If the view changes branches, its storage disappears and is initialized again.&lt;/p&gt;

  &lt;p&gt;With the helper defined above, the example can be tested directly in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#Preview&lt;/code&gt;:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Stateful&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@State&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;TextField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;textFieldStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;roundedBorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Bool&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Stateful&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;applyIf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SamplePreview&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@State&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;isEnabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;VStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;spacing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;Sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toggle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Disable applyIf&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Enable applyIf&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fontWeight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;semibold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;maxWidth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;infinity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clipShape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;RoundedRectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cornerRadius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;isEnabled: &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;caption&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secondary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#Preview {&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;SamplePreview&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;Toggling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isEnabled&lt;/code&gt; makes the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TextField&lt;/code&gt; lose the text the user had typed. The same happens with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@StateObject&lt;/code&gt; created inside the conditional view. It’s not that SwiftUI clears state for no reason: from its perspective, the view has moved to a different position in the tree.&lt;/p&gt;

  &lt;p&gt;There is an important nuance: if the state lives in a parent whose identity doesn’t change, it survives. The problem affects state owned by the view that changes branches.&lt;/p&gt;

  &lt;h2 id=&quot;how-to-preserve-identity&quot;&gt;How to preserve identity&lt;/h2&gt;

  &lt;p&gt;The practical rule is: if both branches represent the same view with a different configuration, try to keep a single type and change only the value.&lt;/p&gt;

  &lt;p&gt;When the modifier accepts a conditional value, that is the most direct option:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;MyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;red&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;MyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;MyView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;opacity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;You can also wrap the condition in a stable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ViewModifier&lt;/code&gt;:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ConditionalBackground&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ViewModifier&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Bool&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;red&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;conditionalBackground&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;modifier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;ConditionalBackground&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isEnabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--note&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ModifiedContent&amp;lt;MyView, ConditionalBackground&amp;gt;&lt;/code&gt; doesn’t change; the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt; is just a parameter. Identity is preserved and the modifier remains applied at all times.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h2 id=&quot;when-an-if-is-the-right-tool&quot;&gt;When an if is the right tool&lt;/h2&gt;

  &lt;p&gt;This is not about banning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applyIf&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; statements in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ViewBuilder&lt;/code&gt;. Branches are the right tool when we genuinely want two different structures: showing a loading screen or the content, an empty state or a list, one view or another.&lt;/p&gt;

  &lt;p&gt;The key is to choose deliberately:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;If you want to change the structure, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if/else&lt;/code&gt; and accept that identity changes.&lt;/li&gt;
    &lt;li&gt;If you want to change only a property of the same view, preserve identity and pass the value to the modifier.&lt;/li&gt;
    &lt;li&gt;If state must survive a structural change, lift it to a stable parent or to a model that isn’t destroyed.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;applyIf&lt;/code&gt; is convenient, but it hides an architectural decision: every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ViewBuilder&lt;/code&gt; creates two branches with their own identity. That resets the state of the view that changes branches.&lt;/p&gt;

  &lt;p&gt;Before adding a conditional helper, it’s worth asking whether the two branches are really two different views or the same view with a different configuration. If it’s the latter, keeping the type stable is the more predictable option.&lt;/p&gt;

  &lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/viewbuilder&quot;&gt;ViewBuilder&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2021/10022&quot;&gt;Demystify SwiftUI (WWDC21)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://www.objc.io/blog/2021/08/24/conditional-view-modifiers&quot;&gt;Why Conditional View Modifiers are a Bad Idea&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://swiftwithmajid.com/2021/12/09/structural-identity-in-swiftui&quot;&gt;Structural identity in SwiftUI&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

&lt;/div&gt;
</content>
  </entry>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">Custom SF Symbols: color and motion beyond the catalog</title>
    <link href="https://www.obocat.com/blog/2026/11/09/sf-symbols-personalizados-color-animacion/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/11/09/sf-symbols-personalizados-color-animacion/</id>
    <published>2026-11-09T00:00:00+00:00</published>
    <updated>2026-11-09T00:00:00+00:00</updated>
    <summary type="text">Custom SF Symbols are not static icons: with the right annotations they inherit the system&apos;s rendering modes and animations.</summary>
    
    <category term="iOS"/>
    
    <category term="SwiftUI"/>
    
    <category term="SF Symbols"/>
    
    <category term="Design"/>
    
    <category term="Accessibility"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/11/09/sf-symbols-personalizados-color-animacion/">&lt;div class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;

  &lt;p&gt;There is a common assumption among developers and designers: SF Symbols are the icon catalog Apple ships with the system, and if you need something different, you draw a PNG or PDF and lose the system benefits. That is not the case.&lt;/p&gt;

  &lt;p&gt;Custom SF Symbols have existed for several releases. They are not regular images: they are vector templates that behave like system symbols. If you prepare them properly, they can use the same rendering modes and the same family of animations as Apple’s icons.&lt;/p&gt;

  &lt;p&gt;The key word is &lt;strong&gt;annotations&lt;/strong&gt;. A pretty SVG is not enough. The symbol needs layers and metadata so SwiftUI knows which parts to color, in what order, and what to animate.&lt;/p&gt;

  &lt;h2 id=&quot;what-a-custom-sf-symbol-really-is&quot;&gt;What a custom SF Symbol really is&lt;/h2&gt;

  &lt;p&gt;A custom SF Symbol is an SVG validated by the SF Symbols app and added to an asset catalog as a &lt;strong&gt;Symbol Image Set&lt;/strong&gt;, not as a regular image set. That distinction matters: if you add it as a normal image, you lose the symbol behavior.&lt;/p&gt;

  &lt;p&gt;The usual workflow is:&lt;/p&gt;

  &lt;ol&gt;
    &lt;li&gt;Find a similar symbol in the SF Symbols app and use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;File &amp;gt; Duplicate as Custom Symbol&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;File &amp;gt; Export Template&lt;/code&gt;.&lt;/li&gt;
    &lt;li&gt;Edit the SVG in a vector tool. Convert strokes to paths, use flat fills, and avoid shadows, effects, and hidden paths.&lt;/li&gt;
    &lt;li&gt;Import the SVG back into the SF Symbols app and validate it.&lt;/li&gt;
    &lt;li&gt;Annotate the rendering modes and, if needed, the animations.&lt;/li&gt;
    &lt;li&gt;Export the symbol and add it to the asset catalog as a Symbol Image Set.&lt;/li&gt;
    &lt;li&gt;Use it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image(&quot;custom.orbit&quot;)&lt;/code&gt;, not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image(systemName:)&lt;/code&gt;.&lt;/li&gt;
  &lt;/ol&gt;

  &lt;p&gt;The SF Symbols app validates that the SVG follows the format. It is also where annotations live.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; If you export a template to edit it, that export &lt;strong&gt;does not include&lt;/strong&gt; the annotations. To keep them, drag the edited SVG onto the existing symbol.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h2 id=&quot;from-a-regular-svg-to-an-sf-symbol&quot;&gt;From a regular SVG to an SF Symbol&lt;/h2&gt;

  &lt;p&gt;The SF Symbols app expects a template with a very specific structure. If you already have the icon as an SVG, you do not have to redraw it from scratch: there are tools that generate the symbol wrapper from a conventional SVG.&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://github.com/jaywcjlove/create-custom-symbols&quot;&gt;&lt;strong&gt;Create Custom Symbols&lt;/strong&gt;&lt;/a&gt;: a macOS app where you drag one or more SVGs and export a file you can import into the SF Symbols app. It is useful for batches and for converting icons that already exist in the design.&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://github.com/swhitty/SwiftDraw&quot;&gt;&lt;strong&gt;SwiftDraw&lt;/strong&gt;&lt;/a&gt;: a command-line tool. With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;brew install swiftdraw&lt;/code&gt;, you can run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;swiftdraw icon.svg --format sfsymbol&lt;/code&gt;. It expands strokes, fixes path winding, and aligns the result so Xcode accepts it.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Figma plugins&lt;/strong&gt; such as &lt;a href=&quot;https://www.figma.com/community/plugin/1487107089082698461/sf-symbol-generator&quot;&gt;&lt;strong&gt;SF Symbol Generator&lt;/strong&gt;&lt;/a&gt; and &lt;a href=&quot;https://www.figma.com/community/plugin/1265484574589944515/sf-symbols-optimizer&quot;&gt;&lt;strong&gt;SF Symbols Optimizer&lt;/strong&gt;&lt;/a&gt;: they export Figma icons as SF Symbols SVGs, with static or variable variants.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--note&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; These tools save you the structural work, not the design work. They cannot guess which layers belong in a hierarchy, which colors go into a palette, or which parts should animate.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;Conventional SVGs often contain strokes, masks, compound paths, or nested groups that do not always convert cleanly. Review the result in the SF Symbols app, check path direction, and annotate the layers before calling it done.&lt;/p&gt;

  &lt;h2 id=&quot;color-four-modes-not-one&quot;&gt;Color: four modes, not one&lt;/h2&gt;

  &lt;p&gt;So the examples can be tried without creating an asset, I use system symbols. The modifiers are the same; only how you load the image changes. When you have yours, replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image(systemName: &quot;sparkles&quot;)&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image(&quot;your.symbol&quot;)&lt;/code&gt;.&lt;/p&gt;

  &lt;p&gt;System symbols can render in four modes. Custom symbols can too, as long as their layers are annotated:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;strong&gt;Monochrome:&lt;/strong&gt; one color for all layers.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Hierarchical:&lt;/strong&gt; one color with different opacity levels to create depth.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Palette:&lt;/strong&gt; one color per layer or layer group.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Multicolor:&lt;/strong&gt; intrinsic colors defined inside the symbol.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;In SwiftUI you choose the mode with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;symbolRenderingMode&lt;/code&gt;:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sparkles&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;72&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;symbolRenderingMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;palette&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cyan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;The important part is not the modifier but what sits behind it. For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.palette&lt;/code&gt; to work, the symbol needs layers annotated for palette; the colors come from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foregroundStyle&lt;/code&gt;. For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.hierarchical&lt;/code&gt; to make sense, it needs primary, secondary, and tertiary groups. Without annotations, the result can be flatter than you expected.&lt;/p&gt;

  &lt;p&gt;SF Symbols 7 also adds gradient rendering, available in every color mode and applicable to custom symbols. It is useful for adding depth, but use it sparingly: a toolbar icon does not need the same treatment as a large illustration.&lt;/p&gt;

  &lt;p&gt;It also pays to use semantic colors (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.primary&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.tint&lt;/code&gt;, asset catalog colors) instead of fixed values. That way the symbol keeps working in dark mode, with vibrancy, and with accessibility settings.&lt;/p&gt;

  &lt;h2 id=&quot;animation-the-same-system-with-caveats&quot;&gt;Animation: the same system, with caveats&lt;/h2&gt;

  &lt;p&gt;Since iOS 17, symbols have universal effects: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bounce&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pulse&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;variableColor&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scale&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;appear&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;disappear&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replace&lt;/code&gt;. Later releases added &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wiggle&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rotate&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breathe&lt;/code&gt;, and, in SF Symbols 7, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;drawOn&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;drawOff&lt;/code&gt;.&lt;/p&gt;

  &lt;p&gt;The good news is that these effects can also be applied to custom symbols. The caveat is that layer-dependent effects — such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;variableColor&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;drawOn&lt;/code&gt; — need the symbol to have those layers and to be annotated. An old or poorly prepared symbol may fall back to a generic fade instead of the animation you expected.&lt;/p&gt;

  &lt;p&gt;A minimal SwiftUI example:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CustomSymbolDemo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@State&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;bounceTrigger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@State&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;isPulsing&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;VStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;spacing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sparkles&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;88&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;symbolRenderingMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hierarchical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;symbolEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bounce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bounceTrigger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;symbolEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pulse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;isActive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isPulsing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;accessibilityLabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Highlight&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;kt&quot;&gt;HStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;spacing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Bounce&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;bounceTrigger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

                &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isPulsing&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Stop pulse&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Pulse&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;isPulsing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toggle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#Preview {&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;CustomSymbolDemo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;value:&lt;/code&gt; is for discrete effects that fire when a value changes. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isActive:&lt;/code&gt; is for indefinite effects that stay active while the condition is true. You can also adjust repetition or speed with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;options&lt;/code&gt;.&lt;/p&gt;

  &lt;p&gt;The SF Symbols app lets you copy an effect’s configuration with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Copy Configuration&lt;/code&gt;. It is a convenient way to go from the preview to code without guessing which combination of options you were looking at.&lt;/p&gt;

  &lt;p&gt;For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;variableColor&lt;/code&gt;, the symbol needs multiple layers and a variable color annotation. A battery or a progress bar is a typical example:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BatterySymbol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;battery.75&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;symbolRenderingMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;palette&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yellow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;symbolEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variableColor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iterative&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;Not every custom symbol has to support everything. The sensible approach is to test each effect in the SF Symbols app before taking it to code.&lt;/p&gt;

  &lt;h2 id=&quot;what-a-custom-symbol-does-not-solve&quot;&gt;What a custom symbol does not solve&lt;/h2&gt;

  &lt;p&gt;An SF Symbol is not an illustration. It is a small, recognizable icon that is optically aligned with the rest of the system. If you drop in a complex logo with gradients, shadows, and fine details, it will struggle at small sizes and in dark mode.&lt;/p&gt;

  &lt;p&gt;You should also avoid copying Apple symbols that depict products or features and modifying their paths. The SF Symbols app marks some symbols as non-customizable.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Annotations depend on the number and order of paths staying the same. If you edit the SVG and remove or reorder paths, the annotations can become out of sync. Validate and review the symbol after every change.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h2 id=&quot;accessibility-and-motion&quot;&gt;Accessibility and motion&lt;/h2&gt;

  &lt;p&gt;An animated icon is still an icon. If it conveys information, it needs an accessible label:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sparkles&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;accessibilityLabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Highlight&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;If it is decorative, hide it:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sparkles&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;accessibilityHidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;And not everyone wants motion. Respect &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;accessibilityReduceMotion&lt;/code&gt; and avoid indefinite animations when the person has asked to reduce motion:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;@Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(\&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;accessibilityReduceMotion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;reduceMotion&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sparkles&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;symbolEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pulse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;isActive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isPulsing&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduceMotion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;For sizing, use text styles (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.font(.title)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.font(.largeTitle)&lt;/code&gt;) instead of fixed pixels. That way the symbol follows Dynamic Type.&lt;/p&gt;

  &lt;h2 id=&quot;checklist-before-calling-it-done&quot;&gt;Checklist before calling it done&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;Is it in a &lt;strong&gt;Symbol Image Set&lt;/strong&gt; in the asset catalog?&lt;/li&gt;
    &lt;li&gt;Does it have enough layers for the modes you plan to use?&lt;/li&gt;
    &lt;li&gt;Have you tested monochrome, hierarchical, palette, and multicolor?&lt;/li&gt;
    &lt;li&gt;Have you tested the effects at every scale and weight?&lt;/li&gt;
    &lt;li&gt;Does it look good in light mode, dark mode, and low-contrast contexts?&lt;/li&gt;
    &lt;li&gt;Does it have an accessibility label, or is it hidden if decorative?&lt;/li&gt;
    &lt;li&gt;Does it respect Reduce Motion?&lt;/li&gt;
    &lt;li&gt;Have you validated the SVG in the SF Symbols app?&lt;/li&gt;
  &lt;/ul&gt;

  &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

  &lt;p&gt;Custom SF Symbols are one of those features that look like a design detail and end up being a visual architecture decision. If you treat them as static SVGs, you get nice icons. If you treat them as annotated symbols, you get icons that color, animate, and behave like Apple’s.&lt;/p&gt;

  &lt;p&gt;The next time you need an icon that is not in the catalog, do not start with a PNG. Start with an SF Symbols template. The result fits better, scales better, and moves just as well.&lt;/p&gt;

  &lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/technologyoverviews/custom-sf-symbols&quot;&gt;Creating custom symbols&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/technologyoverviews/annotating-sf-symbols&quot;&gt;Annotating custom symbols&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/sf-symbols&quot;&gt;SF Symbols&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/design/human-interface-guidelines/sf-symbols&quot;&gt;Human Interface Guidelines: SF Symbols&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/symbols&quot;&gt;Symbols framework&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2023/10257/&quot;&gt;WWDC23: Create animated symbols&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2023/10258/&quot;&gt;WWDC23: Animate symbols in your app&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2025/337/&quot;&gt;WWDC25: What’s new in SF Symbols 7&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

&lt;/div&gt;
</content>
  </entry>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">SwiftUI: removing Liquid Glass from a toolbar (without leaving the system)</title>
    <link href="https://www.obocat.com/blog/2026/11/04/swiftui-toolbar-sin-liquid-glass/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/11/04/swiftui-toolbar-sin-liquid-glass/</id>
    <published>2026-11-04T00:00:00+00:00</published>
    <updated>2026-11-04T00:00:00+00:00</updated>
    <summary type="text">How to use sharedBackgroundVisibility(.hidden) to keep a navigation bar group without Liquid Glass, and why UIDesignRequiresCompatibility no longer works.</summary>
    
    <category term="iOS"/>
    
    <category term="SwiftUI"/>
    
    <category term="Liquid Glass"/>
    
    <category term="Toolbar"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/11/04/swiftui-toolbar-sin-liquid-glass/">&lt;div class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;

  &lt;p&gt;Liquid Glass is the visual language Apple introduced in iOS 26. It changes how navigation bars, buttons, and sheets are grouped and drawn: toolbar items share a Liquid Glass background that adapts to the content behind them. For most screens, adopting it is the right call.&lt;/p&gt;

  &lt;p&gt;But there are exceptions. Sometimes, because of product design or brand identity, we’re asked to keep a specific component without that background: a logo that already has its own shape, an avatar that needs a solid color, or a control that should sit on its own surface. You don’t have to abandon the whole system to do it. SwiftUI has an API designed for exactly this exception.&lt;/p&gt;

  &lt;p&gt;If you’re preparing the app to build with the iOS 27 SDK and for new devices such as the iPhone Duo, the first step is to handle the layout and camera adaptation; I covered that in &lt;a href=&quot;/blog/2026/10/29/iphone-duo-layout-camara/&quot;&gt;Adapting an app to iPhone Duo: easy layouts, hard camera&lt;/a&gt;. Once that foundation is in place, it makes more sense to fine-tune exceptions like the one in this article.&lt;/p&gt;

  &lt;h2 id=&quot;the-api-sharedbackgroundvisibilityhidden&quot;&gt;The API: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sharedBackgroundVisibility(.hidden)&lt;/code&gt;&lt;/h2&gt;

  &lt;p&gt;The modifier applies to the toolbar content, not to the view inside it. Here is the minimal example:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SwiftUI&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ToolbarWithoutGlassView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;NavigationStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Test&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toolbar&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;kt&quot;&gt;ToolbarItemGroup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;placement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;topBarLeading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Test&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Tapped&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sharedBackgroundVisibility&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; The modifier goes after the closing brace of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ToolbarItemGroup&lt;/code&gt;, not inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sharedBackgroundVisibility&lt;/code&gt; is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ToolbarContent&lt;/code&gt; API; if you apply it to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;View&lt;/code&gt;, the compiler won’t find it.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;The example assumes an iOS 26 or later target; if your app supports earlier versions, use the compatibility extension later in the article.&lt;/p&gt;

  &lt;h2 id=&quot;what-it-actually-does&quot;&gt;What it actually does&lt;/h2&gt;

  &lt;p&gt;Before using it, it’s worth understanding what changes and what doesn’t. The name describes the behavior: it controls the visibility of the background shared by toolbar items. Passing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.hidden&lt;/code&gt; tells the system to stop drawing that shared background and places the item in its own grouping. It is not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;background(.clear)&lt;/code&gt; or a transparency trick: it is an instruction to the system not to apply the Liquid Glass effect to that content.&lt;/p&gt;

  &lt;p&gt;With a single item, the result is that item without the shared capsule; with several, the whole group stops using the shared background. The buttons remain interactive: what changes is the visual treatment of the container, not its behavior. The rest of the toolbar items keep theirs.&lt;/p&gt;

  &lt;h2 id=&quot;a-real-case-a-brand-that-already-has-its-own-identity&quot;&gt;A real case: a brand that already has its own identity&lt;/h2&gt;

  &lt;p&gt;Let’s look at a common case. Imagine a brand logo in the top-leading corner. If you put it inside a Liquid Glass capsule, the system adds a layer that competes with the logo and can make it look like a generic button. Hiding the shared background lets the logo read as toolbar content.&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SwiftUI&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BrandToolbarView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;NavigationStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Home&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toolbar&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;kt&quot;&gt;ToolbarItemGroup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;placement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;topBarLeading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Brand tapped&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;kt&quot;&gt;HStack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;spacing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                                &lt;span class=&quot;kt&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sparkles&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foregroundStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

                                &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Acme&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;accessibilityLabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Acme&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sharedBackgroundVisibility&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;If the logo isn’t interactive, don’t write it as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Button&lt;/code&gt; just to make it look at home in the toolbar. A plain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HStack&lt;/code&gt; with a proper accessibility label usually fits better. If it is interactive, keep the hit target and keyboard focus any other button would have.&lt;/p&gt;

  &lt;h2 id=&quot;what-you-lose-when-you-remove-the-liquid-glass-background&quot;&gt;What you lose when you remove the Liquid Glass background&lt;/h2&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--note&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Removing Liquid Glass is not just decoration. Its background separates toolbar content from what’s behind it and helps keep text legible when the screen has photos, maps, or vivid colors. When you hide it, that responsibility becomes part of your design.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;Before accepting an exception, check:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Contrast in light and dark mode.&lt;/li&gt;
    &lt;li&gt;The content behind the item, especially while scrolling.&lt;/li&gt;
    &lt;li&gt;The Reduce Transparency and Increase Contrast settings.&lt;/li&gt;
    &lt;li&gt;Dynamic Type: text shouldn’t break the group.&lt;/li&gt;
    &lt;li&gt;The hit target, which should remain comfortable.&lt;/li&gt;
    &lt;li&gt;VoiceOver: if the item is a button, it needs a clear label; if it’s a brand mark, it shouldn’t be announced as an action.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;Hiding the background is easy. The hard part is keeping the result clear, accessible, and consistent with the rest of the screen.&lt;/p&gt;

  &lt;h2 id=&quot;backward-compatibility&quot;&gt;Backward compatibility&lt;/h2&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sharedBackgroundVisibility&lt;/code&gt; is available starting in iOS 26.0, as well as iPadOS 26 and macOS 26. The example uses iOS navigation bar placements. If your app supports earlier versions, you need to build with the iOS 26 SDK or later and guard runtime execution with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#available&lt;/code&gt;.&lt;/p&gt;

  &lt;p&gt;A reusable extension avoids duplicating the toolbar content:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SwiftUI&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ToolbarContent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@ToolbarContentBuilder&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hideSharedGlassIfAvailable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ToolbarContent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;#available(iOS 26.0, *)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;sharedBackgroundVisibility&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CompatibleToolbarView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;some&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;View&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;NavigationStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Test&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toolbar&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;kt&quot;&gt;ToolbarItemGroup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;placement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;topBarLeading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;kt&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Test&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Tapped&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hideSharedGlassIfAvailable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#Preview {&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;CompatibleToolbarView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;The extension uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ToolbarContentBuilder&lt;/code&gt; to return the same content with or without the modifier. On iOS 26 and later it hides the background; on earlier versions it returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self&lt;/code&gt; and changes nothing.&lt;/p&gt;

  &lt;h2 id=&quot;uidesignrequirescompatibility-the-end-of-the-grace-period&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIDesignRequiresCompatibility&lt;/code&gt;: the end of the grace period&lt;/h2&gt;

  &lt;p&gt;All of this leads to the question that usually comes next: what about the global key? During the transition to iOS 26, Apple offered an escape hatch: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIDesignRequiresCompatibility&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Info.plist&lt;/code&gt;.&lt;/p&gt;

  &lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;key&amp;gt;&lt;/span&gt;UIDesignRequiresCompatibility&lt;span class=&quot;nt&quot;&gt;&amp;lt;/key&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;true/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YES&lt;/code&gt;, the app runs in compatibility mode and keeps the previous look. It was useful for buying time, but it was never a design strategy: it is a grace period for adapting the app, not a way to maintain two visual languages indefinitely.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; According to Apple’s documentation, the system ignores this key when you build with the iOS 27 SDK or later. If your app is built with the iOS 27 SDK, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIDesignRequiresCompatibility&lt;/code&gt; no longer brings the old design back.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;There is a nuance worth keeping in mind: if you still build with the iOS 26 SDK, the key can have an effect on iOS 26. But that means staying on an old SDK, with the maintenance and compatibility consequences that implies. It is not a long-term solution.&lt;/p&gt;

  &lt;p&gt;And there is a date that makes that nuance more urgent. &lt;a href=&quot;https://developer.apple.com/news/?id=k1mtkt1k&quot;&gt;Apple has announced&lt;/a&gt; that, starting in April 2027, iOS and iPadOS apps uploaded to App Store Connect must be built with the iOS 27 SDK or later. This is not a date when iOS stops reading the key in already-installed apps; it is a practical expiration date: as soon as you update the SDK to ship, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIDesignRequiresCompatibility&lt;/code&gt; stops having any effect. Builds already published do not change on their own, but updates will have to embrace the new design.&lt;/p&gt;

  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Need&lt;/th&gt;
        &lt;th&gt;Tool&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;Hide Liquid Glass on a toolbar item or group&lt;/td&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.sharedBackgroundVisibility(.hidden)&lt;/code&gt; on iOS 26+&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;Separate groups while keeping Liquid Glass&lt;/td&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ToolbarSpacer&lt;/code&gt; on iOS 26+&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;Compatibility mode for the whole app&lt;/td&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIDesignRequiresCompatibility&lt;/code&gt; (ignored with the iOS 27 SDK)&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;h2 id=&quot;my-take-exceptions-not-the-norm&quot;&gt;My take: exceptions, not the norm&lt;/h2&gt;

  &lt;p&gt;With all this on the table, my position is clear: consistency with the system should be the default. Users learn once how iOS controls look and behave, and Liquid Glass is part of that visual language. Changing it in one isolated component can make people wonder whether it is a button, an ornament, or content.&lt;/p&gt;

  &lt;p&gt;But consistency isn’t an absolute rule. There are legitimate exceptions: a brand that already has its own language, a control that needs its own background to be legible, or a surface that shouldn’t compete with the system’s Liquid Glass. What matters is that it should be a deliberate, documented, and tested exception, not a way to avoid adapting the app.&lt;/p&gt;

  &lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/toolbarcontent/sharedbackgroundvisibility%28_%3A%29&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sharedBackgroundVisibility(_:)&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/bundleresources/information-property-list/uidesignrequirescompatibility&quot;&gt;UIDesignRequiresCompatibility&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/toolbaritemplacement/topbarleading&quot;&gt;ToolbarItemPlacement.topBarLeading&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2025/323/&quot;&gt;Build a SwiftUI app with the new design (WWDC25)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://sarunw.com/posts/swiftui-toolbar-item-grouping-ios-26/&quot;&gt;When toolbar items share a glass background in iOS 26&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

&lt;/div&gt;
</content>
  </entry>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">ArrangementView: composition and space distribution in SwiftUI</title>
    <link href="https://www.obocat.com/blog/2026/10/31/arrangementview-composicion-espacio-swiftui/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/10/31/arrangementview-composicion-espacio-swiftui/</id>
    <published>2026-10-31T00:00:00+00:00</published>
    <updated>2026-10-31T00:00:00+00:00</updated>
    <summary type="text">How ArrangementView takes SwiftUI space management from view-level priority to section-level composition.</summary>
    
    <category term="iOS"/>
    
    <category term="iPhone Duo"/>
    
    <category term="SwiftUI"/>
    
    <category term="Layout"/>
    
    <category term="ArrangementView"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/10/31/arrangementview-composicion-espacio-swiftui/">&lt;div class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;

  &lt;p&gt;In the &lt;a href=&quot;/blog/2026/10/29/iphone-duo-layout-camara/&quot;&gt;previous article&lt;/a&gt; we saw how we adapted an app to iPhone Duo: layouts were much easier than expected, and the camera was the hard part. One of the pieces that made the layout so manageable was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt;.&lt;/p&gt;

  &lt;p&gt;But before getting into the API, it’s worth understanding what problem it solves. And for that, we should start with something more familiar: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layoutPriority&lt;/code&gt;.&lt;/p&gt;

  &lt;h2 id=&quot;from-layout-priority-to-composition&quot;&gt;From layout priority to composition&lt;/h2&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layoutPriority&lt;/code&gt; lets you influence how a container distributes space among its child views. When space is limited, SwiftUI can use that priority to decide which view keeps its ideal size and which one gets compressed.&lt;/p&gt;

  &lt;p&gt;For example:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;HStack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Media&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;layoutPriority&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Products&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;The first view has preference over the second when space has to be negotiated. It is a local tool: it acts between sibling views inside the same container.&lt;/p&gt;

  &lt;p&gt;The problem appears when the decision is no longer “which &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt; shrinks first”, but “how do two complete sections of the interface relate to each other?”. That’s where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layoutPriority&lt;/code&gt; falls short: it doesn’t express a composition policy.&lt;/p&gt;

  &lt;h2 id=&quot;composition-level-layout-1&quot;&gt;Composition-level layout&lt;/h2&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; raises the level of abstraction. Instead of distributing space between individual views, it lets you declare a relationship between a primary and a secondary section, and lets the container decide how to compose them based on available space and device pose.&lt;/p&gt;

  &lt;p&gt;This fits iPhone Duo especially well: the geometry changes between displays and poses, but the primary/secondary relationship can remain even when the arrangement changes.&lt;/p&gt;

  &lt;p&gt;An example:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;ArrangementView&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mediaSection&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;splitArrangementFixedLayoutSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;vertical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;secondary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;productsSection&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;splitArrangementFixedLayoutSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;vertical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;arrangementViewStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;There are three distinct decisions here:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; establishes the relationship between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mediaSection&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;productsSection&lt;/code&gt;.&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.arrangementViewStyle(.split)&lt;/code&gt; chooses the composition strategy: both sections share space in a split arrangement.&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;splitArrangementFixedLayoutSize(horizontal:vertical:)&lt;/code&gt; indicates which axes each section should keep at its ideal size instead of stretching.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;In the example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mediaSection&lt;/code&gt; doesn’t fix either axis, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;productsSection&lt;/code&gt; fixes the vertical dimension. The result is not a rigid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame&lt;/code&gt;: it is a size constraint that the container interprets according to available space and pose. If there isn’t enough room to keep the split, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; may show only the primary view.&lt;/p&gt;

  &lt;h2 id=&quot;what-each-piece-does&quot;&gt;What each piece does&lt;/h2&gt;

  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;API&lt;/th&gt;
        &lt;th&gt;Responsibility&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layoutPriority&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Influences space negotiation between views.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Defines how primary and secondary sections are composed and distributed.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;splitArrangementFixedLayoutSize&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Expresses per-axis size constraints for a section.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arrangementViewStyle(.split)&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Sets the visual composition strategy.&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;p&gt;The important difference is one of level. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layoutPriority&lt;/code&gt; answers a local question: “which view should keep its preferred size?”. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; answers a structural question: “what relationship exists between these two sections, and how should it change when the container changes shape?”.&lt;/p&gt;

  &lt;h2 id=&quot;the-split-nuance&quot;&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.split&lt;/code&gt; nuance&lt;/h2&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.split&lt;/code&gt; doesn’t mean “always side by side”. It means the container will try to share space between the two sections and decide the orientation based on available space.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;On iPhone Duo it also accounts for reserved regions such as the fold.&lt;/p&gt;

  &lt;p&gt;If you want to constrain the split to a specific axis, you can use:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;arrangementViewStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;axes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;That’s useful when the design only makes sense horizontally. But don’t force it without need: part of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt;’s value is precisely adapting the composition to the device pose.&lt;/p&gt;

  &lt;h2 id=&quot;when-to-use-it-and-when-not-to&quot;&gt;When to use it, and when not to&lt;/h2&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--note&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; is not a general replacement for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HStack&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VStack&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZStack&lt;/code&gt;. It is a specialised container for a stable relationship between primary and secondary content.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;Before using it, it’s worth answering:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Does the primary/secondary relationship make sense in every pose?&lt;/li&gt;
    &lt;li&gt;Which section can disappear when there isn’t enough space?&lt;/li&gt;
    &lt;li&gt;Is the secondary view still accessible if the container hides it?&lt;/li&gt;
    &lt;li&gt;Does switching between split and overlay require swapping the primary and secondary roles?&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;That last point is easy to miss. In a split, the primary view is the one that remains when the division doesn’t fit. In an overlay, the primary is the one on top. The same word doesn’t always imply the same visual hierarchy. If the design alternates between styles, review the roles.&lt;/p&gt;

  &lt;p&gt;Also remember that these configurations express preferences, not guaranteed outcomes. The container makes the final call. If the interface needs to know exactly which arrangement is being shown, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; may not be the right tool.&lt;/p&gt;

  &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

  &lt;p&gt;We can understand &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; as a conceptual evolution of traditional space management in SwiftUI, applied at a higher level.&lt;/p&gt;

  &lt;p&gt;While &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layoutPriority&lt;/code&gt; takes part in space negotiation between individual views, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; lets you define composition policies between complete blocks of content.&lt;/p&gt;

  &lt;p&gt;In other words: we move from deciding which view has priority during layout to defining how we want an interface to be composed and distributed.&lt;/p&gt;

  &lt;p&gt;One way to summarise the difference:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layoutPriority&lt;/code&gt;: layout negotiation priority.&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt;: compositional layout policy.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;That distinction helps us think about interfaces from a more structural perspective, separating composition decisions from the internal layout details of each section.&lt;/p&gt;

  &lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/arrangementview&quot;&gt;ArrangementView&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/view/splitarrangementfixedlayoutsize%28horizontal%3Avertical%3A%29&quot;&gt;splitArrangementFixedLayoutSize(horizontal:vertical:)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/swiftui/view/arrangementviewstyle%28%5F%3A%29&quot;&gt;arrangementViewStyle(_:)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://fatbobman.com/en/posts/arrangementview-think-before-you-arrange&quot;&gt;ArrangementView: Think Before You Arrange&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;/blog/2026/10/29/iphone-duo-layout-camara/&quot;&gt;Adapting an app to iPhone Duo&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

&lt;/div&gt;
</content>
  </entry>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">Adapting an app to iPhone Duo: easy layouts, hard camera</title>
    <link href="https://www.obocat.com/blog/2026/10/29/iphone-duo-layout-camara/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/10/29/iphone-duo-layout-camara/</id>
    <published>2026-10-29T00:00:00+00:00</published>
    <updated>2026-10-29T00:00:00+00:00</updated>
    <summary type="text">How we adapted an app to iPhone Duo with ArrangementView and AVCaptureDevice.RotationCoordinator.</summary>
    
    <category term="iOS"/>
    
    <category term="iPhone Duo"/>
    
    <category term="AVFoundation"/>
    
    <category term="SwiftUI"/>
    
    <category term="Camera"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/10/29/iphone-duo-layout-camara/">&lt;div class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;

  &lt;p&gt;When Apple announced iPhone Duo, the first question was how much work it would take to adapt the app. The short answer: layouts were much easier than expected; the camera was much harder. Not because the API was especially complicated, but because the problem only shows up when the device changes shape in front of you.&lt;/p&gt;

  &lt;h2 id=&quot;an-iphone-that-is-not-just-a-bigger-iphone&quot;&gt;An iPhone that is not just a bigger iPhone&lt;/h2&gt;

  &lt;p&gt;Duo has two displays, one on the outside and one on the inside, and it can be closed, open, or partially folded. When it opens, the app can move from one display to the other, the geometry changes, and system controls may rearrange. The camera facing the user can also change direction.&lt;/p&gt;

  &lt;p&gt;That breaks a very convenient assumption we had carried over from iOS: that interface orientation and camera orientation were more or less equivalent. On a foldable, the app can change displays without a classic rotation, and the camera may still be pointing somewhere else.&lt;/p&gt;

  &lt;h2 id=&quot;layouts-arrangementview-did-most-of-the-work&quot;&gt;Layouts: ArrangementView did most of the work&lt;/h2&gt;

  &lt;p&gt;For the main structure we used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt;, the container Apple added to arrange a primary and a secondary view according to the device pose. UIKit has the equivalent &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIArrangementViewController&lt;/code&gt;.&lt;/p&gt;

  &lt;p&gt;The idea is simple:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;ArrangementView&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;LibraryView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;secondary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;DetailView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;arrangementViewStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;axes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;split&lt;/code&gt; style places both views side by side when the container is wider than it is tall, and stacks them when it is taller than it is wide. It also accounts for reserved regions such as the fold. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;overlay&lt;/code&gt; style works more like a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ZStack&lt;/code&gt;: when the device is closed or fully open it places the primary view over the secondary one, and when partially folded it separates them around the fold.&lt;/p&gt;

  &lt;p&gt;In our case, the first pass was pretty trivial. We moved from a single screen to a primary/secondary hierarchy and the system handled most of the resizing and positioning. What remained was tuning details: which view had priority in each pose, where to put controls, and how to keep important content away from the fold or the inner camera.&lt;/p&gt;

  &lt;p&gt;For the latter, don’t invent your own fold math. Apple exposes reserved regions:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;GeometryReader&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proxy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;divisions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reservedRegions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;division&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;occlusions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;reservedRegions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;occlusion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Adjust content based on active regions.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;The recommendation is to use the actual scene or container size, not fixed iPhone dimensions, and not to make layout decisions from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIInterfaceOrientation&lt;/code&gt;. On a Duo, classic orientation doesn’t tell the whole story.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Don’t place an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NavigationSplitView&lt;/code&gt;, a list, a scroll view, or any container that might make part of the view inaccessible.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;h2 id=&quot;the-camera-the-problem-the-simulator-didnt-show&quot;&gt;The camera: the problem the simulator didn’t show&lt;/h2&gt;

  &lt;p&gt;The layout could be validated pretty well in the simulator. The camera could not.&lt;/p&gt;

  &lt;p&gt;The symptom appeared when opening the device: the preview changed orientation and did not always end up level. The content resized, the preview layer moved to another display, and the rotation was no longer what we expected. In the simulator the layout looked right; the real problem needed a Duo in hand.&lt;/p&gt;

  &lt;p&gt;The root cause is that a camera sensor has a fixed physical orientation. Rotating the device does not change the sensor, and the capture pipeline does not compensate for that difference on its own. On a regular iPhone it is often enough to observe interface orientation, but on a foldable there are display and pose changes that don’t fit that model.&lt;/p&gt;

  &lt;p&gt;The solution was not to calculate angles by hand. It was to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVCaptureDevice.RotationCoordinator&lt;/code&gt; as the single source of truth.&lt;/p&gt;

  &lt;h2 id=&quot;rotationcoordinator-angles-relative-to-the-horizon&quot;&gt;RotationCoordinator: angles relative to the horizon&lt;/h2&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVCaptureDevice.RotationCoordinator&lt;/code&gt; monitors the physical orientation of a capture device and provides adjustment angles to keep images level, relative to gravity.&lt;/p&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--note&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVCaptureDevice.RotationCoordinator&lt;/code&gt; has existed since iOS 17, so it isn’t exclusive to Duo; what changes is that here it becomes the central piece.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;The coordinator publishes two angles:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;videoRotationAngleForHorizonLevelPreview&lt;/code&gt;: for the preview layer.&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;videoRotationAngleForHorizonLevelCapture&lt;/code&gt;: for captured photos and videos.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;The preview angle also takes into account the layer’s position in the view hierarchy, so it can differ from the capture angle. On Duo, both update when the app moves between displays.&lt;/p&gt;

  &lt;p&gt;Create the coordinator with the device and the preview layer:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;coordinator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AVCaptureDevice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;RotationCoordinator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;previewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previewLayer&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;Three details were key to making it work for real:&lt;/p&gt;

  &lt;ol&gt;
    &lt;li&gt;&lt;strong&gt;The layer must be in a window.&lt;/strong&gt; The coordinator measures its position in the hierarchy. If you create it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;previewLayer: nil&lt;/code&gt;, it will never report a preview angle, even if the layer appears later.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Recreate the coordinator when the camera or layer changes.&lt;/strong&gt; A coordinator is bound to one device for its lifetime. If we switch from the inner to the outer camera, or the layer moves to another window, we need a new one.&lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Read the initial values, then observe changes.&lt;/strong&gt; If you only observe, the preview may stay rotated until the first rotation.&lt;/li&gt;
  &lt;/ol&gt;

  &lt;p&gt;A minimal observer could look like this:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;@MainActor&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CameraRotationObserver&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;coordinator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AVCaptureDevice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;RotationCoordinator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;observations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;NSKeyValueObservation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]()&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AVCaptureDevice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;weak&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;previewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AVCaptureVideoPreviewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setDevice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AVCaptureDevice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setPreviewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AVCaptureVideoPreviewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;previewLayer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;device&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;observations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forEach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;invalidate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;observations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;removeAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;coordinator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AVCaptureDevice&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;RotationCoordinator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;previewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previewLayer&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coordinator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coordinator&lt;/span&gt;

        &lt;span class=&quot;nf&quot;&gt;applyPreview&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coordinator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoRotationAngleForHorizonLevelPreview&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;applyCapture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coordinator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoRotationAngleForHorizonLevelCapture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;observations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;coordinator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;observe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoRotationAngleForHorizonLevelPreview&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;weak&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;change&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;angle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;change&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newValue&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;kt&quot;&gt;MainActor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assumeIsolated&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;applyPreview&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;angle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;observations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;coordinator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;observe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoRotationAngleForHorizonLevelCapture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;weak&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;change&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;angle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;change&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newValue&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;kt&quot;&gt;MainActor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assumeIsolated&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;applyCapture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;angle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;applyPreview&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;angle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CGFloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;previewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoRotationAngle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;angle&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;applyCapture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;angle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CGFloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Send the angle to the actor that owns the capture session.&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;Applying the preview angle is a single line:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;previewLayer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoRotationAngle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;angle&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;For capture, apply the angle to each output’s video connection:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;video&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoRotationAngle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;angle&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;h2 id=&quot;the-preview-layer-detail&quot;&gt;The preview layer detail&lt;/h2&gt;

  &lt;p&gt;In SwiftUI, the preview layer usually lives inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIViewRepresentable&lt;/code&gt;. That view is built before the system installs it in a window. So it’s worth notifying the observer when the layer moves to a window:&lt;/p&gt;

  &lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PreviewView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;UIView&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;previewLayerDidMoveToWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;AVCaptureVideoPreviewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)?&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;didMoveToWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;didMoveToWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;previewLayerDidMoveToWindow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;previewLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;That callback recreates the coordinator with the layer already installed. On a Duo, the app can also move between displays, so this kind of transition is not an edge case: it’s part of normal operation.&lt;/p&gt;

  &lt;h2 id=&quot;what-the-simulator-doesnt-tell-you&quot;&gt;What the simulator doesn’t tell you&lt;/h2&gt;

  &lt;blockquote class=&quot;doc-callout doc-callout--important&quot;&gt;
    &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; The simulator is useful for validating layouts, sizes, bars, and poses, but the camera needs to be tested on a physical device.&lt;/p&gt;
  &lt;/blockquote&gt;

  &lt;p&gt;Our test list looked something like this:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Close, open, and leave the Duo partially folded with the camera active.&lt;/li&gt;
    &lt;li&gt;Rotate in every pose: portrait, landscape, tent, and laptop.&lt;/li&gt;
    &lt;li&gt;Switch between inner, outer, and rear cameras.&lt;/li&gt;
    &lt;li&gt;Verify that the preview doesn’t jump when moving from one display to the other.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;The problem was hard precisely because of this: it wasn’t a pure logic bug, it was a physical-state bug. The same configuration could fail only in a specific pose and only for a fraction of a second.&lt;/p&gt;

  &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

  &lt;p&gt;Adapting layouts to iPhone Duo was much easier than expected. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrangementView&lt;/code&gt; and reserved regions handled most of the work, and the first pass worked almost by itself.&lt;/p&gt;

  &lt;p&gt;The camera was a different story. The key was to stop thinking in terms of interface orientation and use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AVCaptureDevice.RotationCoordinator&lt;/code&gt; as the source of truth, recreating it when the device or preview layer changes. That’s when the angle dance on opening the device stopped.&lt;/p&gt;

  &lt;p&gt;If you’re adapting an app to Duo, I’d start with the layout and set aside separate time for the camera. And above all, test on a physical device: there are things the simulator simply cannot tell you.&lt;/p&gt;

  &lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/technologyoverviews/preparing-your-app-for-iphone-duo&quot;&gt;Preparing your app for iPhone Duo&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/videos/play/tech-talks/111465/&quot;&gt;Build a great camera experience for iPhone Duo&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/avkit/choosing-a-camera-by-the-direction-it-faces&quot;&gt;Choosing a camera by the direction it faces&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/avfoundation/supporting-device-rotation-in-your-camera-app&quot;&gt;Supporting device rotation in your camera app&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://developer.apple.com/documentation/avfoundation/avcapturedevice/rotationcoordinator&quot;&gt;AVCaptureDevice.RotationCoordinator&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;

&lt;/div&gt;
</content>
  </entry>
  
    
    
    
  <entry xml:lang="en">
    <title type="text">Hello world</title>
    <link href="https://www.obocat.com/blog/2026/10/23/hola-mundo/" rel="alternate" type="text/html" hreflang="en"/>
    <id>https://www.obocat.com/blog/2026/10/23/hola-mundo/</id>
    <published>2026-10-23T00:00:00+00:00</published>
    <updated>2026-10-23T00:00:00+00:00</updated>
    <summary type="text">First post on the blog: notes on iOS development, architecture and product.</summary>
    
    <category term="iOS"/>
    
    <category term="Swift"/>
    
    <content type="html" xml:base="https://www.obocat.com/blog/2026/10/23/hola-mundo/">&lt;p class=&quot;lang-en&quot; lang=&quot;en&quot;&gt;Hello world. This is the first post on the blog. I&amp;rsquo;ll share notes on iOS development, architecture and product here.&lt;/p&gt;
</content>
  </entry>
  
</feed>
