diff --git a/content/guide/platform-specific-handling.md b/content/guide/platform-specific-handling.md new file mode 100644 index 00000000..4db5d977 --- /dev/null +++ b/content/guide/platform-specific-handling.md @@ -0,0 +1,68 @@ +--- +title: Platform Specific Handling +--- + +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. + +NativeScript offers many ways to deal with platform specific cases. + +:::tip +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. +::: + +## Android + +There are many Android specific features which may only be accessible within the Android ecosystem. Let's start by looking at language specific features. + +### Java + +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: + +1. Add `App_Resources/Android/src/main/java/com/company/features/Custom.java` +2. You can now access `com.company.features.Custom` and any public API implementations from your TypeScript code. + +For example, `Custom.java`: + +```java + +``` + +Upon running your NativeScript project, the `Custom.java` becomes accessible to your TypeScript code: + +```ts +const custom = new com.company.features.Custom(); +console.log(custom.name()); // 'NativeScript' +``` + +### Kotlin + +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: + +1. Add `App_Resources/Android/src/main/java/com/company/features/Custom.kt` +2. You can now access `com.company.features.Custom` and any public API implementations from your NativeScript project. + +For example, `Custom.kt`: + +```java + +``` + +### Jetpack Compose + +[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. + +A great article on using Jetpack Compose with NativeScript is [here](https://dev.to/valorsoftware/introduction-to-jetpack-compose-for-nativescript-54d7). + +## iOS + +There are many iOS specific features which may only be accessible within the Apple ecosystem. Let's start by looking at language specific features. + +### Objective C + +### Swift + +### SwiftUI + +[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. + +A great article on using SwiftUI with NativeScript is [here](https://dev.to/valorsoftware/introduction-to-swiftui-for-nativescript-4m1b). \ No newline at end of file diff --git a/content/guide/platform-version-handling.md b/content/guide/platform-version-handling.md new file mode 100644 index 00000000..54ae5e5b --- /dev/null +++ b/content/guide/platform-version-handling.md @@ -0,0 +1,168 @@ +--- +title: Platform Version Handling +--- + +There are several key things to understand about platform version handling within the scope of your NativeScript project. Platform versioning pertains to specific SDKs shipped with Android, iOS, visionOS, macOS, Meta Quest, etc. + +In general, maintaining `package.json` dependency versions is often a familiar topic with JavaScript developers. We recommend [this article](https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Client-side_tools/Package_management) as well as [our overview here](https://docs.nativescript.org/guide/development-workflow/using-packages#package-managers) if not familiar with JavaScript package managers. + +This document however focuses on a topic beyond the JavaScript ecosystem. + +# How to manage platform versions + +## Android + +There are 2 key Android files in your project to keep an eye on with platform versioning over time: + +1. `app.gradle`: [Gradle](https://gradle.org/) build configuration for Android which contains minimum, maximum and build-tools SDK targets. + +We can specify target SDKs as follows: + +```groovy +android { + compileSdkVersion 34 + buildToolsVersion "34" + defaultConfig { + minSdkVersion 24 + targetSdkVersion 34 + versionName = "1.0.0" + versionCode = 1 + } +} +``` + +Every year, platforms often bump minimum requirements for public store deployments to be accepted which can often be seen [here for Google](https://developer.android.com/google/play/requirements/target-sdk). For example, it may be stated like this: + +> New apps and app updates must target Android 14 (API level 34) or higher to be submitted to Google Play; except for Wear OS and Android TV apps, which must target Android 13 (API level 33) or higher. +Existing apps must target Android 13 (API level 33) or higher to remain available to new users on devices running Android OS higher than your app's target API level. + +It's good to refer to these platform docs from time to time (*email notifications are also often sent to Play Store accounts on these evolving requirements*) to ensure your app's targets keep up with store requirements. + +2. `before-plugins.gradle`: (*optional*) Some plugins may fallback to gradle configured versions which you can define here to also help align with app build configuration targets. + +We can specify additional gradle versions as follows: + +```groovy +ext { + compileSdkVersion = 34 + buildToolsVersion = "34" + minSdkVersion = 24 + targetSdkVersion = 34 +} +``` + +### Android Version Errors with Solutions + +#### Error Sample A + +```bash +1. Dependency 'androidx.appcompat:appcompat-resources:1.6.1' requires libraries and applications that + depend on it to compile against version 33 or later of the + Android APIs. + + :app is currently compiled against android-31. + + Recommended action: Update this project to use a newer compileSdk + of at least 33, for example 34. +``` + +#### Error Solution A + +This one is a bit more self explanatory since the error includes a recommended action. Just targeting higher sdk version would resolve this one. + +#### Error Sample B + +```bash +platforms/android/app/build.gradle' line: 574 +A problem occurred configuring project ':app'. +Could not find androidx.dynamicanimation:dynamicanimation:1.1.2 +``` + +#### Error Solution B + +This is often a misspelled plugin name or invalid version. In this particular error, it's that 1.1.2 of that library does not exist; it's actually `1.1.0-alpha03` for example. + +## iOS + +There are 2 key iOS files in your project to keep an eye on with platform versioning over time: + +- `App_Resources/iOS/build.xcconfig`: Sets minimum iOS deployment version + +We can specify the minimum target as follows: + +```bash +IPHONEOS_DEPLOYMENT_TARGET = 17.0; +``` + +Every year, platforms often bump minimum requirements for public store deployments to be accepted which can often be seen [here for Apple](https://developer.apple.com/ios/submit/). For example, the `Build with Xcode` section may often state something like this: + +> All iOS and iPadOS apps uploaded to App Store Connect must be built with a minimum of Xcode 15 and the iOS 17 SDK. Starting April 2025, all iOS and iPadOS apps uploaded to App Store Connect must be built with the iOS 18 SDK. + +It's good to refer to these platform docs from time to time (*email notifications are also often sent to App Store accounts on these evolving requirements*) to ensure your app's targets keep up with store requirements. + +- `App_Resources/iOS/Podfile`: (*optional*) If your project brings in plugins that involve [Cocoapods](https://cocoapods.org/) it's a good idea to have one of these. It can help align platform version minimums to match your build.xcconfig. + +We can make our `Podfile` match our build.xcconfig target versions as follows: + +```ruby +platform :ios, '17.0' + +post_install do |installer| + installer.pods_project.targets.each do |target| + target.build_configurations.each do |config| + config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '17.0' + end + end +end +``` + +It's generally a good practice to have these match. + +### iOS Version Errors with Solutions + +#### iOS Version Error Sample A + +``` +node_modules/@nativescript/swift-ui/platforms/ios/src/Common/View+Modifiers.swift:874:49: error: 'accessibilitySortPriority' is only available in iOS 14.0 or newer + view = AnyView(view.accessibilitySortPriority(priority)) + +RichTextKit/Sources/RichTextKit/Format/RichTextFormatToolbarBase.swift:148:31: error: 'bordered' is only available in iOS 15.0 or newer + .buttonStyle(.bordered) + +RichTextKit/Sources/RichTextKit/Colors/RichTextColor+Picker.swift:175:22: error: 'foregroundStyle' is only available in iOS 17.0 or newer + .foregroundStyle(foregroundColor) +``` + +### iOS Version Error Solution A + +This is related to 2 causes: + +- A plugin in use, @nativescript/swift-ui, uses a modifier that is only available on iOS 14.0 or newer. +- Another plugin or internal implementation in the project use uses Swift APIs that are only available in 15 and 17 or newer. + +So what to do in a case like this? You have several options. + +A. Set an `IPHONEOS_DEPLOYMENT_TARGET` in your `App_Resource/iOS/build.xcconfig` to the highest minimum version stated. In this case, it would be 17.0. + +``` +IPHONEOS_DEPLOYMENT_TARGET = 17.0; +``` + +B. Make a change to source code to properly wrap the implementations with [availability](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/statements/#Availability-Condition) decorators to ensure that platform version specific code does not cause an issue if your app is run on an older devicie. You can use [patch-package](https://www.npmjs.com/package/patch-package) to create the diff to manage custom source code changes made to plugins. + +``` +var body: some View { + if #available(iOS 17.0, *) { + ZStack { + // ... something only available on iOS 17 or newer + } + } else { + // fallback + EmptyView() + } +} +``` + +## Other Considerations + +It's common for NativeScript plugins to include a `platforms/{ios|android}` folder which merge various platform dependencies in with your project. These will often include Cocoapods, gradle plugins, or just platform specific code. It's possible these plugins may specify SDK's which need an update from time to time to match the store requirements mentioned above. You can contact plugin authors or become involved in open source yourself by helping keep your plugins up to date. diff --git a/content/guide/unexpected-error-handling.md b/content/guide/unexpected-error-handling.md new file mode 100644 index 00000000..1c280564 --- /dev/null +++ b/content/guide/unexpected-error-handling.md @@ -0,0 +1,11 @@ +--- +title: Unexpected Error Handling +--- + +Beyond the standard error handling [mentioned here](/guide/error-handling), what to do with completely unexpected errors? + +Let's look at a few examples here. + +## Android + +## iOS \ No newline at end of file diff --git a/content/sidebar.ts b/content/sidebar.ts index f9e0ea58..8cb8f09c 100644 --- a/content/sidebar.ts +++ b/content/sidebar.ts @@ -161,10 +161,18 @@ export default [ { text: 'Fundamental Concepts', items: [ + { + text: 'Accessibility', + link: '/guide/accessibility', + }, { text: 'Animations', link: '/guide/animations', }, + { + text: 'Best Practices', + link: '/best-practices/', + }, ...coreSidebarItems, { text: 'Data Binding', @@ -199,7 +207,7 @@ export default [ { text: 'Styling', link: '/guide/styling', - }, + } ], }, { @@ -254,14 +262,6 @@ export default [ text: 'Shared Element Transitions', link: '/guide/shared-element-transitions', }, - { - text: 'Accessibility', - link: '/guide/accessibility', - }, - { - text: 'Best Practices', - link: '/best-practices/', - }, { text: 'Multithreading', link: '/guide/multithreading', @@ -296,6 +296,18 @@ export default [ }, ], }, + { + text: 'Platform Specific Handling', + link: '/guide/platform-specific-handling', + }, + { + text: 'Platform Version Handling', + link: '/guide/platform-version-handling', + }, + { + text: 'Unexpected Error Handling', + link: '/guide/unexpected-error-handling', + }, ], }, ] as NSSidebarGroup[]