markdown 使用从Objective-C到Cocoascript的回调

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 使用从Objective-C到Cocoascript的回调相关的知识,希望对你有一定的参考价值。

## Re-build cocoa framework
- Clone source code from: [https://github.com/ccgus/CocoaScript](https://github.com/ccgus/CocoaScript)
- Make `MOJavascriptObject.h` as public headers in Build Phases
- Build project and we will get `CocoaScript.framework`
- Copy `CocoaScript.framework` to your Xcode project

## Define the objective-c methods with callback

API.h
```objective-c
#import <Foundation/Foundation.h>
#import <CocoaScript/MOJavaScriptObject.h>
#import <CocoaScript/COScript.h>

@interface Api : NSObject
- (void) login:(NSDictionary*)params onComplete:(MOJavaScriptObject*)onComplete;
@end
```

API.m
```objective-c
- (void) login:(NSDictionary*) params onComplete:(MOJavaScriptObject*)onComplete {
  // handle your logic
  
  // call the callback
   COScript *script = [[COScript alloc] init];
  [script callJSFunction:[onComplete JSObject] withArgumentsInArray:@[@true, @{@"name": @"Sam"}]];
}
```

## Using in .cocoascript

```js
var api = API.alloc().init();
api.login({
  username: 'sam',
  password: '123456'
}, function (success, user) {
  log(success);
  log(user);
});
```

以上是关于markdown 使用从Objective-C到Cocoascript的回调的主要内容,如果未能解决你的问题,请参考以下文章

《程序员》杂志精选:从Objective-C到Swift

从Objective-C到Swift的UITextView类,intrinsicContentSize问题[关闭]

markdown Objective-C备忘录

markdown 从.c到.out

Objective-C - Firebase 从数据库中检索数据并填充到表中

从 Objective-C 到 Swift 的 Swift 传递块