【发布时间】:2019-07-17 23:08:14
【问题描述】:
我正在构建一个需要使用本机 API 的应用程序,但事实证明,使用这些 API 非常困难,因为许多参数在 TypeScript 中没有明确的类比,缺少定义等等。人们如何使用 NativeScript 实际构建?
例如,我正在尝试使用 Apple 的 Secure Enclave 在this 页面之后生成一个公私密钥对。这些 sn-ps 是用 Swift 编写的(我认为),所以打字稿中不存在语法的许多方面。经过数小时的反复试验,我提出了不会导致错误的论点。但是我得到的privateKey 值为空。
可能有几十或一百种不同的参数排列我可以尝试,它们sort 等同于 Swift 代码......这不可能是人们在 NativeScript 上构建的方式吗?我到处找,我在 NativeScript Slack 上问过,我读过帖子......
tns create
(choose Angular and hello-world)
npm install --save-dev tns-platform-declarations
在项目根目录中添加reference.d.ts。将以下内容放入其中:
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
更新tsconfig.json,使paths 看起来像这样:
"~/*": ["src/*", "./node_modules/tns-core-modules/*", "./node_modules/*"]
将app.component.html 修改为只有一个按钮
<Button (tap)="onTap()"></Button>
修改app.component.ts添加按钮代码:
onTap() {
const access = SecAccessControlCreateWithFlags(
kCFAllocatorDefault,
kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
SecAccessControlCreateFlags.kSecAccessControlPrivateKeyUsage,
new interop.Pointer(0)
);
const attributes = {
kSecAttrKeyType: 'type',
kSecAttrKeySizeInBits: '256',
kSecAttrTokenID: kSecAttrTokenIDSecureEnclave,
kSecPrivateKeyAttrs: {
kSecAttrIsPermanent: 'true',
kSecAttrApplicationTag: '<# a tag #>',
kSecAttrAccessControl: 'access',
},
};
const privateKey = SecKeyCreateRandomKey(attributes as any, new interop.Pointer(0));
console.info(privateKey);
}
我确信我对 Swift 代码的解释(如上所示)存在很多问题。一个解决方案会很好。但我真正的问题是人们如何实际使用 Angular(意思是 NativeScript)进行构建?我不能只是猜测这些参数在 typescript 中应该是什么!
预期结果:
privateKey 不为空,访问原生 API 的明智方式?
【问题讨论】:
-
你可以开始查看 Nativescript Documentation 然后搜索具有类似 iOS 实现的插件 Geolocation
标签: android ios swift angular nativescript