Adding SwiftUI to Objective-C Apps

Image for post
Image for post

Field Notes on freshening up legacy iOS apps

//
// BoatDetailsView.swift
//
import SwiftUIstruct BoatDetailsView : View {
var shipName = ""
var body: some View {
Text(shipName)
}
}
//
// BoatDetailsInterface.swift
//
import Foundation
import SwiftUI
@objc
class BoatDetailsInterface: NSObject {

@objc func makeShipDetailsUI(_ name: String) -> UIViewController{
var details = BoatDetailsView()
details.shipName = name
return UIHostingController(rootView: details)
}
}
#import "Boat_Watch-Swift.h"
UIViewController *detailsViewController = [[BoatDetailsInterface new] makeShipDetailsUI:@"Sarita"];[self presentModalViewController:detailsViewController animated:YES];
Image for post
Image for post

Written by

Emerging Technology R&D • PhD student, researching Blockchain, AI and Crowdworking • Mobile, IOT & Alexa App coder and product manager

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store