Skip to content

Instantly share code, notes, and snippets.

@acegreen
Last active October 27, 2015 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acegreen/05282d20eb45de8d30d5 to your computer and use it in GitHub Desktop.
Save acegreen/05282d20eb45de8d30d5 to your computer and use it in GitHub Desktop.
let script: JSValue = jsContext.objectForKeyedSubscript("getImageURL")
[jsContext callWithArguments:@["FirstParam",^(NSString* callbackValue) {
NSLog(@"Got a value: %@",callbackValue)
}]
basically I have a javascript function that looks like this:
function getImageURL(object, callback) {
object.executeActionById("takeScreenshot");
object.onScreenshotReady(function(imageName) {
alert(imageName);
callback(imageName);
});
};
And all I really want is imageName on the iOS side. So I'm trying to call the callback part of it on iOS and get imageName.
onScreenshotReady is async so I need to wait for it to finish before I get my imageName. So I created a callback within it
@acegreen
Copy link
Author

I GOT IT!!!!!!!! :)

@natecook1000
Copy link

🎉 👍 🎈 👏

What did the trick?

@acegreen
Copy link
Author

I added the exceptionHandler to see if I get any errors:

And turns out I did. "widget" was not being passed along so the function was executing

object.executeActionById("takeScreenshot");
object.onScreenshotReady(function(imageName) ....

instead of
widget.executeActionById("takeScreenshot");
widget.onScreenshotReady(function(imageName) ....

So since I actually don't need to pass that parameter from iOS. I just hardcoded it directly. so my function became:

        function getImageURL(callback) {
            widget.executeActionById("takeScreenshot");
            widget.onScreenshotReady(function(imageName) {
              callback(imageName);
            });
        };

and on iOS side i did:

        let args = [unsafeBitCast(callback, AnyObject.self)]

        jsContext.objectForKeyedSubscript("getImageURL").callWithArguments(args)

And works exactly as expected.

Many many thanks for feedback. Spent two days on this pesky issue. Now onto the next

@tv-webdev
Copy link

HI @acegreen,

How can I save an image to my local("Downloads") without using the snapshot url from tradingview?

@tv-webdev
Copy link

Hi @acegreen,
In addition to my first question, how can I create a method that will not generate an image on tradingview, but generating a base64 image and saving it to my local directory.

Thanks in advance..=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment