Sitemap

Automatic update for new Xcode version

4 min readNov 3, 2024

--

Press enter or click to view image in full size

Motivation

Apple requires app developers to use the latest version of Xcode to upload their apps to the store. Starting April 24th, 2024, apps must be built with Xcode 15.

Available Methods

In order to be notified when the latest Xcode is released, there are several options:

  1. Periodically check the app store here
  2. Periodically check the Apple developer site here
  3. Register for mailing lists from Apple here from your developer account

All these options require manual intervention, which is a push-based approach, where you actively seek the updates. Wouldn’t it be nice if you can get notified of this? Here’s how:

Automated Version Checking

Unfortunately, Apple doesn’t give API to receive this information, but they have it in an html site, so the process is as follows:

    Get Guy Lis’s stories in your inbox

    Join Medium for free to get updates from this writer.

  1. Write a script that parses this site, and retrieves the latest Xcode version.
  2. Use git actions to create a scheduled job running on an Xcode machine and installs the Xcode you’re building your apps on.
  3. Run the script above daily using git actions, comparing the version in the Apple site and the one in the machine.
  4. When the versions differ, GitHub Actions can send you an email notification about the new version

Examples

Example for script parsing the site, retrieving the local Xcode version and comparing them

import urllib.request
import subprocess

from html.parser import HTMLParser


# Step 1: Fetch the Web Page
def fetch_releases_page():
url = "https://developer.apple.com/news/releases/"
try:
with urllib.request.urlopen(url) as response:
return response.read().decode('utf-8')
except Exception as e:
print(f"Failed to fetch the page. Error: {e}")
return None


# Step 2: Parse the HTML Content
class XcodeVersionParser(HTMLParser):
def __init__(self):
super().__init__()
self.latest_version = 0
self.in_article = False
self.in_title = False

def handle_starttag(self, tag, attrs):
if tag == 'article':
self.in_article = True
if self.in_article and tag == 'h2':
self.in_title = True

def handle_endtag(self, tag):
if tag == 'article':
self.in_article = False
if tag == 'h2':
self.in_title = False

def handle_data(self, data):
if self.in_title and 'Xcode' in data:
# check if version includes beta or RC
if ('beta' in data) or ('RC' in data):
return
# Extract version in the form of 15.4 from 15F31d
version_parts = data.split(' ')
if len(version_parts) > 1:
version_str = version_parts[1]

# Parse version to a float for comparison
try:
current_version = float(version_str)

# If self.latest_version is not set or current version is higher, update latest_version
if (not hasattr(self, 'latest_version')) or current_version > self.latest_version:
self.latest_version = current_version
except ValueError:
return


def parse_xcode_version(html_content):
parser = XcodeVersionParser()
parser.feed(html_content)
return parser.latest_version


def get_xcode_version():
try:
# Run the xcodebuild command and capture its output
result = subprocess.run(['xcodebuild', '-version'], capture_output=True, text=True, check=True)

# Extract the version line from the output
output = result.stdout
lines = output.splitlines()
for line in lines:
if line.startswith('Xcode'):
# Extract the version number after 'Xcode '
version = line.split(' ')[1]
return version

raise ValueError("Xcode version not found in the output.")

except subprocess.CalledProcessError as e:
print(f"Error executing xcodebuild: {e}")
except Exception as e:
print(f"An error occurred: {e}")


# Step 4: Combine Everything
def check_for_new_xcode_version():
html_content = fetch_releases_page()
if not html_content:
return ""

latest_version = parse_xcode_version(html_content)
if not latest_version:
print("No Xcode version found.")
return ""

local_version = get_xcode_version()
if local_version != latest_version:
print(f"New Xcode version found: {latest_version}")
return latest_version
else:
print("No new Xcode version.")
return ""


if __name__ == "__main__":
new_version = check_for_new_xcode_version()

Example for git action installing a specific Xcode version and running the script

  check-xcode-version:
runs-on: macos-14
outputs:
new_version: ${{ steps.check-xcode.outputs.new_version }}

steps:
- name: Set up Xcode version
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.4'

- name: Checkout code
uses: actions/checkout@v2

- name: Check Xcode version
id: check-xcode
run: |
new_version=$(python ./scripts/checkXcodeVersion.py)
echo "Xcode version: $new_version"
echo "::set-output name=new_version::$new_version"

Example for git action, sending a mail for the new Xcode version

name: Send Email
inputs:
email-recipient:
description: The email to notify to
type: string
required: true
email-subject:
description: Email Subject
type: string
required: true
email-body:
description: Email Body
type: string
required: true
email-password:
description: The password of the email account to send the notification from
required: true
runs:
using: composite
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Send mail
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.gmail.com
server_port: 465
username: **********
password: ${{ inputs.email-password }}
subject: ${{ inputs.email-subject }}
to: ${{ inputs.email-recipient }}
from: Github Actions
html_body: ${{ inputs.email-body }}

You can find further documentation for GitHub Actions here

Conclusion

While there is a potential issue if Apple’s site changes, causing the script to break, this is a rare occurrence. In the worst case, you won’t receive the notification, but this method ensures you’re generally aware of Xcode releases and can decide when to update your app accordingly.

--

--

Guy Lis
Guy Lis

Written by Guy Lis

Senior Mobile Developer at Unity | Developing a Mobile SDK for native Android & IOS