Techno-magis

Base 64 sur NativeScript

Tuesday 4th February 2020

Il est possible d'utiliser Base64 avec Nativescript sans passer par un package, en utilisant directement celui du système :

CODE:

    // for Android
 
    base64Encode(string: string): string {
        const text = new java.lang.String(string);
        const data = text.getBytes('UTF-8');
        return String.fromCharCode(...Array.from(android.util.Base64.encode(data, android.util.Base64.DEFAULT));
    }
 
    base64Decode(string: string): string {
        const text = new java.lang.String(string);
        const data = text.getBytes('UTF-8');
        return String.fromCharCode(...Array.from(android.util.Base64.decode(data, android.util.Base64.DEFAULT)));
    }


CODE:

    //iOS
 
    base64Encode(string: string): string {
        const text = NSString.stringWithString(string);
        const data = text.dataUsingEncoding(NSUTF8StringEncoding);
        const base64 = data.base64EncodedStringWithOptions(0);
        return base64;
    }
 
    base64Decode(string: string): string {
        const decodedData = NSData.alloc().initWithBase64EncodedStringOptions(string, 0);
        const decodedString = NSString.alloc().initWithDataEncoding(decodedData, NSUTF8StringEncoding);
        return `${decodedString}`;
    }

Categories:
By Zéfling, the 04/02/2020 at 00:32:57
The ticket was read 94 times, with 0 comment posted.
👍 0 👎 0

No comment

Write your below.

Write a commentary