dimanche 3 juillet 2016

Generate Javascript file dynamically on iOS


I am working with a WKWebView and I need to get some Javascript from a server and execute this javascript on my WKWebView.

Basically, to have this javascript, I make a call to an API that sends a JSON like :

{
    "script" : "function()..." // the content of the script is a string here
}

So my question : how can I inject this script ? I first need to create a JS file with this content, right ? Until now, I was injecting JS from files that were first added to my project.

I know I could create a file like that :

NSError *error;
NSString *stringToWrite = @"function()... my javascript";
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"myScript.js"];
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

But then, the user would see this file on his / her iPhone, right ?


Aucun commentaire:

Enregistrer un commentaire