问题
I created a directive to disable the context menu on android and ios app in Nativescript.
import { Directive, OnInit, OnDestroy, ElementRef, Renderer2 } from "@angular/core";
import { isIOS } from "tns-core-modules/platform";
import * as utils from "tns-core-modules/utils/utils";
import { EventData } from "tns-core-modules/data/observable";
import { TextField } from "tns-core-modules/ui/text-field";
declare var UITextField, CGRectMake, android;
if (isIOS) {
UITextField.prototype._originalCanPerformActionWithSender = UITextField.prototype.canPerformActionWithSender;
UITextField.prototype.canPerformActionWithSender = function (action, sender) {
if (this.disableMenu) {
return false;
}
return UITextField.prototype._originalCanPerformActionWithSender.call(this, action, sender)
};
}
@Directive({
selector: "[disableCutCopyPaste]"
})
export class DisableCutCopyPasteDirective implements OnInit, OnDestroy {
listener: () => void;
constructor(private renderer: Renderer2, private el: ElementRef) {
}
ngOnInit() {
this.listener = this.renderer.listen(this.el.nativeElement, TextField.loadedEvent, (event: EventData) => {
const textField = <TextField>event.object;
if (isIOS) {
Object.defineProperty(textField.nativeView, "disableMenu", {
get: function () {
return true;
}
});
} else {
textField.nativeView.setLongClickable(false);
textField.nativeView.setOnTouchListener(new android.view.View.OnTouchListener({
onTouch: function (view, event) {
if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
view.requestFocus();
utils.ad.showSoftInput(view);
}
return true;
}
}));
}
});
}
ngOnDestroy() {
this.removeListener();
}
private removeListener() {
if (this.listener) {
this.listener();
this.listener = null;
}
}
}
This code is working fine on Android devices but the iOS app is crashing and I'm getting the following error
TypeError: Attempting to change the getter of an unconfigurable property
at this line
Object.defineProperty(textField.nativeView, "disableMenu", {
Can anyone tell me what's causing this issue?
My package.json
{
"nativescript": {
"id": "com.abcde.app",
"tns-android": {
"version": "6.1.1"
},
"tns-ios": {
"version": "6.5.3"
}
},
"description": "NativeScript Application",
"license": "SEE LICENSE IN <your-license-filename>",
"repository": "<fill-your-repository-here>",
"dependencies": {
"@angular/animations": "~8.2.9",
"@angular/common": "~8.2.9",
"@angular/compiler": "~8.2.9",
"@angular/core": "~8.2.9",
"@angular/forms": "~8.2.9",
"@angular/http": "8.0.0-beta.10",
"@angular/platform-browser": "~8.2.9",
"@angular/platform-browser-dynamic": "~8.2.9",
"@angular/router": "~8.2.9",
"@nstudio/nativescript-checkbox": "^1.0.0",
"@nstudio/nativescript-loading-indicator": "^1.0.0",
"nativescript-angular": "^8.2.1",
"nativescript-carousel": "^6.1.1",
"nativescript-checkbox": "^3.0.3",
"nativescript-drop-down": "^5.0.4",
"nativescript-exit": "^1.0.1",
"nativescript-floatingactionbutton": "^5.1.0",
"nativescript-iqkeyboardmanager": "^1.5.1",
"nativescript-modal-datetimepicker": "^1.2.3",
"nativescript-plugin-firebase": "^10.0.1",
"nativescript-root-detection": "^1.0.0",
"nativescript-theme-core": "~1.0.4",
"nativescript-ui-listview": "7.1.0",
"reflect-metadata": "~0.1.13",
"rxjs": "^6.5.3",
"rxjs-compat": "^6.5.3",
"simple-crypto-js": "^2.2.0",
"tns-core-modules": "^6.5.21",
"tns-platform-declarations": "^6.5.15",
"zone.js": "0.9.1"
},
"devDependencies": {
"@angular/compiler-cli": "8.2.9",
"@nativescript/schematics": "~0.7.2",
"@ngtools/webpack": "8.3.8",
"nativescript-dev-webpack": "^1.3.0",
"tns-android": "6.1.1",
"typescript": "~3.5.3"
},
"gitHead": "9b65dfgdgdfgdgdgdfgdfd818a8205e",
"readme": "NativeScript Application"
}
Playground Link
来源:https://stackoverflow.com/questions/64892167/nativescript-ios-app-crashes-attempting-to-change-the-getter-of-an-unconfigura