PGPlugin.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. #define PDRPluginHandleOpenURLNotification @"PDRPluginHandleOpenURLNotification"
  4. typedef enum {
  5. PDRCommandStatusNoResult = 0,
  6. PDRCommandStatusOK,
  7. PDRCommandStatusError
  8. } PDRCommandStatus;
  9. @class PDRCoreApp;
  10. @class PDRCoreAppFrame;
  11. /** Native执行结果
  12. */
  13. @interface PDRPluginResult : NSObject {
  14. }
  15. @property (nonatomic, strong, readonly) NSNumber* status;
  16. @property (nonatomic, strong, readonly) id message;
  17. @property (nonatomic, assign) BOOL keepCallback;
  18. -(PDRPluginResult*) init;
  19. +(PDRPluginResult*) resultWithStatus: (PDRCommandStatus) statusOrdinal;
  20. /**
  21. @brief 返回JS字符串
  22. @param statusOrdinal 结果码
  23. @param theMessage 字符串结果
  24. @return PDRPluginResult*
  25. */
  26. +(PDRPluginResult*) resultWithStatus: (PDRCommandStatus) statusOrdinal messageAsString: (NSString*) theMessage;
  27. /**
  28. @brief 返回JS数组
  29. @param statusOrdinal 结果码
  30. @param theMessage 字符串结果
  31. @return PDRPluginResult*
  32. */
  33. +(PDRPluginResult*) resultWithStatus: (PDRCommandStatus) statusOrdinal messageAsArray: (NSArray*) theMessage;
  34. +(PDRPluginResult*) resultWithStatus: (PDRCommandStatus) statusOrdinal messageAsInt: (int) theMessage;
  35. +(PDRPluginResult*) resultWithStatus: (PDRCommandStatus) statusOrdinal messageAsDouble: (double) theMessage;
  36. +(PDRPluginResult*) resultWithStatus: (PDRCommandStatus) statusOrdinal messageAsDictionary: (NSDictionary*) theMessage;
  37. +(PDRPluginResult*) resultWithStatus: (PDRCommandStatus) statusOrdinal messageToErrorObject: (int) errorCode;
  38. /**
  39. @brief 返回错误对象
  40. @param statusOrdinal 结果码
  41. @param errorCode 错误码
  42. @param message 错误描述
  43. @return PDRPluginResult*
  44. */
  45. +(PDRPluginResult*) resultWithStatus: (PDRCommandStatus) statusOrdinal
  46. messageToErrorObject: (int) errorCode
  47. withMessage:(NSString*)message;
  48. +(PDRPluginResult*) resultWithInnerError:(int) errorCode
  49. withMessage:(NSString*)message;
  50. /**
  51. @brief 返回JSON格式的结果
  52. @return NSString*
  53. */
  54. -(NSString*) toJSONString;
  55. @end
  56. enum {
  57. PGPluginErrorInner = -100,
  58. PGPluginErrorUnknown = -99,
  59. PGPluginErrorAuthDenied = -10,
  60. PGPluginErrorNoInstall = -8,
  61. PGPluginErrorConfig = -7,
  62. PGPluginErrorNet = -6,
  63. PGPluginErrorIO = -5,
  64. PGPluginErrorFileNotFound = -4,
  65. PGPluginErrorNotSupport = -3,
  66. PGPluginErrorUserCancel = -2,
  67. PGPluginErrorInvalidArgument = -1,
  68. PGPluginOK = 0,
  69. PGPluginErrorFileExist,
  70. PGPluginErrorFileCreateFail,
  71. PGPluginErrorZipFail,
  72. PGPluginErrorUnZipFail,
  73. PGPluginErrorNotAllowWrite,
  74. PGPluginErrorNotAllowRead,
  75. PGPluginErrorBusy,
  76. PGPluginErrorNotPermission,
  77. PGPluginErrorNext
  78. };
  79. typedef NS_ENUM(NSInteger, PGPluginAuthorizeStatus) {
  80. PGPluginAuthorizeStatusNotDetermined,
  81. PGPluginAuthorizeStatusDenied,
  82. PGPluginAuthorizeStatusRestriction,
  83. PGPluginAuthorizeStatusAuthorized
  84. };
  85. @protocol PGPluginAuthorize <NSObject>
  86. - (PGPluginAuthorizeStatus)authorizeStatus;
  87. @end
  88. @protocol PGPlugin <NSObject>
  89. @optional
  90. //事件通知类
  91. ///App(5+ App)webview关闭/应用关闭/应用启动
  92. - (void) onAppFrameWillClose:(PDRCoreAppFrame*)theAppframe;
  93. - (void) onAppFrameDidShow:(PDRCoreAppFrame*)theAppframe;
  94. - (void) onAppFrameDidHidden:(PDRCoreAppFrame*)theAppframe;
  95. - (void) onAppClose;
  96. - (void) onAppStarted:(NSDictionary*)options;
  97. - (void) onAppUpgradesNoClose;
  98. - (void) onNeedLayout;
  99. ///Application(iOS App)终止/进入后台/进入前台
  100. - (void) onAppTerminate;
  101. - (void) onAppEnterBackground;
  102. - (void) onAppEnterForeground;
  103. - (void) onMemoryWarning;
  104. @end
  105. /** PDR插件基类
  106. 扩展插件都应该从该类继承
  107. */
  108. @interface PGPlugin : NSObject<PGPluginAuthorize,PGPlugin>
  109. /// 插件运行的窗口对象 <br/>参考: `PDRCoreAppFrame`
  110. @property (nonatomic, assign) PDRCoreAppFrame* JSFrameContext;
  111. /// 插件运行的应用对象 <br/>参考: `PDRCoreApp`
  112. @property (nonatomic, assign) PDRCoreApp* appContext;
  113. /// 插件错误码参考地址
  114. @property (nonatomic, copy) NSString *errorURL;
  115. @property (nonatomic, copy) NSString *sdkErrorURL;
  116. /// 插件名字
  117. @property (nonatomic, copy) NSString *name;
  118. /// 插件描述
  119. @property (nonatomic, copy) NSString *content;
  120. - (PGPlugin*) initWithWebView:(PDRCoreAppFrame*)theWebView withAppContxt:(PDRCoreApp*)app;
  121. - (UIViewController*) rootViewController;
  122. - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion;
  123. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion;
  124. - (void) handleOpenURL:(NSNotification*)notification;
  125. - (NSString*)getUniversalLink;
  126. ///创建插件时调用
  127. - (void) onCreate;
  128. ///销毁插件前调用
  129. - (void) onDestroy;
  130. - (id) onMessage:(id)payload;
  131. - (id) postMessage:(id)payload toPlugin:(NSString*)name inWebview:(NSString*)webview;
  132. - (NSString*)plusObject;
  133. - (NSString*)errorMsgWithCode:(int)errorCode;
  134. //将5+应用转化为系统路径
  135. //支持5+路径_doc/ _www/ _document/ _download/
  136. //file://
  137. //html相对路径
  138. - (NSString*) h5Path2SysPath:(NSString*)path;
  139. - (UIScrollView*)webviewScrollView;
  140. - (NSString*)JSFrameContextID;
  141. //同步执行时放回int值
  142. - (NSData*) resultWithInt:(int)value;
  143. - (NSData*) resultWithString:(NSString*)value;
  144. - (NSData*) resultWithBool:(BOOL)value;
  145. - (NSData*) resultWithDouble:(double)value;
  146. - (NSData*) resultWithJSON:(NSDictionary*)dict;
  147. - (NSData*) resultWithArray:(NSArray*)array;
  148. - (NSData*) resultWithNull;
  149. - (NSData*) resultWithNaN;
  150. - (NSData*) resultWithUndefined;
  151. /**
  152. @brief 同步调用JavaScript回调函数 参考:`toCallback:withReslut:`
  153. */
  154. -(void) toSyncCallback: (NSString*) callbackId withReslut:(NSString*)message;
  155. /**
  156. @brief 异步调用JavaScript回调函数
  157. @param callbackId 回调ID
  158. @param message JSON格式结果 参考:`toJSONString`
  159. */
  160. -(void) toCallback: (NSString*) callbackId withReslut:(NSString*)message;
  161. -(void) toCallback: (NSString*) callbackId withReslut:(NSString*)message inWebview:(NSString*)webviewId;
  162. ///调用JS层错误回调
  163. -(void) toErrorCallback: (NSString*) callbackId withCode:(int)errorCode;
  164. -(void) toErrorCallback: (NSString*) callbackId withCode:(int)errorCode withMessage:(NSString*)message;
  165. -(void) toErrorCallback: (NSString*) callbackId withCode:(int)errorCode withMessage:(NSString*)message keepCallback:(BOOL)keepCallback;
  166. -(void) toErrorCallback: (NSString*) callbackId withCode:(int)errorCode withMessage:(NSString*)message withResult:(NSString*)resultStr keepCallback:(BOOL)keepCallback;
  167. -(void) toErrorCallback: (NSString*) callbackId withInnerCode:(int)errorCode withMessage:(NSString*)message;
  168. -(void) toErrorCallback: (NSString*) callbackId withInnerCode:(int)errorCode withMessage:(NSString*)message keepCallback:(BOOL)keepCallback;
  169. ///将OC NSError转化为H5+Error
  170. -(void) toErrorCallback: (NSString*) callbackId
  171. withNSError:(NSError*)error;
  172. ///封装插件使用的SDK产生的错误信息
  173. ///code:-100
  174. ///message:[self.name self.contexnt:errorCode]message,self.sdkErrorURL
  175. -(void) toErrorCallback: (NSString*) callbackId
  176. withSDKError:(int)errorCode
  177. withMessage:(NSString*)message;
  178. -(void) toErrorCallback: (NSString*) callbackId
  179. withSDKNSError:(NSError*)error;
  180. -(void) toErrorCallback: (NSString*) callbackId
  181. withMoudleName:(NSString*)moudleName
  182. withCode:(int)errorCode
  183. withMessage:(NSString*)message withURL:(NSString*)url;
  184. -(void) toSucessCallback: (NSString*) callbackId withInt:(int)intValue;
  185. -(void) toSucessCallback: (NSString*) callbackId withInt:(int)errorCode keepCallback:(BOOL)keepCallback;
  186. -(void) toSucessCallback: (NSString*) callbackId withDouble:(double)doubleValue;
  187. -(void) toSucessCallback: (NSString*) callbackId withDouble:(double)doubleValue keepCallback:(BOOL)keepCallback;
  188. -(void) toSucessCallback: (NSString*) callbackId withString:(NSString*)stringValue;
  189. -(void) toSucessCallback: (NSString*) callbackId withString:(NSString*)stringValue keepCallback:(BOOL)keepCallback;
  190. -(void) toSucessCallback: (NSString*) callbackId withJSON:(NSDictionary*)jsonValue;
  191. -(void) toSucessCallback: (NSString*) callbackId withJSON:(NSDictionary*)jsonValue keepCallback:(BOOL)keepCallback;
  192. -(void) toSucessCallback: (NSString*) callbackId withArray:(NSArray*)arrayValue;
  193. -(void) toSucessCallback: (NSString*) callbackId withArray:(NSArray*)arrayValue keepCallback:(BOOL)keepCallback;
  194. -(void) toSucessCallback: (NSString*) callbackId
  195. inWebview:(NSString*)webviewId
  196. withJSON:(NSDictionary*)jsonValue
  197. keepCallback:(BOOL)keepCallback;
  198. - (void) writeJavascript:(NSString*)javascript;
  199. - (void) writeJavascript:(NSString*)javascript completionHandler:(void (^)(id, NSError*))completionHandler;
  200. - (void) asyncWriteJavascript:(NSString*)javascript;
  201. - (void) asyncWriteJavascript:(NSString*)javascript inWebview:(NSString*)webviewId;
  202. - (PGPluginAuthorizeStatus)authorizeStatus;
  203. @end
  204. @interface PGPluginParamHelper : NSObject
  205. +(BOOL)getBoolValue:(id)jsValue defalut:(BOOL)defalutValue;
  206. +(BOOL)getBoolValueInDict:(NSDictionary*)jsValue
  207. forKey:(NSString*)key defalut:(float)defalutValue;
  208. +(BOOL)getBoolValueInDict:(NSDictionary*)jsValue
  209. forKey:(NSString*)key secondKey:(NSString*)secondKey defalut:(float)defalutValue;
  210. +(BOOL)isValue:(id)jsValue sameToValue:(NSString*)equalValue defalut:(BOOL)defalutValue;
  211. +(BOOL)isValueInDict:(NSDictionary*)dict
  212. forKey:(NSString*)key
  213. sameToValue:(NSString*)equalValue
  214. defalut:(BOOL)defalutValue;
  215. +(int)getIntValue:(id)jsValue defalut:(int)defalutValue;
  216. +(int)getIntValueInDict:(NSDictionary*)jsValue
  217. forKey:(NSString*)key
  218. defalut:(int)defalutValue;
  219. +(float)getFloatValue:(id)jsValue defalut:(float)defalutValue;
  220. +(float)getFloatValueInDict:(NSDictionary*)jsValue
  221. forKey:(NSString*)key;
  222. +(float)getFloatValueInDict:(NSDictionary*)jsValue
  223. forKey:(NSString*)key defalut:(float)defalutValue;
  224. +(CGFloat)getPixelValueInDict:(NSDictionary*)jsValue
  225. forKey:(NSString*)key defalut:(CGFloat)defalutValue;
  226. +(BOOL)isAutoValue:(id)jsValue;
  227. ///String
  228. +(BOOL)isEmptyString:(NSString*)jsValue;
  229. +(NSString*)getStringValue:(id)jsValue;
  230. +(NSString*)getLowercaseStringValue:(id)jsValue defalut:(NSString*)defalutValue;
  231. +(NSString*)getStringValue:(id)jsValue defalut:(NSString*)defalutValue;
  232. +(NSString*)getStringValueInDict:(NSDictionary*)jsValue
  233. forKey:(NSString*)key;
  234. +(NSString*)getStringValueInDict:(NSDictionary*)jsValue
  235. forKey:(NSString*)key defalut:(NSString*)defalutValue;
  236. +(NSString*)getStringValueInDict:(NSDictionary*)jsValue
  237. forKey:(NSString*)key
  238. testEmptyString:(BOOL)testEmpty
  239. defalut:(NSString*)defalutValue;
  240. + (int)getEnumValueCaseInsensitive:(NSString*)key inMap:(NSDictionary*)enumvalue defautValue:(int)defaultValue ;
  241. +(NSDictionary*)getJSONValue:(id)jsValue defalut:(NSDictionary*)defalutValue;
  242. +(NSDictionary*)getJSONValueInDict:(NSDictionary*)jsValue
  243. forKey:(NSString*)key;
  244. +(NSArray*)getArray:(id)jsValue defalut:(NSArray*)defalutValue;
  245. +(NSArray*)getArrayValueInDict:(NSDictionary*)jsValue
  246. forKey:(NSString*)key defalut:(NSArray*)defalutValue;
  247. +(NSString*)testString:(NSString*)jsValue
  248. inRange:(NSArray*)ranges
  249. defalut:(NSString*)defalutValue;
  250. +(CGFloat)getMeasure:(id)jsValue
  251. withStaff:(CGFloat)withStaff
  252. defalut:(CGFloat)defalutValue;
  253. +(CGFloat)getMeasure:(id)jsValue
  254. withStaff:(CGFloat)withStaff
  255. defalut:(CGFloat)defalutValue
  256. error:(BOOL*)error;
  257. +(UIColor*)getCssColor:(id)jsValue
  258. defalut:(UIColor*)defalutValue;
  259. +(CGFloat)getCssColorForAlph:(id)jsValue
  260. defalut:(CGFloat)defalutValue;
  261. +(NSTextAlignment)getAlign:(NSString*)jsValue
  262. defalut:(NSTextAlignment)defalutValue;
  263. + (CGRect)getValueFromJSRect:(NSDictionary*)jsRect
  264. withReferenceSize:(CGSize)refSize error:(NSError**)error;
  265. + (id)getValue:(NSString*)key inMap:(NSDictionary*)enumvalue defautValue:(id)defaultValue;
  266. + (int)getEnumValue:(NSString*)key inMap:(NSDictionary*)enumvalue defautValue:(int)defaultValue;
  267. + (NSDictionary*)lowercaseStringKey:(NSDictionary*)dict;
  268. + (void)pareseStatusbar:(id)statusBar completion:(void(^)(BOOL,UIColor*, BOOL))result;
  269. + (id)getObjectAtIndex:(NSUInteger)index inArray:(NSArray*)argArray;
  270. @end