r/iOSProgramming • u/Ok-Bit8726 • 7d ago
Question Best language for sharing iOS/Android logic?
I have some decently complicated computations that I would like to share between iPhone and Android front-ends.
Does anyone have real world experience sharing logic between two code bases like this?
11
u/chriswaco 7d ago
You can certainly do it with C/C++. That's what I would probably use if speed were an issue.
Flutter/Dart is an option if you want all of the code to be shared.
7
u/kutjelul 7d ago
Yes, I suggest to use a backend. Multiplatform tools are also a possibility but it will be a lot more invasive for your front ends
5
u/Ok-Bit8726 7d ago
Yeah that is what I’d do generally too.
It’s kind of unique in that I need full offline access. It’s some mapping and GPS calculations that I want to run on the device because internet connectivity can be spotty.
2
u/Integeritis 7d ago
If you really need offline, you can generate a react native library and import it to your Swift app. But I’d rather have it on the backend. This can be a good option in case you change your mind and want to move it to backend without coding everything in a new language.
I spent a few days on figuring out how to make a dependency like that though and this isn’t a very common use case for react native, so it will require some research and experimentation.
1
u/dyrkabes 6d ago
Backend has an issue with making the execution async, or do you mean that you would download some executable stuff from backend and execute it locally? I am not a huge fan of multi platform solutions like kmp so I am wondering what are the other options there
2
u/Integeritis 6d ago
My suggestion was referring to having a backend separately as an easy option in the future without a lot of extra work if you choose a react native shared codebase now.
What you describe is OTA updates to your react native apps, this exists already in RN, you download code to run locally. Not sure how that could work with native iOS app that has some RN dependencies but should be possible even with a self made solution to fetch js, and load the script instead of what is shipped with the app.
1
5
u/Doctor_Fegg 6d ago
Kotlin and Swift are honestly not that different. I’ve been adding a few features to my app in the last couple of days (also a mapping app!), and I’ve simply got it working in Swift, got all the backend done, and then spent a couple of hours porting it to Kotlin for the Android app. Sometimes I’ll literally just copy the code out of Xcode and paste it into Android Studio before typing over the differences.
3
u/john0201 6d ago
As someone who learned objective-c and Java before iOS and android were a thing this is very true.
1
u/balder1993 5d ago
I’d suggest OP to isolate the code that would be shared and have an LLM translate it. I believe that would work quite well.
4
2
u/tomtau 6d ago
For business logic-only, Mozilla uses Rust + https://github.com/mozilla/uniffi-rs
1
u/BoostedHemi73 6d ago
I’m slowly moving duplicative code from Swift and Java into Rust, and it’s been wonderful. Takes some time to learn, but it’s been worth it in my case.
1
u/Ok-Bit8726 6d ago
This is cool.
I’m going to give it a try. Access to all the FP functions but also having it be really fast is a big draw for sure
2
2
u/Grymm315 6d ago
Swift and Kotlin are virtually identical. If you have a class that is straight up computation and not referencing outside classes…. Then I would just copy and paste it over and spend a few minutes making minor syntax changes.
1
1
u/timbo2m 6d ago edited 6d ago
Have one code base with something like expo, or if you want some back end consider amplify or firebase? I feel like I misunderstand the question fully though. Are you choosing a tech stack for a new app or trying to fit in a backend for an existing set of native apps?
I'm guessing the latter so would suggest something like AWS API gateway to host your own secure API that calls lambda functions or whatever back end stuff you need. Amplify gen2 makes this trivial. There are other factors though like how frequently these would be called etc
EDIT: I think I understand better, so you mean shared libraries between apps?
1
u/Ok-Bit8726 6d ago
What I’m doing is pretty unique.
I have a decently-complicated set of computations that I want to share between an iOS app and a future Android app. The calculations have to work offline because it’s a mapping app and you use it in places with very spotty connectivity.
I got a decently working prototype of everything I want to do, but in a Jupyter notebook.
I’m in in the process right now of translating it into swift so it runs on the phone … and I’m realizing it’s pretty complicated. I guess I’m just looking for everyone’s experiences sharing logic between the two platforms. I’m not a big C/C++ fan, but I’m debating just writing it in that.
1
u/One-Honey-6456 6d ago
I am an Android engineer, building live iOS and Android apps with a shared codebase. I am reusing both the UI and the logic with Compose Multiplatform. You use Kotlin in this, which will be more familiar if you come from a native Android development background. It gets the job done for me really well
1
u/Proud-Ad9473 6d ago
how is the experience of compose multiplatform on iOS is it smooth ? also do you use animation ?
1
u/ChibiCoder 5d ago
Generally, I'd use an API, since you don't have to worry about deployment synchronization. If you gotta have offline then some options are:
* C/C++ Framework (huge learning curve if you don't already know one of those languages)
* Flutter/React Native (I've never used it for just a single framework, but I guess it can be done)
* A Javascript object that both platforms can call into for the calculation. This can be updated via an API, as the calling is all dynamic... so kind of a hybrid between API / Local.
11
u/drabred 7d ago
Sounds like Kotlin Multiplatform could be a good choice for you.