Introduction au développement natif iOS/Android

Développement d'applications mobiles natives avec Swift (iOS) et Kotlin (Android).

Swift (iOS)

// Vue de base
import SwiftUI

struct ContentView: View {
    @State private var text = ""

    var body: some View {
        NavigationView {
            VStack {
                TextField("Entrez du texte", text: $text)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding()

                Button(action: {
                    // Action
                }) {
                    Text("Valider")
                        .foregroundColor(.white)
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(8)
                }

                List {
                    ForEach(1...5, id: \.self) { item in
                        Text("Item \(item)")
                    }
                }
            }
            .navigationTitle("Mon App")
        }
    }
}

// Modèle de données
struct User: Identifiable, Codable {
    var id: UUID
    var name: String
    var email: String

    init(name: String, email: String) {
        self.id = UUID()
        self.name = name
        self.email = email
    }
}

Kotlin (Android)

// Activity principale
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textInput = findViewById(R.id.textInput)
        val button = findViewById

Navigation et vues

Swift Navigation

// Navigation SwiftUI
struct NavigationExample: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: DetailView(item: item)) {
                    Text("Voir détails")
                }
            }
        }
    }
}

// Gestion des routes
struct AppView: View {
    var body: some View {
        TabView {
            HomeView()
                .tabItem {
                    Label("Accueil", systemImage: "house")
                }
            ProfileView()
                .tabItem {
                    Label("Profil", systemImage: "person")
                }
        }
    }
}

Kotlin Navigation

// Navigation avec Jetpack
// nav_graph.xml
<navigation xmlns:android="...">
    <fragment
        android:id="@+id/homeFragment"
        android:name=".HomeFragment">
        <action
            android:id="@+id/toDetail"
            app:destination="@id/detailFragment" />
    </fragment>
</navigation>

// Fragment
class HomeFragment : Fragment() {
    private val navController by lazy {
        findNavController()
    }

    fun navigateToDetail() {
        navController.navigate(
            R.id.toDetail,
            bundleOf("itemId" to itemId)
        )
    }
}

Fonctionnalités natives

// Swift - Accès caméra
import AVFoundation

class CameraManager {
    func requestAccess() {
        AVCaptureDevice.requestAccess(for: .video) { granted in
            if granted {
                self.setupCamera()
            }
        }
    }
}

// Swift - Localisation
import CoreLocation

class LocationManager: NSObject, CLLocationManagerDelegate {
    private let manager = CLLocationManager()

    func requestLocation() {
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager,
                        didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else { return }
        print("Location: \(location)")
    }
}

APIs système

// Kotlin - Permissions
private fun checkCameraPermission() {
    when {
        ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.CAMERA
        ) == PackageManager.PERMISSION_GRANTED -> {
            startCamera()
        }
        else -> {
            requestPermissions(
                arrayOf(Manifest.permission.CAMERA),
                CAMERA_PERMISSION_CODE
            )
        }
    }
}

// Kotlin - Services en arrière-plan
class MyService : Service() {
    override fun onBind(intent: Intent): IBinder? = null

    override fun onStartCommand(
        intent: Intent?,
        flags: Int,
        startId: Int
    ): Int {
        // Logique du service
        return START_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}