Copilot with Xcode: Use genAI to accelerate your iOS development.

22 / May / 2024 by Ankit Nigam 0 comments

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.

Github Copilot Supported IDE

Github Copilot Supported IDE

Let’s embark on a detailed exploration of the setup process for GitHub Copilot, Codeium, and the Xcode extension.

Setting up GitHub Copilot:

  1. 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.

    Enable Xcode Extension

    Enable Xcode Extension

  2. 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.
  3. 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.
  4. 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.
    Extension for Xcode Source Editor

    Extension for Xcode Source Editor

  5. 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.

    Grant permissions for the Accessibility API

    Grant permissions for the Accessibility API

  6. 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.

    Update Node path

    Update Node path

  7. 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.

    GitHub verification page

    GitHub verification page

  8. 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.

  1. In the host app, navigate to “Service – Codeium” to access the Codeium account settings.
  2. Click on “Install” to install the language server.
  3. 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.
  4. Go to “Feature – Suggestion” and update the feature provider to “Codeium”.

    Select Provider

    Select Provider

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:

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:

Write Code upon request

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:

Refactor Code

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:

Test Case Suggestions

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:

  1. Please switch to the Chat Section on the Host app.
  2. 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.
  3. Clip on Key Button Next to the API key dropdown, Add your keys in the prompted dialog box.
  4. Select the key in the API key dropdown. Click Save and then Test to check if all is set correctly.
  5. 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.
Add Chat Model Keys

Add Chat Model Keys

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:

Write Unit Test cases:

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:

Code Explanation

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:

Code Improvement Suggestions

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:

  1. 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.
  2. 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.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *