How to Run iOS App on Device in Swift: Step-by-Step Guide
To run an iOS app on a physical device in Swift, connect your device to your Mac, select it as the run destination in Xcode, and ensure your Apple Developer account is set up for code signing. Then build and run the app from Xcode, which installs it on your device for testing.
Syntax
Running an iOS app on a device involves selecting the device in Xcode and building the app. The key steps are:
Connect device: Plug your iPhone or iPad into your Mac.Select device: Choose your device from the device list in Xcode's toolbar.Set signing: Ensure your project has a valid Team and provisioning profile under Signing & Capabilities.Build and run: Press the Run button or useCmd + Rto install and launch the app on your device.
ios_swift
1. Connect your iOS device to your Mac via USB. 2. Open your project in Xcode. 3. In the top toolbar, click the device selector and choose your connected device. 4. Go to your project settings > Signing & Capabilities. 5. Select your Apple Developer Team for automatic signing. 6. Press the Run button (▶️) or Cmd + R to build and run the app on your device.
Example
This example shows how to run a simple SwiftUI app on your iPhone using Xcode.
After connecting your device and setting signing, build and run this code to see a greeting on your device.
swift
import SwiftUI @main struct MyApp: App { var body: some Scene { WindowGroup { Text("Hello, iPhone!") .padding() } } }
Output
The app launches on your iPhone and displays a white screen with the text "Hello, iPhone!" centered with padding.
Common Pitfalls
- Device not trusted: Make sure to tap "Trust" on your iPhone when prompted after connecting to your Mac.
- Signing errors: Ensure your Apple Developer account is added in Xcode Preferences and a valid team is selected.
- Provisioning profile issues: Use automatic signing to avoid manual profile management.
- Device not appearing: Restart Xcode and reconnect your device if it doesn't show up.
ios_swift
/* Wrong: No team selected in Signing & Capabilities */ // This causes build errors when running on device. /* Right: Select your Apple Developer Team */ // Xcode manages signing and provisioning automatically.
Quick Reference
Follow these quick steps to run your Swift iOS app on a device:
- Connect your iPhone/iPad to your Mac with a USB cable.
- Open your project in Xcode and select your device as the run destination.
- Go to Signing & Capabilities and select your Apple Developer Team.
- Press the Run button or use
Cmd + Rto build and install the app. - Trust your Mac on the device if prompted.
Key Takeaways
Connect your iOS device to your Mac and select it in Xcode's device list before running.
Set up automatic signing with your Apple Developer Team in the project settings.
Trust your Mac on the device when prompted to allow app installation.
Use the Run button in Xcode to build and deploy the app directly to your device.
Restart Xcode or reconnect your device if it does not appear as a run destination.