RLMApp.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2020 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import <Realm/RLMConstants.h>
  19. #import <AuthenticationServices/AuthenticationServices.h>
  20. RLM_HEADER_AUDIT_BEGIN(nullability, sendability)
  21. @protocol RLMNetworkTransport, RLMBSON;
  22. @class RLMUser, RLMCredentials, RLMSyncManager, RLMEmailPasswordAuth, RLMPushClient, RLMSyncTimeoutOptions;
  23. /// A block type used for APIs which asynchronously vend an `RLMUser`.
  24. typedef void(^RLMUserCompletionBlock)(RLMUser * _Nullable, NSError * _Nullable);
  25. /// A block type used to report an error
  26. typedef void(^RLMOptionalErrorBlock)(NSError * _Nullable);
  27. #pragma mark RLMAppConfiguration
  28. /// Properties representing the configuration of a client
  29. /// that communicate with a particular Realm application.
  30. @interface RLMAppConfiguration : NSObject <NSCopying>
  31. /// A custom base URL to request against.
  32. @property (nonatomic, strong, nullable) NSString *baseURL;
  33. /// The custom transport for network calls to the server.
  34. @property (nonatomic, strong, nullable) id<RLMNetworkTransport> transport;
  35. /// :nodoc:
  36. @property (nonatomic, strong, nullable) NSString *localAppName
  37. __attribute__((deprecated("This field is not used")));
  38. /// :nodoc:
  39. @property (nonatomic, strong, nullable) NSString *localAppVersion
  40. __attribute__((deprecated("This field is not used")));
  41. /// The default timeout for network requests.
  42. @property (nonatomic, assign) NSUInteger defaultRequestTimeoutMS;
  43. /// If enabled (the default), a single connection is used for all Realms opened
  44. /// with a single sync user. If disabled, a separate connection is used for each
  45. /// Realm.
  46. ///
  47. /// Session multiplexing reduces resources used and typically improves
  48. /// performance. When multiplexing is enabled, the connection is not immediately
  49. /// closed when the last session is closed, and instead remains open for
  50. /// ``RLMSyncTimeoutOptions.connectionLingerTime`` milliseconds (30 seconds by
  51. /// default).
  52. @property (nonatomic, assign) BOOL enableSessionMultiplexing;
  53. /**
  54. Options for the assorted types of connection timeouts for sync connections.
  55. If nil default values for all timeouts are used instead.
  56. */
  57. @property (nonatomic, nullable, copy) RLMSyncTimeoutOptions *syncTimeouts;
  58. /// :nodoc:
  59. - (instancetype)initWithBaseURL:(nullable NSString *)baseURL
  60. transport:(nullable id<RLMNetworkTransport>)transport
  61. localAppName:(nullable NSString *)localAppName
  62. localAppVersion:(nullable NSString *)localAppVersion
  63. __attribute__((deprecated("localAppName and localAppVersion are unused")));
  64. /// :nodoc:
  65. - (instancetype)initWithBaseURL:(nullable NSString *) baseURL
  66. transport:(nullable id<RLMNetworkTransport>)transport
  67. localAppName:(nullable NSString *)localAppName
  68. localAppVersion:(nullable NSString *)localAppVersion
  69. defaultRequestTimeoutMS:(NSUInteger)defaultRequestTimeoutMS
  70. __attribute__((deprecated("localAppName and localAppVersion are unused")));
  71. /**
  72. Create a new Realm App configuration.
  73. @param baseURL A custom base URL to request against.
  74. @param transport A custom network transport.
  75. */
  76. - (instancetype)initWithBaseURL:(nullable NSString *)baseURL
  77. transport:(nullable id<RLMNetworkTransport>)transport;
  78. /**
  79. Create a new Realm App configuration.
  80. @param baseURL A custom base URL to request against.
  81. @param transport A custom network transport.
  82. @param defaultRequestTimeoutMS A custom default timeout for network requests.
  83. */
  84. - (instancetype)initWithBaseURL:(nullable NSString *) baseURL
  85. transport:(nullable id<RLMNetworkTransport>)transport
  86. defaultRequestTimeoutMS:(NSUInteger)defaultRequestTimeoutMS;
  87. @end
  88. #pragma mark RLMApp
  89. /**
  90. The `RLMApp` has the fundamental set of methods for communicating with a Realm
  91. application backend.
  92. This interface provides access to login and authentication.
  93. */
  94. RLM_SWIFT_SENDABLE RLM_FINAL // internally thread-safe
  95. @interface RLMApp : NSObject
  96. /// The configuration for this Realm app.
  97. @property (nonatomic, readonly) RLMAppConfiguration *configuration;
  98. /// The `RLMSyncManager` for this Realm app.
  99. @property (nonatomic, readonly) RLMSyncManager *syncManager;
  100. /// Get a dictionary containing all users keyed on id.
  101. @property (nonatomic, readonly) NSDictionary<NSString *, RLMUser *> *allUsers;
  102. /// Get the current user logged into the Realm app.
  103. @property (nonatomic, readonly, nullable) RLMUser *currentUser;
  104. /// The app ID for this Realm app.
  105. @property (nonatomic, readonly) NSString *appId;
  106. /**
  107. A client for the email/password authentication provider which
  108. can be used to obtain a credential for logging in.
  109. Used to perform requests specifically related to the email/password provider.
  110. */
  111. @property (nonatomic, readonly) RLMEmailPasswordAuth *emailPasswordAuth;
  112. /**
  113. Get an application with a given appId and configuration.
  114. @param appId The unique identifier of your Realm app.
  115. */
  116. + (instancetype)appWithId:(NSString *)appId;
  117. /**
  118. Get an application with a given appId and configuration.
  119. @param appId The unique identifier of your Realm app.
  120. @param configuration A configuration object to configure this client.
  121. */
  122. + (instancetype)appWithId:(NSString *)appId
  123. configuration:(nullable RLMAppConfiguration *)configuration;
  124. /**
  125. Login to a user for the Realm app.
  126. @param credentials The credentials identifying the user.
  127. @param completion A callback invoked after completion.
  128. */
  129. - (void)loginWithCredential:(RLMCredentials *)credentials
  130. completion:(RLMUserCompletionBlock)completion NS_REFINED_FOR_SWIFT;
  131. /**
  132. Switches the active user to the specified user.
  133. This sets which user is used by all RLMApp operations which require a user. This is a local operation which does not access the network.
  134. An exception will be throw if the user is not valid. The current user will remain logged in.
  135. @param syncUser The user to switch to.
  136. @returns The user you intend to switch to
  137. */
  138. - (RLMUser *)switchToUser:(RLMUser *)syncUser;
  139. /**
  140. A client which can be used to register devices with the server to receive push notificatons
  141. */
  142. - (RLMPushClient *)pushClientWithServiceName:(NSString *)serviceName
  143. NS_SWIFT_NAME(pushClient(serviceName:));
  144. /**
  145. RLMApp instances are cached internally by Realm and cannot be created directly.
  146. Use `+[RLMRealm appWithId]` or `+[RLMRealm appWithId:configuration:]`
  147. to obtain a reference to an RLMApp.
  148. */
  149. - (instancetype)init __attribute__((unavailable("Use +appWithId or appWithId:configuration:.")));
  150. /**
  151. RLMApp instances are cached internally by Realm and cannot be created directly.
  152. Use `+[RLMRealm appWithId]` or `+[RLMRealm appWithId:configuration:]`
  153. to obtain a reference to an RLMApp.
  154. */
  155. + (instancetype)new __attribute__((unavailable("Use +appWithId or appWithId:configuration:.")));
  156. @end
  157. #pragma mark - Sign In With Apple Extension
  158. API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
  159. /// Use this delegate to be provided a callback once authentication has succeed or failed
  160. @protocol RLMASLoginDelegate
  161. /// Callback that is invoked should the authentication fail.
  162. /// @param error An error describing the authentication failure.
  163. - (void)authenticationDidFailWithError:(NSError *)error NS_SWIFT_NAME(authenticationDidComplete(error:));
  164. /// Callback that is invoked should the authentication succeed.
  165. /// @param user The newly authenticated user.
  166. - (void)authenticationDidCompleteWithUser:(RLMUser *)user NS_SWIFT_NAME(authenticationDidComplete(user:));
  167. @end
  168. API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
  169. /// Category extension that deals with Sign In With Apple authentication.
  170. /// This is only available on OS's that support `AuthenticationServices`
  171. @interface RLMApp (ASLogin)
  172. /// Use this delegate to be provided a callback once authentication has succeed or failed.
  173. @property (nonatomic, weak, nullable) id<RLMASLoginDelegate> authorizationDelegate;
  174. /// Sets the ASAuthorizationControllerDelegate to be handled by `RLMApp`
  175. /// @param controller The ASAuthorizationController in which you want `RLMApp` to consume its delegate.
  176. - (void)setASAuthorizationControllerDelegateForController:(ASAuthorizationController *)controller NS_REFINED_FOR_SWIFT;
  177. @end
  178. RLM_HEADER_AUDIT_END(nullability, sendability)