Search code examples
androidiosnativescriptnativescript-vue

include line only if platform is Android


One problem that I have is that when I navigate to a page containing a form on android the keyboard pops up automatically. I found a solution for it but it only works on Android:

import { View } from "tns-core-modules/ui/core/view";

export class AutoFocusView extends View {

    createNativeView() {
        if (typeof android !== "undefined") {
            const linearLayout = new android.widget.LinearLayout(this._context);
            linearLayout.setFocusableInTouchMode(true);
            linearLayout.setFocusable(true);
            return linearLayout;
        } 
        return super.createNativeView();
    }

    onLoaded() {
        super.onLoaded();
        this.requestFocus();
    }

    requestFocus() {
        const nativeViewProtected = this.nativeViewProtected;
        nativeViewProtected.requestFocus();
    }
}

I use this component. but it only works on android so I need to comment it from my code everytime I want to build for IOS. I was wondering if there was an easier way.


Solution

  • You could do like this:

    import {isAndroid, isIOS} from '@nativescript/core';
    

    then in your computed:

    isandroid() {
                    return isAndroid;
                }
    

    then use this flag in any method you need:

    if(isAndroid) {
      this.requestFocus();
    }
    

    You also can use this isAndroid flag in your template if need be with v-if.