title: Capture model identifier for Apple Watch from WatchOS
body: <p>It looks like there aren't any documented official methods of obtaining the Apple Watch model from a watch app, but there is this post showing special use of <code>sysctlbyname</code>:</p>
  <p><a href="https://stackoverflow.com/questions/49087330/how-to-determine-the-apple-watch-model">How to determine the Apple Watch model?</a></p>
  <p>Could anyone help me figure out why this function isn't working? It sometimes returns</p>
  <blockquote>
  <p>&quot;Watc\t&quot;</p>
  </blockquote>
  <p>instead of the expected</p>
  <blockquote>
  <p>&quot;Watch2,4&quot;</p>
  </blockquote>
  <p>Swift's sysctlbyname function seems to be this c function described here:
  <a href="https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/sysctlbyname.c.auto.html" rel="nofollow noreferrer">https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/sysctlbyname.c.auto.html</a></p>
  <p>My code is copied from the swift answer in the SO post:</p>
  <pre><code>private func getWatchModel() -&gt; String? {
     var size: size_t = 0
     sysctlbyname(&quot;hw.machine&quot;, nil, &amp;size, nil, 0)
     var machine = CChar()
     sysctlbyname(&quot;hw.machine&quot;, &amp;machine, &amp;size, nil, 0)
     return String(cString: &amp;machine, encoding: String.Encoding.utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
  } // Should return a string like &quot;Watch2,4&quot;
  </code></pre>