开放定制化JSAPI
开发者可以通过JSAPI,将端上的能力开放给小程序或H5微应用
注册JSAPI
开发者需要在bundle.plist文件中注册jsapi,填入export-jsapis
字段,key为jsapi名称,value为实现jsapi的类,例如
Demo
@interface DKTOpenFileJSAPIHandler: NSObject<DTKExternalJSAPIHandlerProtocol>
@end
@implementation DKTOpenFileJSAPIHandler
+ (NSArray<NSString *>*)apiNameList {
return @"file.open";
}
- (void)handleRequest:(id<DTKExternalAPIRequest>)request
withContext:(id<DTKExternalJSAPIContext>)context
callback:(DTKExternalAPICallback)callback {
// do something with request and context
NSString *fileId = request.data[@"fileId"];
if (fileId == nil) {
NSError *error = [NSError errorWithDomain:@"domain" errorCode:-11001 userInfo:nil];
callback(error);
} else {
NSDictionary *result = self.openFile(fileId);
callback(result);
}
}
@end
成功时,需要传回NSDictionary,失败时,传NSError对象
注意:无论成功还是失败,都需要调用callback,告知小程序(H5微应用)
前端调用
// JSAPI: exclusiveInvoke
// 入参:
// - api:sdk定义的jsapi,比如 "file.open"
// - params:请求的参数
import exclusiveInvoke from 'gdt-jsapi/exclusiveInvoke';
exclusiveInvoke({
api: 'file.open',
params: {
filePath: 'xxxxx',
toekn: 'xxxxx'
}
})