Dmitry Mukhin
Dmitry Mukhin

Reputation: 6937

Access methods and properties of arbitrary Objective-C object from JavaScript

I want to access properties and call methods of an Objective-C object that was returned to JavaScript host as property of exposed object ([windowScriptObject setValue:self forKey:@"a"]):

- (id) valueForUndefinedKey:(NSString*) key {
  if ( [key isEqualToString:@"b"] ) {
    MyObject* obj = [ [ MyObject alloc ] init ];
    return obj;
  }
  return Nil;
}

In Javascript I want to be able to do the following:

// a is already exposed Objective-C object
var b = a.b; // reference to myObject
var c = a.b.c; // myObject.c
var d = a.b.d(); // [ myObject d ]

Upvotes: 1

Views: 898

Answers (1)

Rob Napier
Rob Napier

Reputation: 299265

MyObject needs to implement +isSelectorExcludedFromWebScript: and/or +isKeyExcludedFromWebScript:. By default, Javascript is not allowed to access Objective-C methods; you have to explicitly permit it.

Are you seeing some other symptom beyond that?

For more information, see Using Objective-C From Javascript.

Upvotes: 1

Related Questions