Copilot with Xcode: Use genAI to accelerate your iOS development.
The world of software development is fast-evolving. Today’s need is not only learning programming but to capitalise on edge cutting tools. GitHub Copilot, an AI-powered code completion tool by GitHub and OpenAI, is making a significant impact by generating code snippets and offering intelligent suggestions based on context. As an iOS developer, I integrated Copilot into my workflow to explore its potential and speed up development cycles.
However, GitHub Copilot does not natively support Xcode, the primary IDE for iOS development. Through research and using GPT, I learned how to leverage GitHub Copilot and Codeium to enhance my coding experience in Xcode.
This guide aims to help developers optimize their coding processes by integrating GitHub Copilot and Codeium within Xcode. We’ll explore use cases and conclude with an overview of its limitations.
Here is the screenshot of their official page listing some of the IDE they support.
Let’s embark on a detailed exploration of the setup process for GitHub Copilot, Codeium, and the Xcode extension.
Setting up GitHub Copilot:
- To set up GitHub Copilot, ensure you have access to GitHub Copilot through your GitHub account. Note that GitHub Copilot is a paid service, requiring payment information for setup. A 30-day free trial is available for learning purposes.
- Next, follow the setup instructions provided on the GitHub Copilot setup page, which typically involves installing Visual Studio Code (VS Code). However, using VS Code would mean sacrificing the autocomplete feature and visual interface for UI creation offered by Xcode. To retain these features, we’ll install a third-party extension on Xcode.
- Manually install the Copilot for Xcode.app by downloading it from the latest release. Install the application in the Application Folder and open it to set up a background running service.
- From the Apple menu located in the top-left corner of your screen click System Settings. Navigate to Privacy & Security then toward the bottom click Extensions. Click Xcode Source Editor and tick Copilot.
- The first time the app is open and the command runs, the extension will ask for the necessary permissions. To grant permissions for the Accessibility API, click Accessibility, and drag the CopilotForXcodeExtensionService.app to the list.
You can locate the extension app by clicking the Reveal Extension App in Finder in the host app.
- Update the Node setting in the Host App by accessing the Service Tab and setting the Node path. If Node is not installed, you can download it from the official website. You may get the node path by running the which node command in the terminal.
- We are now ready to set up the suggestion feature. Click on “Install” in the Copilot Language Server section. On the click, it will redirect us to the GitHub verification page, A user code will be copied to the clipboard, Paste it on the verification input box, and complete the SignUp by Clicking the “Confirm Sign-in” in the Host Application.
- Go to “Feature – Suggestion” and update the feature provider to “GitHub Copilot”.
Setting Up Codeium:
Codeium serves as an alternative to GitHub Copilot, offering a free service for individual users with unlimited usage.
The installation process is similar to GitHub Copilot as we will use the same extension for using Codeium as the service provider.
- In the host app, navigate to “Service – Codeium” to access the Codeium account settings.
- Click on “Install” to install the language server.
- Click on “Sign In” and you will be redirected to codeium.com. After signing in, a token will be provided. You need to copy and paste this token back into the app to complete the sign-in process.
- Go to “Feature – Suggestion” and update the feature provider to “Codeium”.
That was all about setting up the code suggestion feature.
Now, Let’s see in code a few use cases how it may help us. Just start typing as usual and Copilot will start assisting you with code suggestions.
Code Suggestions:
GitHub Copilot and Codeium offer valuable assistance in generating code snippets tailored to specific contexts within Xcode. For instance, upon entering “Ank,” Copilot suggested the text “Ankit Nigam” along with other necessary view modifiers.
Write Code upon request:
Upon requesting, “Add Story Model Struct and make it Codable,” Copilot generated a Codable Story model with property suggestions, demonstrating its ability to fulfill coding requests within the editor.
Refactor Code:
I tasked Copilot with refactoring a class named RelationStorage to be generic instead of saving only Strings. The result exceeded expectations, highlighting Copilot’s skill in code optimization and enhancement.
Test Case Suggestions:
Beyond code generation and refactoring, Copilot and Codeium offer valuable insights for test case generation, further enhancing code quality and reliability within Xcode.
Incorporating Chat and Code Prompts:
Advanced features like chat and code prompts require access to paid OpenAI services. Once your OpenAI account is set up with the necessary permissions, you can integrate these features into your Xcode environment for enhanced collaboration and code exploration.
Setting Up the Chat & Code Prompt:
- Please switch to the Chat Section on the Host app.
- Click on Add model. That will open a dialog to input the key. Select the Chat Model Provider and follow the steps to create keys on your choice of Provider. I create with OpenAI.
- Clip on Key Button Next to the API key dropdown, Add your keys in the prompted dialog box.
- Select the key in the API key dropdown. Click Save and then Test to check if all is set correctly.
- Jump into XCode. Setup key binding, I’ll leave it to you to set up your key. ⌥⇧P is my key to open the code prompt. Alternatively, you may explore all the options by going to Editor > CoPilot > All the Options list.
Now with the addition of Chat and Code Prompt into Xcode. We can chat about code and ask to write up code right into Xcode.
Let’s see how I was able to make use of Code Prompt and Chat Prompt.
Write Unit Test cases:
When I asked Copilot to write a unit test, it quickly generated a well-structured Test class. The class included setup methods, clear test cases, and thorough teardown procedures, demonstrating Copilot’s skill in creating test cases.
Code Explanation:
Deciphering the logic in legacy code can be challenging for developers. Yet, with the Chat prompt feature, I swiftly obtained a concise explanation, saving valuable time and accelerating my understanding of the code’s intricacies.
Code Improvement Suggestions:
Asking for code improvements paid off, with satisfactory results. There’s more to explore, but I’ll wrap up here. You can dive deeper to discover Copilot’s full capabilities within Xcode.
Summarised Use Cases:
GitHub Copilot/Codeium simplifies coding by generating boilerplate code and suggesting error handling strategies and test cases, streamlining development. It also provides insights into various coding patterns, making it ideal for learning and exploration.
Limitations:
- Quality of Suggestions: While GitHub Copilot’s suggestions can be helpful, they may not always be perfect or suitable for every situation. Developers should review and test the generated code thoroughly.
- Privacy Concerns: While providers assure that their AI models only collect code snippet data to improve functionality and quality, privacy concerns persist. Thus, cautious use is advised.
In conclusion, the integration of GitHub Copilot, and Codeium with Xcode, presents an exciting opportunity for iOS developers to elevate their coding experience. By seamlessly blending Copilot’s AI-driven suggestions with the familiar Xcode environment, developers can enhance productivity, streamline workflows, and explore new coding avenues with accelerated development cycles.