简体   繁体   中英

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 ]

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM