NativeScript: ATOB() and BTOA() methods not working

I’m running 2.5 nativescript with 2.4 angular. I’m building on android, i haven’t tested ios.

In a pure js or angular2 app, atob() and btoa() work as expected. However, within an angular 2 nativescript app, those methods appear to somewhat pause the code but throw no errors at all.

Is this a known issue? Is there a fix?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 36 (12 by maintainers)

Most upvoted comments

Android

const text = new java.lang.String(“Yolo 10000”);
const data = text.getBytes(“UTF-8”);
const base64 = android.util.Base64.encodeToString(data,android.util.Base64.DEFAULT);
android.util.Base64.decode(text, android.util.Base64.DEFAULT);

IOS

const decodedData = NSData.alloc().initWithBase64EncodedStringOptions(base64String,0);
const decodedString = NSString.alloc().initWithDataEncoding(decodedData,NSUTF8StringEncoding);

@dlucidone ☝️

@mast3rd3mon there are native methods that can handle encoding/decoding of base64 for you in few lines of code. Example given by @triniwiz here

Android
const text = new java.lang.String("Yolo 10000");
const data = text.getBytes("UTF-8");
const base64 = android.util.Base64.encodeToString(data, android.util.Base64);

IOS
const text = NSString.stringWithString("Yolo 10000");
const data = text.dataUsingEncoding(NSUTF8StringEncoding);
const base64 = data.base64EncodedStringWithOptions(0);

@triniwiz Thanks a lot

@mast3rd3mon perhaps you can use custom implementation like this one

NativeScript works with the native mobile APIs and not with the DOM and those methods require a Window object which is simply non-existent in both Android and iOS. You can use Angular in NativeScript but not the DOM related properties and methods.