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?
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.
Then why do the methods get accepted and compiled if they cant be used? If they aren't available then they shouldn't appear
How am i able to achieve what I'm after then?
@mast3rd3mon perhaps you can use custom implementation like this one
that requires non npm plugins, ideally i want to do it in nativescript
@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);
i just tried the first way you recommended, the same thing happens as using the atob/btoa methods
as for the native way, 1, thats encoding but im after decoding 2, [ts] Cannot find name 'java'
you can install the platform declarations then add the android typings to your ref or add the following declare const java:any
forgot about that, ill give it a try
nope, still freezes when decoding
Try decoding using a worker to take the load of the main thread
i can leave it for 10+ minutes and still nothing
@mast3rd3mon here you can find a basic example for encode and decode methods using the native Android API. Still, as @triniwiz mentioned if you want to operate with large files the best solution is to do that with workers in the background so that your UI thread won't freeze.
the ui doesnt freeze, the code behind does, as for workers, im still yet to be told how
@mast3rd3mon here you can find information on how to implement workers in NativeScript
why is this closed? its not resolved
@mast3rd3mon I think we answered the main question (why ATOB and BTOA methods are not working). We have also provided suggestions on how to encode/decode a string using the Native APIs and in addition to work with rather large files, we suggested to use workers so that the heavy-duty operation won't be on your UI thread but in will be made in the background.
Even with the example I have provided it would be expected your whole app to freeze if you are working with rather large files on the UI thread (this is why we suggested using workers) so I have closed this one as it is not a bug but more of a conceptual question.
If you think that this is related to a bug, please provide a sample project which can reproduce the issue.
my UI doesn't freeze, its the code behind because those methods don't work
I've just tried your way again and this is what happens
i would like to reopen this issue until a correct solution is presented as the one given doesnt work
Hey, @mast3rd3mon there is no issue that we can specify as a bug or non-expected NativeScript behavior based on our discussion. As discussed before both methods (atob() and btoa()) are using browser specifics which are not present in iOS or in Android. So what you need is to either create a polyfill for these methods or use the Native APIs.. Both solutions are tested and are working as expected.
The last screenshot suggests that the error you are receiving is not in the example app (as there were no document paths used at all) but in your own application logic and is probably related to invalid document path which has nothing to do with this discussion so far.
the native methods return horrible looking string type values, as for the screenshot, i tried to open the file by going in to file manager
@NickIliev the example doesnt have methods for ios ?
@dlucidone theres a way to write it in native android and native ios, i never got round to adding the ios way but i couldnt find any other solution to this
@mast3rd3mon Android one works perfectly! but i need for iOS. So if there any one can help me in that.
@dlucidone i ended up googling around until i found the native ios example, cant remember the links anymore, sorry
@NickIliev @triniwiz can you throw some light here?
@dlucidone in the very same thread there is an iOS example (here) - isn't this native solution working out for you?
@NickIliev Its working and its for encoding to base64. I need base64 to string decode function -
Someone shared on slack channel but i dont know how to marshal this -
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(@"Decode String Value: %@", decodedString);
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 ☝️
@triniwiz Thanks a lot
I created a Gist for this conversion methods -https://gist.github.com/dlucidone/95264cf6cd216225224b2d72e7e27399
@NickIliev,
As mentioned in this comment , the link you shared about on how to implement workers in NativeScript is broken.
Its showing site cannot be reached,
http://uatdocs.nativescript.org/core-concepts/multithreading-model#multithreading-model
Can you please share the latest link?
Perfect, we can generate PDFs with {N}! 👍 https://play.nativescript.org/?template=play-ng&id=OGrw9s&v=13
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Android
IOS
@dlucidone ☝️