WXSDKInstance.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #import <UIKit/UIKit.h>
  20. #import "WXComponent.h"
  21. #import "WXJSExceptionInfo.h"
  22. #import "WXResourceResponse.h"
  23. #import "WXResourceRequest.h"
  24. #import "WXBridgeProtocol.h"
  25. #import "WXApmForInstance.h"
  26. #import "WXComponentManager.h"
  27. extern NSString *const bundleUrlOptionKey;
  28. @interface WXSDKInstance : NSObject
  29. /**
  30. * Init instance and render it using iOS native views.
  31. * It is the same as initWithRenderType:@"platform"
  32. **/
  33. - (instancetype)init;
  34. /**
  35. * Init instance with custom render type.
  36. **/
  37. - (instancetype)initWithRenderType:(NSString*)renderType;
  38. /**
  39. * The render type. Default is "platform"
  40. **/
  41. @property (nonatomic, strong, readonly) NSString* renderType;
  42. /**
  43. * Returns YES when self.renderType != "platform"
  44. **/
  45. @property (nonatomic, assign, readonly) BOOL isCustomRenderType;
  46. /*
  47. * For weex containers in view controller(main containers), we may need to release render buffer
  48. * of custom render type page to save memory.
  49. */
  50. @property (nonatomic, assign) BOOL isMainContainerStack;
  51. /**
  52. * The viewControler which the weex bundle is rendered in.
  53. **/
  54. @property (nonatomic, weak) UIViewController *viewController;
  55. /**
  56. * The Native root container used to bear the view rendered by weex file.
  57. * The root view is controlled by WXSDKInstance, so you can only get it, but not change it.
  58. **/
  59. @property (nonatomic, strong) UIView *rootView;
  60. /**
  61. * Component can freeze the rootview frame through the variable isRootViewFrozen
  62. * If Component want to freeze the rootview frame, set isRootViewFrozen YES, weex will not change the rootview frame when layout,or set NO.
  63. **/
  64. @property (nonatomic, assign) BOOL isRootViewFrozen;
  65. /**
  66. * Which indicates current instance needs to be validated or not to load,default value is false.
  67. **/
  68. @property (nonatomic, assign) BOOL needValidate;
  69. /**
  70. * Which indicates current instance use backup JS thread run,default value is false.
  71. **/
  72. @property (nonatomic, assign) BOOL useBackupJsThread;
  73. /**
  74. * The scriptURL of weex bundle.
  75. **/
  76. @property (nonatomic, strong) NSURL *scriptURL;
  77. /**
  78. * The parent instance.
  79. **/
  80. @property (nonatomic, weak) WXSDKInstance *parentInstance;
  81. /**
  82. * The node reference of parent instance.
  83. **/
  84. @property (nonatomic, weak) NSString *parentNodeRef;
  85. /**
  86. * The unique id to identify current weex instance.
  87. **/
  88. @property (nonatomic, strong) NSString *instanceId;
  89. /**
  90. * Which indicates current instance needs to be prerender or not,default value is false.
  91. **/
  92. @property (nonatomic, assign) BOOL needPrerender;
  93. /**
  94. * Custom info.
  95. **/
  96. @property (nonatomic, strong) NSDictionary* containerInfo;
  97. /**
  98. * Whether this instance is rendered or not. Please MUST not render an instance twice even if you have called destroyInstance.
  99. **/
  100. @property (nonatomic, assign, readonly) BOOL isRendered;
  101. /**
  102. * Get component manager of this instance. You can manipulate components then.
  103. **/
  104. @property (nonatomic, readonly, strong) WXComponentManager *componentManager;
  105. /**
  106. * The state of current instance.
  107. **/
  108. typedef NS_ENUM(NSInteger, WXState) {//state.code
  109. WeexInstanceAppear = 100,
  110. WeexInstanceDisappear,
  111. WeexInstanceForeground,
  112. WeexInstanceBackground,
  113. WeexInstanceMemoryWarning,
  114. WeexInstanceBindChanged,
  115. WeexInstanceDestroy
  116. };
  117. typedef NS_ENUM(NSInteger, WXErrorType) {//error.domain
  118. TemplateErrorType = 1,
  119. };
  120. typedef NS_ENUM(NSInteger, WXErrorCode) {//error.code
  121. PlatformErrorCode = 1000,
  122. OSVersionErrorCode,
  123. AppVersionErrorCode,
  124. WeexSDKVersionErrorCode,
  125. DeviceModelErrorCode,
  126. FrameworkVersionErrorCode,
  127. };
  128. @property (nonatomic, assign) WXState state;
  129. /**
  130. * The callback triggered when the instance finishes creating the body.
  131. *
  132. * @return A block that takes a UIView argument, which is the root view
  133. **/
  134. @property (nonatomic, copy) void (^onCreate)(UIView *);
  135. /**
  136. * The callback triggered when the root container's frame has changed.
  137. *
  138. * @return A block that takes a UIView argument, which is the root view
  139. **/
  140. @property (nonatomic, copy) void (^onLayoutChange)(UIView *);
  141. /**
  142. * The callback triggered when the instance finishes rendering.
  143. *
  144. * @return A block that takes a UIView argument, which is the root view
  145. **/
  146. @property (nonatomic, copy) void (^renderFinish)(UIView *);
  147. /**
  148. * The callback triggered when the instance finishes refreshing weex view.
  149. *
  150. * @return A block that takes a UIView argument, which is the root view
  151. **/
  152. @property (nonatomic, copy) void (^refreshFinish)(UIView *);
  153. /**
  154. * bundleType is the DSL type
  155. */
  156. @property (nonatomic, strong) NSString * bundleType;
  157. /**
  158. * Which decide whether to use data render,default value is false
  159. */
  160. @property (nonatomic, assign, readonly) BOOL dataRender;
  161. /**
  162. * Which decide whether to use binary code render, default value is false
  163. */
  164. @property (nonatomic, assign, readonly) BOOL wlasmRender;
  165. /**
  166. * The callback triggered when the instance fails to render.
  167. *
  168. * @return A block that takes a NSError argument, which is the error occured
  169. **/
  170. @property (nonatomic, copy) void (^onFailed)(NSError *error);
  171. /**
  172. *
  173. * @return instance runtime JavaScript.
  174. * you must call it in brige thread. Learn more at WXPerformBlockOnBridgeThread
  175. **/
  176. - (id<WXBridgeProtocol>)instanceJavaScriptContext;
  177. /**
  178. * The callback triggered when js occurs runtime error while executing.
  179. *
  180. * @return A block that takes a WXJSExceptionInfo argument, which is the exception info
  181. **/
  182. @property (nonatomic, copy) void (^onJSRuntimeException)(WXJSExceptionInfo * jsException);
  183. /**
  184. * The callback triggered when the instacne executes scrolling .
  185. *
  186. * @return A block that takes a CGPoint argument, which is content offset of the scroller
  187. **/
  188. @property (nonatomic, copy) void (^onScroll)(CGPoint contentOffset);
  189. /**
  190. * the callback to be run repeatedly while the instance is rendering.
  191. *
  192. * @return A block that takes a CGRect argument, which is the rect rendered
  193. **/
  194. @property (nonatomic, copy) void (^onRenderProgress)(CGRect renderRect);
  195. /**
  196. * The callback triggered when the bundleJS request finished in the renderWithURL.
  197. * @return A block that takes response which the server response,request which send to server,data which the server returned and an error
  198. */
  199. @property (nonatomic, copy) void(^onJSDownloadedFinish)(WXResourceResponse *response,WXResourceRequest *request,NSData *data, NSError* error);
  200. /**
  201. * The callback triggered when the bundleJS request finished in the renderWithURL. If the callback returns YES, the render process will terminate.
  202. * @return A block that takes response which the server response,request which send to server,data which the server returned and an error
  203. */
  204. @property (nonatomic, copy) BOOL (^onRenderTerminateWhenJSDownloadedFinish)(WXResourceResponse *response,WXResourceRequest *request,NSData *data, NSError* error);
  205. @property(nonatomic,strong) NSDictionary* continerInfo;
  206. /**
  207. * the frame of current instance.
  208. **/
  209. @property (nonatomic, assign) CGRect frame;
  210. /**
  211. * the info stored by user.
  212. */
  213. @property (atomic, strong) NSMutableDictionary *userInfo;
  214. /**
  215. * scale factor from css unit to device pixel.
  216. */
  217. @property (nonatomic, assign, readonly) CGFloat pixelScaleFactor;
  218. /**
  219. * track component render
  220. */
  221. @property (nonatomic, assign)BOOL trackComponent;
  222. /**
  223. * Renders weex view with bundle url.
  224. *
  225. * @param url The url of bundle rendered to a weex view.
  226. **/
  227. - (void)renderWithURL:(NSURL *)url;
  228. /**
  229. * Renders weex view with bundle url and some others.
  230. *
  231. * @param url The url of bundle rendered to a weex view.
  232. *
  233. * @param options The params passed by user
  234. *
  235. * @param data The data the bundle needs when rendered. Defalut is nil.
  236. **/
  237. - (void)renderWithURL:(NSURL *)url options:(NSDictionary *)options data:(id)data;
  238. ///**
  239. // * Renders weex view with resource request.
  240. // *
  241. // * @param request The resource request specifying the URL to render with.
  242. // *
  243. // * @param options The params passed by user.
  244. // *
  245. // * @param data The data the bundle needs when rendered. Defalut is nil.
  246. // **/
  247. //- (void)renderWithRequest:(WXResourceRequest *)request options:(NSDictionary *)options data:(id)data;
  248. /**
  249. * Renders weex view with source string of bundle and some others.
  250. *
  251. * @param options The params passed by user.
  252. *
  253. * @param data The data the bundle needs when rendered. Defalut is nil.
  254. **/
  255. - (void)renderView:(id)source options:(NSDictionary *)options data:(id)data;
  256. /**
  257. * Reload the js bundle from the current URL and rerender.
  258. *
  259. * @param forcedReload when this parameter is true, the js bundle will always be reloaded from the server. If it is false, the instance may reload the js bundle from its cache. Default is false.
  260. *
  261. **/
  262. - (void)reload:(BOOL)forcedReload;
  263. /**
  264. * Refreshes current instance components' layout after setting custom view port/device width.
  265. **/
  266. - (void)reloadLayout;
  267. /**
  268. * Refreshes current instance with data.
  269. *
  270. * @param data The data the bundle needs when rendered.
  271. **/
  272. - (void)refreshInstance:(id)data;
  273. /**
  274. * Destroys current instance. An instance destroyed should not be used for rendering again, please create another instance.
  275. **/
  276. - (void)destroyInstance;
  277. /**
  278. * Trigger full GC, for dev and debug only.
  279. **/
  280. - (void)forceGarbageCollection;
  281. /**
  282. * get module instance by class
  283. */
  284. - (id)moduleForClass:(Class)moduleClass;
  285. /**
  286. * get Component instance by ref, must be called on component thread by calling WXPerformBlockOnComponentThread
  287. */
  288. - (WXComponent *)componentForRef:(NSString *)ref;
  289. /**
  290. * Number of components created, must be called on component thread by calling WXPerformBlockOnComponentThread
  291. */
  292. - (NSUInteger)numberOfComponents;
  293. /**
  294. * Enumerate components using breadth-first search algorithm,
  295. must be called on component thread by calling WXPerformBlockOnComponentThread
  296. */
  297. - (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *component, BOOL *stop))block;
  298. /**
  299. * check whether the module eventName is registered
  300. */
  301. - (BOOL)checkModuleEventRegistered:(NSString*)event moduleClassName:(NSString*)moduleClassName;
  302. /**
  303. * fire module event;
  304. * @param module which module you fire event to
  305. * @param eventName the event name
  306. * @param params event params
  307. */
  308. - (void)fireModuleEvent:(Class)module eventName:(NSString *)eventName params:(NSDictionary*)params;
  309. /**
  310. * fire global event
  311. */
  312. - (void)fireGlobalEvent:(NSString *)eventName params:(NSDictionary *)params;
  313. /**
  314. * complete url based with bundle url
  315. */
  316. - (NSURL *)completeURL:(NSString *)url;
  317. /**
  318. * jsbundle str ,may be nil (weak)
  319. */
  320. - (NSString*) bundleTemplate;
  321. /**
  322. * application performance statistics
  323. */
  324. @property (nonatomic, strong) NSString *bizType;
  325. @property (nonatomic, strong) NSString *pageName;
  326. @property (nonatomic, weak) id pageObject;
  327. //Deprecated, use @WXApmForInstance
  328. @property (nonatomic, strong) NSMutableDictionary *performanceDict;
  329. @property (nonatomic, strong) WXApmForInstance* apmInstance;
  330. @property (nonatomic, assign) BOOL appearState;
  331. /*
  332. * For custom render page to release/restore OpenGL resources, etc.
  333. */
  334. - (void)willAppear;
  335. - (void)didDisappear;
  336. /**
  337. * Raw css styles are dropped after applied to layout nodes in WeexCore.
  338. * If a page needs hot refresh(without recreating instance and reload js) after screen orientation changes or
  339. * after setting custom view-port-width/screen-width/, you need to call setPageNeedsRawCssStyles to store all css styles
  340. * internally for later use. Or you can use MetaModule's setPageArguments method and provide "reserveCssStyles" as "true" before rendering the page.
  341. */
  342. - (void)setPageKeepRawCssStyles;
  343. - (void)isKeepingRawCssStyles:(void(^)(BOOL))callback;
  344. /**
  345. * Set additional argument value for WeexCore
  346. */
  347. - (void)setPageArgument:(NSString*)key value:(NSString*)value;
  348. /**
  349. * Set specific required page width and height to prevent this page using global values.
  350. */
  351. - (void)setPageRequiredWidth:(CGFloat)width height:(CGFloat)height;
  352. /**
  353. * Set specific required view port width prevent this page using global value (750px).
  354. */
  355. - (void)setViewportWidth:(CGFloat)width;
  356. #pragma mark - dc extend
  357. //-----------↓↓↓↓ uni extend ↓↓↓↓---------------------------------------------------------
  358. @property(nonatomic, assign)BOOL dc_transparentEvent;
  359. @property (nonatomic, copy) void (^bundleJsExecFinish)(void);
  360. @property (nonatomic, copy) UIView* (^dc_customRootView)(CGRect);
  361. @property (nonatomic, assign, readonly) CGFloat dc_textFontSize;
  362. @property (nonatomic, strong) NSString *dc_docPath;
  363. @property(nonatomic, strong) NSString *dc_webviewId;
  364. @property(nonatomic, strong) NSString *dc_oldInstanceId;
  365. @property(nonatomic, weak) id dc_appframeContext; //关联的5+ coreappframe
  366. - (void)dc_setDefalutFontSize:(CGFloat)defaultFontSize;
  367. //-----------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑---------------------------------------------------------
  368. /**
  369. * Deprecated
  370. */
  371. @property (nonatomic, strong) NSDictionary *properties DEPRECATED_MSG_ATTRIBUTE();
  372. @property (nonatomic, assign) NSTimeInterval networkTime DEPRECATED_MSG_ATTRIBUTE();
  373. @property (nonatomic, copy) void (^updateFinish)(UIView *);
  374. @end
  375. @interface WXSDKInstance (Deprecated)
  376. - (void)finishPerformance DEPRECATED_MSG_ATTRIBUTE();
  377. - (void)reloadData:(id)data DEPRECATED_MSG_ATTRIBUTE("Use refreshInstance: method instead.");
  378. - (void)creatFinish DEPRECATED_MSG_ATTRIBUTE();
  379. @end