RCTUtils.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <tgmath.h>
  8. #import <CoreGraphics/CoreGraphics.h>
  9. #import <Foundation/Foundation.h>
  10. #import <UIKit/UIKit.h>
  11. #import <React/RCTAssert.h>
  12. #import <React/RCTDefines.h>
  13. NS_ASSUME_NONNULL_BEGIN
  14. // JSON serialization/deserialization
  15. RCT_EXTERN NSString *__nullable RCTJSONStringify(id __nullable jsonObject, NSError **error);
  16. RCT_EXTERN id __nullable RCTJSONParse(NSString *__nullable jsonString, NSError **error);
  17. RCT_EXTERN id __nullable RCTJSONParseMutable(NSString *__nullable jsonString, NSError **error);
  18. // Sanitize a JSON object by stripping invalid types and/or NaN values
  19. RCT_EXTERN id RCTJSONClean(id object);
  20. // Get MD5 hash of a string
  21. RCT_EXTERN NSString *RCTMD5Hash(NSString *string);
  22. // Check if we are currently on the main queue (not to be confused with
  23. // the main thread, which is not necessarily the same thing)
  24. // https://twitter.com/olebegemann/status/738656134731599872
  25. RCT_EXTERN BOOL RCTIsMainQueue(void);
  26. // Execute the specified block on the main queue. Unlike dispatch_async()
  27. // this will execute immediately if we're already on the main queue.
  28. RCT_EXTERN void RCTExecuteOnMainQueue(dispatch_block_t block);
  29. // Legacy function to execute the specified block on the main queue synchronously.
  30. // Please do not use this unless you know what you're doing.
  31. RCT_EXTERN void RCTUnsafeExecuteOnMainQueueSync(dispatch_block_t block);
  32. // Get screen metrics in a thread-safe way
  33. RCT_EXTERN CGFloat RCTScreenScale(void);
  34. RCT_EXTERN CGSize RCTScreenSize(void);
  35. // Round float coordinates to nearest whole screen pixel (not point)
  36. RCT_EXTERN CGFloat RCTRoundPixelValue(CGFloat value);
  37. RCT_EXTERN CGFloat RCTCeilPixelValue(CGFloat value);
  38. RCT_EXTERN CGFloat RCTFloorPixelValue(CGFloat value);
  39. // Convert a size in points to pixels, rounded up to the nearest integral size
  40. RCT_EXTERN CGSize RCTSizeInPixels(CGSize pointSize, CGFloat scale);
  41. // Method swizzling
  42. RCT_EXTERN void RCTSwapClassMethods(Class cls, SEL original, SEL replacement);
  43. RCT_EXTERN void RCTSwapInstanceMethods(Class cls, SEL original, SEL replacement);
  44. // Module subclass support
  45. RCT_EXTERN BOOL RCTClassOverridesClassMethod(Class cls, SEL selector);
  46. RCT_EXTERN BOOL RCTClassOverridesInstanceMethod(Class cls, SEL selector);
  47. // Creates a standardized error object to return in callbacks
  48. RCT_EXTERN NSDictionary<NSString *, id>
  49. *RCTMakeError(NSString *message, id __nullable toStringify, NSDictionary<NSString *, id> *__nullable extraData);
  50. RCT_EXTERN NSDictionary<NSString *, id> *
  51. RCTMakeAndLogError(NSString *message, id __nullable toStringify, NSDictionary<NSString *, id> *__nullable extraData);
  52. RCT_EXTERN NSDictionary<NSString *, id> *RCTJSErrorFromNSError(NSError *error);
  53. RCT_EXTERN NSDictionary<NSString *, id>
  54. *RCTJSErrorFromCodeMessageAndNSError(NSString *code, NSString *message, NSError *__nullable error);
  55. // The default error code to use as the `code` property for callback error objects
  56. RCT_EXTERN NSString *const RCTErrorUnspecified;
  57. // Returns YES if React is running in a test environment
  58. RCT_EXTERN BOOL RCTRunningInTestEnvironment(void);
  59. // Returns YES if React is running in an iOS App Extension
  60. RCT_EXTERN BOOL RCTRunningInAppExtension(void);
  61. // Returns the shared UIApplication instance, or nil if running in an App Extension
  62. RCT_EXTERN UIApplication *__nullable RCTSharedApplication(void);
  63. // Returns the current main window, useful if you need to access the root view
  64. // or view controller
  65. RCT_EXTERN UIWindow *__nullable RCTKeyWindow(void);
  66. // Returns the presented view controller, useful if you need
  67. // e.g. to present a modal view controller or alert over it
  68. RCT_EXTERN UIViewController *__nullable RCTPresentedViewController(void);
  69. // Does this device support force touch (aka 3D Touch)?
  70. RCT_EXTERN BOOL RCTForceTouchAvailable(void);
  71. // Create an NSError in the RCTErrorDomain
  72. RCT_EXTERN NSError *RCTErrorWithMessage(NSString *message);
  73. // Convert nil values to NSNull, and vice-versa
  74. #define RCTNullIfNil(value) ((value) ?: (id)kCFNull)
  75. #define RCTNilIfNull(value) \
  76. ({ \
  77. __typeof__(value) t = (value); \
  78. (id) t == (id)kCFNull ? (__typeof(value))nil : t; \
  79. })
  80. // Convert NaN or infinite values to zero, as these aren't JSON-safe
  81. RCT_EXTERN double RCTZeroIfNaN(double value);
  82. // Returns `0` and log special warning if value is NaN or INF.
  83. RCT_EXTERN double RCTSanitizeNaNValue(double value, NSString *property);
  84. // Convert data to a Base64-encoded data URL
  85. RCT_EXTERN NSURL *RCTDataURL(NSString *mimeType, NSData *data);
  86. // Gzip functionality - compression level in range 0 - 1 (-1 for default)
  87. RCT_EXTERN NSData *__nullable RCTGzipData(NSData *__nullable data, float level);
  88. // Returns the relative path within the main bundle for an absolute URL
  89. // (or nil, if the URL does not specify a path within the main bundle)
  90. RCT_EXTERN NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL);
  91. // Returns the Path of Library directory
  92. RCT_EXTERN NSString *__nullable RCTLibraryPath(void);
  93. // Returns the relative path within the library for an absolute URL
  94. // (or nil, if the URL does not specify a path within the Library directory)
  95. RCT_EXTERN NSString *__nullable RCTLibraryPathForURL(NSURL *__nullable URL);
  96. // Determines if a given image URL refers to a image in bundle
  97. RCT_EXTERN BOOL RCTIsBundleAssetURL(NSURL *__nullable imageURL);
  98. // Determines if a given image URL refers to a image in library
  99. RCT_EXTERN BOOL RCTIsLibraryAssetURL(NSURL *__nullable imageURL);
  100. // Determines if a given image URL refers to a local image
  101. RCT_EXTERN BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL);
  102. // Returns an UIImage for a local image asset. Returns nil if the URL
  103. // does not correspond to a local asset.
  104. RCT_EXTERN UIImage *__nullable RCTImageFromLocalAssetURL(NSURL *imageURL);
  105. // Only used in case when RCTImageFromLocalAssetURL fails to get an image
  106. // This method basically checks for the image in the bundle location, instead
  107. // of the CodePush location
  108. RCT_EXTERN UIImage *__nullable RCTImageFromLocalBundleAssetURL(NSURL *imageURL);
  109. // Creates a new, unique temporary file path with the specified extension
  110. RCT_EXTERN NSString *__nullable RCTTempFilePath(NSString *__nullable extension, NSError **error);
  111. // Get RGBA components of CGColor
  112. RCT_EXTERN void RCTGetRGBAColorComponents(CGColorRef color, CGFloat rgba[_Nonnull 4]);
  113. // Converts a CGColor to a hex string
  114. RCT_EXTERN NSString *RCTColorToHexString(CGColorRef color);
  115. // Get standard localized string (if it exists)
  116. RCT_EXTERN NSString *RCTUIKitLocalizedString(NSString *string);
  117. // Get a human readable type string from an NSObject. For example NSString becomes string
  118. RCT_EXTERN NSString *RCTHumanReadableType(NSObject *obj);
  119. // URL manipulation
  120. RCT_EXTERN NSString *__nullable RCTGetURLQueryParam(NSURL *__nullable URL, NSString *param);
  121. RCT_EXTERN NSURL *__nullable
  122. RCTURLByReplacingQueryParam(NSURL *__nullable URL, NSString *param, NSString *__nullable value);
  123. // Given a string, drop common RN prefixes (RCT, RK, etc.)
  124. RCT_EXTERN NSString *RCTDropReactPrefixes(NSString *s);
  125. RCT_EXTERN BOOL RCTUIManagerTypeForTagIsFabric(NSNumber *reactTag);
  126. RCT_EXTERN BOOL RCTValidateTypeOfViewCommandArgument(
  127. NSObject *obj,
  128. id expectedClass,
  129. NSString const *expectedType,
  130. NSString const *componentName,
  131. NSString const *commandName,
  132. NSString const *argPos);
  133. NS_ASSUME_NONNULL_END