RCTUtils.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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> *RCTMakeError(NSString *message, id __nullable toStringify, NSDictionary<NSString *, id> *__nullable extraData);
  49. RCT_EXTERN NSDictionary<NSString *, id> *RCTMakeAndLogError(NSString *message, id __nullable toStringify, NSDictionary<NSString *, id> *__nullable extraData);
  50. RCT_EXTERN NSDictionary<NSString *, id> *RCTJSErrorFromNSError(NSError *error);
  51. RCT_EXTERN NSDictionary<NSString *, id> *RCTJSErrorFromCodeMessageAndNSError(NSString *code, NSString *message, NSError *__nullable error);
  52. // The default error code to use as the `code` property for callback error objects
  53. RCT_EXTERN NSString *const RCTErrorUnspecified;
  54. // Returns YES if React is running in a test environment
  55. RCT_EXTERN BOOL RCTRunningInTestEnvironment(void);
  56. // Returns YES if React is running in an iOS App Extension
  57. RCT_EXTERN BOOL RCTRunningInAppExtension(void);
  58. // Returns the shared UIApplication instance, or nil if running in an App Extension
  59. RCT_EXTERN UIApplication *__nullable RCTSharedApplication(void);
  60. // Returns the current main window, useful if you need to access the root view
  61. // or view controller
  62. RCT_EXTERN UIWindow *__nullable RCTKeyWindow(void);
  63. // Returns the presented view controller, useful if you need
  64. // e.g. to present a modal view controller or alert over it
  65. RCT_EXTERN UIViewController *__nullable RCTPresentedViewController(void);
  66. // Does this device support force touch (aka 3D Touch)?
  67. RCT_EXTERN BOOL RCTForceTouchAvailable(void);
  68. // Create an NSError in the RCTErrorDomain
  69. RCT_EXTERN NSError *RCTErrorWithMessage(NSString *message);
  70. // Convert nil values to NSNull, and vice-versa
  71. #define RCTNullIfNil(value) (value ?: (id)kCFNull)
  72. #define RCTNilIfNull(value) \
  73. ({ __typeof__(value) t = (value); (id)t == (id)kCFNull ? (__typeof(value))nil : t; })
  74. // Convert NaN or infinite values to zero, as these aren't JSON-safe
  75. RCT_EXTERN double RCTZeroIfNaN(double value);
  76. // Returns `0` and log special warning if value is NaN or INF.
  77. RCT_EXTERN double RCTSanitizeNaNValue(double value, NSString *property);
  78. // Convert data to a Base64-encoded data URL
  79. RCT_EXTERN NSURL *RCTDataURL(NSString *mimeType, NSData *data);
  80. // Gzip functionality - compression level in range 0 - 1 (-1 for default)
  81. RCT_EXTERN NSData *__nullable RCTGzipData(NSData *__nullable data, float level);
  82. // Returns the relative path within the main bundle for an absolute URL
  83. // (or nil, if the URL does not specify a path within the main bundle)
  84. RCT_EXTERN NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL);
  85. // Returns the Path of Library directory
  86. RCT_EXTERN NSString *__nullable RCTLibraryPath(void);
  87. // Returns the relative path within the library for an absolute URL
  88. // (or nil, if the URL does not specify a path within the Library directory)
  89. RCT_EXTERN NSString *__nullable RCTLibraryPathForURL(NSURL *__nullable URL);
  90. // Determines if a given image URL refers to a image in bundle
  91. RCT_EXTERN BOOL RCTIsBundleAssetURL(NSURL *__nullable imageURL);
  92. // Determines if a given image URL refers to a image in library
  93. RCT_EXTERN BOOL RCTIsLibraryAssetURL(NSURL *__nullable imageURL);
  94. // Determines if a given image URL refers to a local image
  95. RCT_EXTERN BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL);
  96. // Returns an UIImage for a local image asset. Returns nil if the URL
  97. // does not correspond to a local asset.
  98. RCT_EXTERN UIImage *__nullable RCTImageFromLocalAssetURL(NSURL *imageURL);
  99. // Creates a new, unique temporary file path with the specified extension
  100. RCT_EXTERN NSString *__nullable RCTTempFilePath(NSString *__nullable extension, NSError **error);
  101. // Converts a CGColor to a hex string
  102. RCT_EXTERN NSString *RCTColorToHexString(CGColorRef color);
  103. // Get standard localized string (if it exists)
  104. RCT_EXTERN NSString *RCTUIKitLocalizedString(NSString *string);
  105. // URL manipulation
  106. RCT_EXTERN NSString *__nullable RCTGetURLQueryParam(NSURL *__nullable URL, NSString *param);
  107. RCT_EXTERN NSURL *__nullable RCTURLByReplacingQueryParam(NSURL *__nullable URL, NSString *param, NSString *__nullable value);
  108. // Given a string, drop common RN prefixes (RCT, RK, etc.)
  109. RCT_EXTERN NSString *RCTDropReactPrefixes(NSString *s);
  110. NS_ASSUME_NONNULL_END