|
| 1 | +--- |
| 2 | +title: Platform Specific Handling |
| 3 | +--- |
| 4 | + |
| 5 | +Given there are often various differences between Android, iOS, visionOS, macOS, Meta Quest, etc.), how best to handle platform specific behavior? Like when a feature or behavior is only available on one and not the other. |
| 6 | + |
| 7 | +NativeScript offers many ways to deal with platform specific cases. |
| 8 | + |
| 9 | +:::tip |
| 10 | +In general, never sacrifice the quality of a target feature for the sake of cross platform compatibility. Your job is to deliver a superior experience for your users and thus NativeScript gives you a lot to work with in this regard. |
| 11 | +::: |
| 12 | + |
| 13 | +## Android |
| 14 | + |
| 15 | +There are many Android specific features which may only be accessible within the Android ecosystem. Let's start by looking at language specific features. |
| 16 | + |
| 17 | +### Java |
| 18 | + |
| 19 | +NativeScript gives you access to Java API's from TypeScript, however you may just want to work with Java and/or integrate Java features on Android devices. You can do so as follows: |
| 20 | + |
| 21 | +1. Add `App_Resources/Android/src/main/java/com/company/features/Custom.java` |
| 22 | +2. You can now access `com.company.features.Custom` and any public API implementations from your TypeScript code. |
| 23 | + |
| 24 | +For example, `Custom.java`: |
| 25 | + |
| 26 | +```java |
| 27 | + |
| 28 | +``` |
| 29 | + |
| 30 | +Upon running your NativeScript project, the `Custom.java` becomes accessible to your TypeScript code: |
| 31 | + |
| 32 | +```ts |
| 33 | +const custom = new com.company.features.Custom(); |
| 34 | +console.log(custom.name()); // 'NativeScript' |
| 35 | +``` |
| 36 | + |
| 37 | +### Kotlin |
| 38 | + |
| 39 | +NativeScript gives you access to many Kotlin API's from TypeScript, however you may just want to work with Kotlin and/or integrate Kotlin features on Android devices. You can do so as follows: |
| 40 | + |
| 41 | +1. Add `App_Resources/Android/src/main/java/com/company/features/Custom.kt` |
| 42 | +2. You can now access `com.company.features.Custom` and any public API implementations from your NativeScript project. |
| 43 | + |
| 44 | +For example, `Custom.kt`: |
| 45 | + |
| 46 | +```java |
| 47 | + |
| 48 | +``` |
| 49 | + |
| 50 | +### Jetpack Compose |
| 51 | + |
| 52 | +[Jetpack Compose](https://developer.android.com/compose) is "Android’s recommended modern toolkit for building native UI" and thus is not available on iOS. However with NativeScript you are never limited and can use Jetpack Compose using [@nativescript/jetpack-compose](https://docs.nativescript.org/plugins/jetpack-compose) for particular feature implementations or entire UI's if desired. |
| 53 | + |
| 54 | +A great article on using Jetpack Compose with NativeScript is [here](https://dev.to/valorsoftware/introduction-to-jetpack-compose-for-nativescript-54d7). |
| 55 | + |
| 56 | +## iOS |
| 57 | + |
| 58 | +There are many iOS specific features which may only be accessible within the Apple ecosystem. Let's start by looking at language specific features. |
| 59 | + |
| 60 | +### Objective C |
| 61 | + |
| 62 | +### Swift |
| 63 | + |
| 64 | +### SwiftUI |
| 65 | + |
| 66 | +[SwiftUI](https://developer.apple.com/xcode/swiftui/) "helps you build great-looking apps across all Apple platforms with the power of Swift" and thus is not available on Android. However with NativeScript you are never limited and can use SwiftUI using [@nativescript/swift-ui](https://docs.nativescript.org/plugins/swift-ui) for particular feature implementations or entire UI's if desired. |
| 67 | + |
| 68 | +A great article on using SwiftUI with NativeScript is [here](https://dev.to/valorsoftware/introduction-to-swiftui-for-nativescript-4m1b). |
0 commit comments