我现在想分享我的解决方案,我已经看到大量与父母门相关的帖子,但都没有得到回答。
不确定 Google Play 是否会接受它,但能够在我的应用中设置家长门,如果您发现更好的东西,请分享您的答案!
所以我正在使用这个 pkg:https://github.com/Notalib/nativescript-webview-ext,它是一个 webview 组件(浏览器),我的代码看起来像这样
player.component.html
<GridLayout class="page page-content" xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:nota="@nota/nativescript-webview-ext">
... other html code ...
<nota:WebViewExt (loaded)="onWebViewLoaded($event)"
src='https://www.youtube.com/embed/{{ src }}'
width="100%" height="100%">
</nota:WebViewExt>
</GridLayout>
player.component.ts
import { Component, OnInit, NgZone } from "@angular/core";
import { RouterExtensions } from "nativescript-angular/router";
import { WebView } from "tns-core-modules/ui/web-view";
import { WebViewExt, ShouldOverrideUrlLoadEventData } from "@nota/nativescript-webview-ext";
/*
Events
*/
onWebViewLoaded(args) {
let webview: WebView = args.object;
// This code will grab any click that try to load/switch to an external app/url
webview.on(WebViewExt.shouldOverrideUrlLoadingEvent, (_args: ShouldOverrideUrlLoadEventData) => {
// Disable the event
_args.cancel = true;
// Switch to parental gateway
this.activateParentalGateway(_args.url);
});
}
activateParentalGateway(blocked_url) {
if (typeof(blocked_url) !== "string") {
return false;
}
// run inside the ngZone to connect the event back to the
// component state ( otherwise there's no "this" variable )
this.zone.run(() => {
this.routerExtensions.navigate(['parental_gateway', blocked_url], {
transition: {
name: "fade"
}
});
});
}
parental_gateway.component.html
这是我们问一些“成人”问题的地方......现在只制作了两个按钮来测试正确/错误的答案。
<GridLayout columns="*" rows="*">
<StackLayout row="0" width="100%" orientation="horizontal">
<Label text="Answer this question please" width="20%" height="50"></Label>
<Button text="Correct" (tap)="answeredCorrect($event)"></Button>
<Button text="Failed" (tap)="answeredWrong($event)"></Button>
</StackLayout>
</GridLayout>
parental_gateway.component.ts
import { RouterExtensions } from "nativescript-angular/router";
import { openUrl } from "tns-core-modules/utils/utils";
answeredCorrect() {
console.log("Answer was correct");
// openUrl will actually open the YouTube video
// in a native app ( basically continue the event )
openUrl(this.blocked_url);
// In the background we want to go back from the
// parental gate page that we've been in
// and switch to the video where we first clicked it.
this.routerExtensions.backToPreviousPage();
}
answeredWrong() {
console.log("Wrong answer go back");
// Just go back to our page where we've clicked the link
this.routerExtensions.backToPreviousPage();
}
希望这对其他人有帮助,如果可以,请投票。