Welcome to another issue!
As I mentioned last week, I am set to launch a new newsletter on "Remote Jobs for iOS Developers.".
Many people have responded with tips, thoughts, and ideas. Thank you for your time! πͺ
The launch is planned for May. BUT I already have a draft ready for how it will look. If you would be interested in checking feel free to reply to this newsletter and I will send you a draft.
I appreciate your help and let's dive into this week's news! π
Subscribe to another newsletter I run!
3450 subscribers already! π
Weekly newsletter featuring the best tools for iOS developers. Subscribe if you like @iOS developers
Sponsored Link
Must See Webinar π: The Future of Mobile App Releases
A live webinar by the AppCircle. Speakers, Pol Piella and Ural Emekci, will talk about:
- What does the future of CI/CD look like?
- What role can AI play?
- What does platform maturity look like for your in a mobile team?
- What does 'peak'Β performance look like in mobile DevOps?
It's FREE to join. Click the link below and reserve your seat now!
Swift Around the Web
Where Swift Concurrency will run your function?
Swift Concurrency, introduced in Swift 5.5, lets us write concurrent code using async and await keywords. This blog post explains where our code runs in this environment.
There are two main execution locations: the main thread (for UI updates) and the Cooperative Thread Pool (for heavy tasks). Swift uses a few rules to decide where to run your code.
- If your code is inside an actor, it always runs on the Cooperative Thread Pool, regardless of being async or not.
- If your code is an async function outside an actor, it also runs on the Cooperative Thread Pool for efficient concurrency.
- Regular (non-async) functions run on the calling thread, meaning they don't switch threads.
Swift: Working with date. This Is all we need to know.
This blog post helps to understand how we can show dates and times correctly in our Swift apps. It can be tricky because different countries format dates differently (like April 16 vs 16/04). Here's the key points:
- There's a tool called DateFormatter that automatically formats the date based on the user's language.
- Adding days or finding how many days are in a month is simple with Calendar. It's like a built-in calculator for dates!
- Learn shortcuts for basic formatting and a tip to reuse tools for faster code.
This guide gives you the basics for working with dates and times in Swift.
Coding
Xcode bookmarks
Xcode 15 added a new way to bookmark source code annotations and search queries.
- Bookmarks help you easily revisit specific lines of code or search results.
- You can bookmark lines of code, entire files, or search queries.
- Bookmarks are displayed in a dedicated sidebar for quick access.
- Add descriptions to your bookmarks for better organization.
- Group bookmarks together to categorize them (e.g., TODOs).
- Mark bookmarks as complete when you're done with the task.
- Xcode 15 also lets you bookmark specific search queries.
Overall, bookmarks seem like a useful improvement over traditional source code annotations.
Building async button in SwiftUI
This blog post explains how to create a custom SwiftUI button that works with Swift Concurrency's async/await features.
Standard SwiftUI buttons don't natively support asynchronous operations, but with an AsyncButton we can handle long-running tasks triggered by button presses.
The key steps involve:
- Tracking the button's state to disable it during tasks.
- Extracting button logic into a reusable AsyncButton view.
- Implementing cancellation functionality using a trigger value to stop ongoing tasks.
This custom AsyncButton provides a way to integrate asynchronous operations seamlessly into the SwiftUI applications.
Key press events detection in SwiftUI
This blog post guides you through handling keyboard input in SwiftUI. You can listen for specific keys or any key press using onKeyPress modifiers. But there's a catch: views need focus to be receptive (use focusable and focused modifiers).
The onKeyPress handler provides valuable information about the pressed key, including any pressed modifier keys and the specific phase of the press (down, repeat, or up). You can even target specific key sets, like numeric digits, for focused functionality.
To ensure successful key press detection, make sure parent views aren't consuming the events by returning .handled from their onKeyPress handlers. The post also delves into focus management techniques, helping you effectively integrate keyboard interaction into your SwiftUI applications.
visionOS
Recreating Apple's beautiful visionOS search bar
This blog post explains how to create a custom search bar for SwiftUI apps on visionOS that looks similar to the search bars found in Apple's built-in apps like Music and Safari.
- Apple's SwiftUI search bar options (.searchable() and related APIs) can't achieve the desired central placement.
- The solution involves a custom SwiftUI view that utilizes a UIKit UISearchBar under the hood.
- Steps include creating the search bar, positioning it correctly, and customizing its appearance (rounded corners, removing bottom border).
This approach lets you create a centered search bar that visually matches Apple's visionOS search bars.
Design
Bar Chart creation using Swift Charts
This blog post is a guide to creating bar charts in your SwiftUI apps. It explains how to use the new Swift Charts framework to visualize your data.
The post starts with the benefits of using Swift Charts, then dives into creating a basic bar chart. It covers customizing the chart's appearance, including color and data grouping. Finally, it offers tips on controlling the data displayed in the chart.
Overall, the blog post is a helpful introduction to using Swift Charts for data visualization in SwiftUI.
Creating shapes from SVG in a SwiftUI app
This article shows how to turn SVG images into SwiftUI shapes for more customization.
- SVGs are resizable vector graphics.
- Convert them to SwiftUI shapes to dynamically change color, stroke, and more in your app.
Steps:
- Create a new SwiftUI Shape: Define a struct following the Shape protocol and implement the path(in:) method to describe the shape's path.
- Translate SVG Data: Find the path data in the SVG file and convert them to SwiftUI path commands using online tools or manually.
- Use the Shape in a View: Apply the created shape to your SwiftUI view and use modifiers to change its appearance.
This lets you work with SVG images more flexibly in your SwiftUI apps.
Other Cool Stuff
Request and check local network permission on iOS and visionOS
iPhones (iOS 14+) now ask permission to see other devices on your Wi-Fi. This blog helps app makers deal with this privacy change.
- User approval is mandatory for local network access.
- Existing code waits for permission before network actions.
- A custom function (requestLocalNetworkAuthorization()) helps to check and request permission.
Benefits:
- Easy-to-use API for requesting and checking permission.
- Permission dialog only appears once (unless the app is reinstalled).
Limitations:
- There's no way to silently check permission without initially triggering the request.
This blog provides a solution for developers to effectively manage local network access in their iOS and visionOS apps.
How to define a custom file header for a Swift Package
We can define a custom file header for a Swift package, that will then be used for all new files that we create for that package or any project.
- By default, new files have a basic header.
- You can create a file named IDETemplateMacros.plist to define a custom header.
- This file lets you include things like file name, package name, author info, and copyright.
- Remember to update your .gitignore file so it doesn't hide this new file.
This way, all new files in our package will have the custom header with all the important details.
Quick and painless persistency on iOS
This blog post explores ways to save data in our iOS app for quick retrieval later.
Here are the methods:
- Codable: Encode data as JSON and write it to disk. Ideal for structured data and works well with SwiftUI's @Observable.
- NSKeyedArchiver: For saving custom objects, especially useful if you're using Objective-C code.
- UserDefaults: Easiest option for basic data types like numbers or strings.
- Local Files: Works for existing data files like JSON or PLIST.
- AppStorage (SwiftUI): Saves data directly within SwiftUI using UserDefaults behind the scenes.
These methods are for temporary or small amounts of data. There are better solutions for large or complex data storage.
Videos
Apple guide to Metal ray tracing
This WWDC23 video dives into Metal ray tracing for game and app developers by Apple's graphics API for ultra-realistic rendering. It covers:
- The basics of the Metal ray tracing API
- Using ray tracing to enhance visual quality
- Latest improvements for creating bigger, more intricate scenes
- Techniques to reduce memory usage and build times
- Efficient rendering of complex visuals like hair and fur
By watching, you'll learn how to leverage Metal ray tracing to create stunning visuals in your games and apps.