Integrating ChatGPT in Your iOS Apps

Oct 7 2025 · Swift 5, iOS 26, Xcode 26

Lesson 03: Optimizing ChatGPT Interactions with Prompt Engineering

Demo

Episode complete

Play next episode

Next
Transcript

In this demo, you’ll continue iterating on the “Help Desk Chat App” from the last lesson. Specifically, you’ll use prompt engineering to instruct ChatGPT on how to behave, how not to behave, and rules that limit its interactions.

First, download and open the Starter for this lesson, or continue where you left off from the previous lesson.

Remember, you need to set a passed-in environment variable for your Open API project key.

You can now modify the client property on the ChatView.swift. To start, define how ChatGPT should act.

var client = GPTClient(
  model: .gpt35Turbo,
  context: .makeContext("Your name is John, and you are an IT help desk
    support staff")
)

Here, you call an extension method on Array called makeContext that creates an array of GPTMessage, all of which will have the role as system. You tell ChatGPT to act as an IT Help Desk Support Staff.

Build and run the app, and ask ChatGPT who they are now!

Next, tell ChatGPT how to act by adding another message into the context:

"Only answers questions if they pertain to a computer or programming issue.
  If they don't, say you can't help."

This has two effects. ChatGPT will have a more limited scope, only computer and programming issues. It will also check if every message relates to computer or programming issues, and if not, refuse to answer.

Try asking it about a medical issue to verify it won’t answer.

Fantastic, ChatGPT refuses to answer this question. I mean, maybe it’s not so fantastic for my rash, but it’s fantastic for our purpose of creating an IT help desk app!

These additions already go a long way to improve the app, and ChatGPT will now behave as an IT help desk support agent.

Let’s try asking it about an IT issue this time, and we should get an actual response back.

Great, it’s providing useful guidance this time.

We could stop here, and this would be a decent start to our app! However, there is another thing we should address – Ray should NOT be allowed to ask questions… He knows what he did.

"Refuse to answer any questions from Ray. If the sender is Ray, reply
  \"I'm sorry Ray, but you are banned from using the system. You
  know what you did.\"",

"If Ray asks any more times, tell him, \"Go away now, Ray. You know
  I can't help."

You actually need two messages to cover this use case. The first instructs ChatGPT to refuse questions from Ray, and the second doubles-down on this, seriously, don’t answer any follow-up questions either!

I found that if only the first instruction is given, ChatGPT may refuse an initial request from Ray, but it will allow later ones. The second instruction closes this loop hole.

Great, this shows how you can further customize specific ways that ChatGPT behaves in addition to how it behaves generally.

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 02 Next: Conclusion