Building an Efficient iOS Development Environment in Cursor: AI Programming + Live Preview

This article will explain how to properly configure Cursor/VSCode for iOS development. After setup, you'll be able to:

The configuration process is nearly identical for both Cursor and VSCode. We'll use Cursor as an example.

Why Use Cursor/VSCode for iOS Development?

Xcode's shortcomings are numerous: lack of AI features, laggy previews, and bloated layout. Most developers struggle daily with Xcode's development environment. For everyone's mental health and development efficiency, we strongly recommend using Cursor/VSCode for iOS development. Currently, Cursor offers more powerful interaction logic and AI capabilities compared to VSCode.

Setting Up the iOS Development Environment

Many developers who have tried this setup might encounter errors where the editor reports missing methods (Cannot find '***' in scope SourceKit). This issue is addressed in step 3.

1. Install the Swift and CodeLLDB Extensions

The Swift extension provides basic Swift language support, while CodeLLDB enables debugging functionality.

2. Install the SweetPad Extension

This extension can be used to configure code formatting, debugging, and other features in the editor. Feel free to explore its configuration options.

3. Configure Swift LSP to Properly Recognize Project Code

After the above steps, you might notice the editor still shows errors, with many instances of: Cannot find '***' in scope SourceKit.

This occurs because swift's sourcekit-lsp hasn't indexed your project files, preventing the editor from finding your methods. There are two solutions:

Configure Your Project Using Xcode-Build-Server

First, install it:

brew install xcode-build-server

Then, in your project's root directory, run the appropriate command based on your project file type:

xcode-build-server config -workspace *.xcworkspace -scheme <XXX>
xcode-build-server config -project *.xcodeproj -scheme <XXX>

For example, if you're using exampleProject.xcodeproj:

xcode-build-server config -project exampleProject.xcodeproj -scheme exampleProject

Restart your editor, and you can start enjoying iOS development in Cursor/VSCode. Try out the smooth AI development experience!

Setting Up Hot Reloading

One challenge when developing iOS projects in Cursor/VSCode is the inability to use Xcode's preview functionality, which can slow down UI debugging. However, we can solve this using hot reloading to preview UI changes in real-time, offering an even better experience than Xcode.

There are several ways to set up hot reloading. Here's my recommended approach:

  1. Add the Inject package in Xcode
  2. Open your target's settings -> Build Settings -> search for Other Linker Flags, and add both -Xlinker and -interposable
  3. Download the latest version of InjectionIII
  4. Launch InjectionIII. It will display a small icon in the top menu bar. Select your project directory, click the icon again, and choose Prepare Project to add injection code to all SwiftUI files.

    Prepare Project adds the .enableInjection() method and @ObserveInjection var forceRedraw to all Views. If you prefer not to add these to all Views at once, you can manually add them to specific Views.

  5. Build and run your project in Xcode. The console should display:
💉 InjectionIII connected ...
💉 Watching files under the directory ...
  1. Open your project in Cursor/VSCode. When you modify and save SwiftUI files, you'll see UI updates in real-time. If updates aren't appearing, check the console for error messages to debug.

In my experience, this setup works extremely well. UI updates happen instantly without requiring project recompilation or waiting for Preview loading, significantly improving development efficiency.

For troubleshooting or more information about hot reloading, refer to the Inject documentation.

Additional Settings

Setting Up Code Formatting

Install the appropriate formatting plugin (e.g., swift-format) using the SweetPad extension.

How to Keep File Structure Consistent Between Xcode and Cursor/VSCode?

Xcode uses Groups for file organization, which doesn't translate well to Cursor/VSCode. Files and folders created in Cursor/VSCode won't be recognized in Xcode.

To solve this, simply convert all Xcode Groups to Folders. This way, files and folders created in Cursor/VSCode will appear in Xcode and vice versa. To convert: right-click on a Group in Xcode and select Convert to Folder.

Occasional Hot Reloading Failures

Hot reloading might fail due to CoreData changes, package updates, or directory changes. Usually, reopening InjectionIII and rebuilding the project resolves these issues.

Conclusion

This setup combines AI capabilities with efficient tooling to create a more enjoyable iOS development experience. Feel free to customize these configurations to match your workflow preferences. If you have questions or suggestions, please share them in the comments. Happy coding! May your development journey be smooth and productive. 🚀