Integrating GitHub Copilot with Xcode: Enhance Your Coding Experience
Integrating GitHub Copilot with Xcode: Enhance Your Coding Experience

Integrating GitHub Copilot with Xcode: Enhance Your Coding Experience

Pramod Kumar
AppleCommunity
Published in
7 min readJun 10, 2024

--

As developers šŸ§‘ā€šŸ’», we are always on the lookout for tools that can enhance our productivity and streamline our coding process. GitHub Copilot, an AI-powered code completion tool, has emerged as a game-changer in the development community. While primarily known for its integration with popular code editors like Visual Studio Code, you can also leverage its capabilities within Xcode, Appleā€™s premier IDE for macOS and iOS development.

What is GitHub Copilot? šŸ¤”

GitHub Copilot is an AI pair programmer that helps you write code faster and with fewer errors. By leveraging machine learning models trained on a vast amount of open-source code, Copilot suggests whole lines or blocks of code as you type, making it easier to tackle complex problems, understand unfamiliar codebases, and accelerate routine coding tasks.

Today here, we will be discussing about how we can integrate GitHub Copilot in Xcode to enhance and speed up the coding practice while developing iOS applications.

We will be completing this discussion in two parts first GitHub Copilot integration in Xcode, second how to use GitHub Copilot to generate code snippet for iOS application.

Wow!!

How to Enable and Integrate GitHub Copilot with Xcode

Enabling GitHub Copilot on your GitHub account is a straightforward process. Follow these steps to get started:

Step ā€” 1: Enable GitHub Copilot on you Github account

  1. Sign In to GitHub:
    -
    Open your web browser and go to GitHub.
    - Sign in to your GitHub account. If you donā€™t have an account, create one by clicking on the ā€œSign upā€ button.
  2. Access GitHub Copilot Settings:
    -
    After signing in, click on your profile picture in the upper right corner to open the user menu.
    - Select ā€œSettingsā€ from the dropdown menu.
  3. Enable GitHub Copilot:
    -
    In the left sidebar, scroll down and find ā€œGitHub Copilotā€ under the ā€œCode, planning, and automationā€ section.
    - On the GitHub Copilot settings page, you will see an option to enable GitHub Copilot.
    - Click on the ā€œEnable GitHub Copilotā€ button. You may be prompted to authorize GitHub Copilot. Review the permissions and click ā€œAuthorizeā€ if prompted.
  4. Choose Your Copilot Plan:
    -
    GitHub Copilot may offer different subscription plans. Choose the plan that best suits your needs. There is a free trial available for you to try Copilot before committing to a subscription.

HerešŸ‘‡ it shows how we can enable GitHub Copilot free trail.

Enable GitHub Copilot on you Github account
Enable GitHub Copilot on you Github account

Step ā€” 2: Integrate GitHub Copilot Extension with Xcode

Although GitHub Copilot does not natively support Xcode, you can still use it in conjunction with CopilotForXcode and IDE Xcode.
Here we are using an extension CopilotForXcode by Shx Guo (Intitni).

After successfully installation of Copilot extension with Xcode, you can simply type the prompts ( instructions or queries you enter into the artificial intelligenceā€™s (AI) interface to get responses), GitHub Copilot will generate the suggestion with the generated code on the basis of your typed input prompts.

While going to install the CopilotForXcode extension, we must be sure that you Mac machine have Node installed already. if not you can download and install Node from the official website.

We can install the CopilotForXcode extension using Homebrew.

brew install --cask copilot-for-xcode

for some Mac machine may need to add 0-ad like:

brew install --cask 0-ad copilot-for-xcode

Also, we can directly download the extension from CopilotForXcode and install the app.

Now, letā€™s enable the Copilot extension to get integrate in Xcode. To do so we can go Setting App > Privacy & Security > Extensions > Xcode Source Editor and mark the checkbox next to Copilot.

HerešŸ‘‡ it shows how we can enable GitHub Copilot extension for Xcode:

Enable Copilot Extension in Settings

So, here we are done with installing and enabling Copilot extension for Xcode. But before using it we must need to link Copilot extension with GitHub account.
To do so, follow the below steps:
1. Open ā€œCopilot for Xcodeā€ app.
2. Select options ā€œGitHub Copilotā€ in ā€œServiceā€ tab.
3. Choose ā€œSign Inā€, will popup a 6-digits code and redirect to GitHub in browser.
4. Enter the 6-digits code in browser and continue.
5. Click ā€œInstallā€ Copilot.vim

HerešŸ‘‡ this image shows how to link GitHub account with extension:

Integrate GitHub Copilot Extension with Xcode
Integrate GitHub Copilot Extension with Xcode

By following these steps, you can leverage the powerful AI capabilities of GitHub Copilot to enhance your coding experience in Xcode.

Practices

Now, we have liked GitHub Copilot extension with Xcode. So, itā€™s time to practice our copilot for generating the code.
Letā€™s try it to generate a simple code for adding two number and check if sum is even or odd.

Letā€™s try it together
  1. Write a function to add two numbers and check if the sum is even or odd.
    HerešŸ‘‡ is the snippet of code suggested by Copilot
Write a function to add two numbers and check if the sum is even or odd.
Write a function to add two numbers and check if the sum is even or odd.

Now, letā€™s write the unit test cases for the same suggested code.
2. Write the unit test cases to test the above function.
HerešŸ‘‡ is the snippet of code suggested by Copilot

Write the unit test cases to test the above function
Write the unit test cases to test the above function

3. Letā€™s try with more complex example with BDD and MVVM architecture.

Letā€™s ask to create a Movie app. Generated code should load movies with some information like movie name, release date, rating and director name. All the movies should be sorted by the movie name in alphabetic order. It should use MVVM architecture and provide unit test cases.

Prompts Instructions:

/* 

Loading movies details successfully for three movies
GIVEN the target movies are Lagaan, 3 Idiots and Anand
AND for Lagan, directed by Ashutosh Gowariker, released in 2001, with rating of 8.1,
AND for 3 Idiots, directed by Rajkumar Hirani, released in 2009, with rating of 8.4,
AND for Anand, directed by Hrishikesh Mukherjee, released in 1971, with rating of 8.1,
AND the app has started to load the movies details for the target movies
WHEN loading finishes successfully
THEN there should be three movies loaded, Lagaan, 3 Idiots and Anand
AND the movies should be in alphabetic order


*/

// Write code for above scenario using MVVM architecture pattern and unit test case for view model
Behaviour Driven Development BDD using the MVVM architecture with the help of Github Copilot

Cancellation for GitHub Copilot Subscription

Canceling your GitHub Copilot subscription is a straightforward process. HerešŸ‘‡ are the steps to cancel your subscription:

  1. Sign In to GitHub:
    -
    Open your web browser and go to GitHub.
    - Sign in to your GitHub account.
  2. Navigate to Billing & Plans:
    -
    Click on your profile picture in the upper right corner of the page.
    - Select ā€œSettingsā€ from the dropdown menu.
    - In the left sidebar, click on ā€œBilling & plansā€ under the ā€œAccessā€ section.
  3. Manage Your Copilot Subscription or Add-Ons Trail:
    -
    In the Plan & Usage section, you will see your active Add-ons.
    - Locate the GitHub Copilot Add-Ons. There will be an option to cancel the trail.
    - Make confirmation and cancel the trails.
  4. Confirmation:
    -
    After completing the cancellation process, you should see ā€œYour next Paymentā€ as $0 in ā€œBilling Summaryā€ section.

HerešŸ‘‡ it shows how to cancel Copilot subscription:

Cancellation for GitHub Copilot Subscription
Cancellation for GitHub Copilot Subscription

Limitations of GitHub Copilot šŸ¤«

GitHub Copilot is an innovative AI tool that can greatly enhance the development process by providing code suggestions and completions.

Donā€™t tell anyone

However, it has several limitations that developers should be aware of:

  • Copilot may suggest code that is not the most efficient or optimal solution for the problem at hand, leading to performance issues or maintenance challenges. Such as, in the first example the addition of two number and check even and odd, can be written in more optimised way like:
func addTwoNumbers(_ a: Int, _ b: Int) -> String {
return ((a + b) % 2 == 0) ? "Even" : "Odd"
}
  • Since Copilot is trained on public code repositories, it might suggest code that contains security vulnerabilities or follows poor security practices.
  • It might not fully understand or manage dependencies and interactions within complex systems, potentially leading to broken functionality.
  • Copilotā€™s training data includes publicly available code, some of which may be under restrictive licenses. This can lead to suggestions that inadvertently violate software licenses.

Contacting Support

If you encounter any issues during the cancellation process or have specific questions, you can contact GitHub support for assistance:

By following these steps, you can easily cancel your GitHub Copilot subscription and stop any future billing.

Conclusion

Integrating GitHub Copilot with Xcode, albeit indirectly, can greatly enhance your development workflow. By adopting this hybrid approach, you can take full advantage of Copilotā€™s AI-powered code suggestions to write better code faster and make your development process more efficient and enjoyable. Give it a try and experience the future of coding today!

šŸ‘©ā€šŸ’» !!! HAPPY CODING !!! šŸ‘Øā€šŸ’»

Thank you for reading, please hit the recommend icon if like this collection šŸ˜Š . Questions? Leave them in the comment.

--

--