Last active
January 31, 2021 00:53
-
-
Save dlucidone/95264cf6cd216225224b2d72e7e27399 to your computer and use it in GitHub Desktop.
Nativescript Base64 Manipulation Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Convert A String to Base64 String or Vice-Versa | |
*/ | |
declare const android: any; | |
declare const java: any; | |
declare const NSData: any; | |
declare const NSUTF8StringEncoding: any; | |
declare const NSString: any; | |
/* | |
* Convert A String to Base64 String | |
*/ | |
export function transformStringToBase64(platformType, textString) { | |
if (platformType == "Android") { | |
const text = new java.lang.String(textString); | |
const data = text.getBytes("UTF-8"); | |
const base64String = android.util.Base64.encodeToString(data,android.util.Base64.DEFAULT); | |
return base64String; | |
} | |
else if (platformType == "iOS") { | |
const text = NSString.stringWithString(textString); | |
const data = text.dataUsingEncoding(NSUTF8StringEncoding); | |
const base64String = data.base64EncodedStringWithOptions(0); | |
return base64String; | |
} | |
} | |
/* | |
* Convert A Base64 String to String | |
*/ | |
export function transformBase64ToString(platformType, base64String) { | |
if (platformType == "Android") { | |
var data = android.util.Base64.decode(base64String,android.util.Base64.DEFAULT); | |
var decodedString = new java.lang.String(data,java.nio.charset.StandardCharsets.UTF_8); | |
return decodedString; | |
} | |
else if (platformType == "iOS") { | |
const decodedData = NSData.alloc().initWithBase64EncodedStringOptions(base64String,0); | |
const decodedString = NSString.alloc().initWithDataEncoding(decodedData,NSUTF8StringEncoding); | |
return decodedString; | |
} | |
} | |
P.S. - Thanks @triniviz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Decoding does not work for Android at least. I tried
rwtrew53452
which encodes tocnd0cmV3NTM0NTI=
and can be decoded in browser usingatob()
buttransformBase64ToString()
returns an empty object