I need to use a NSBlock as a property that can be changed in JavaScript.
@protocol ExportingUser <NSObject, JSExport>
@property (nonatomic, copy) void (^changedName) (void);
@property (nonatomic) NSString* name;
@end
I basically want to call changedName from Obj-C, a function whose definition comes from JavaScript. Like this:
user.name = "Peyman"; // this will change the name property in the Obj-C as well.
user.changedName = function() { console.log("yay"); } // but this doesn't
I know that this is possible through JSValue's callWithArguments method but I prefer this method if possible.
Edit: actually, I don't think the callWithArguments method would work. It's possible since objects that are bound to Objective-C classes are not extensible in JavaScript so introducing new fields would simply be ignored.
As listed in JSValue.h :
Apparently, you can get away with :
Then, when you want to call it: