RLMApp.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. ///
  31. /// `RLMAppConfiguration` options cannot be modified once the `RLMApp` using it
  32. /// is created. App's configuration values are cached when the App is created so any modifications after it
  33. /// will not have any effect.
  34. @interface RLMAppConfiguration : NSObject <NSCopying>
  35. /// A custom base URL to request against.
  36. @property (nonatomic, strong, nullable) NSString *baseURL;
  37. /// The custom transport for network calls to the server.
  38. @property (nonatomic, strong, nullable) id<RLMNetworkTransport> transport;
  39. /// :nodoc:
  40. @property (nonatomic, strong, nullable) NSString *localAppName
  41. __attribute__((deprecated("This field is not used")));
  42. /// :nodoc:
  43. @property (nonatomic, strong, nullable) NSString *localAppVersion
  44. __attribute__((deprecated("This field is not used")));
  45. /// The default timeout for network requests.
  46. @property (nonatomic, assign) NSUInteger defaultRequestTimeoutMS;
  47. /// If enabled (the default), a single connection is used for all Realms opened
  48. /// with a single sync user. If disabled, a separate connection is used for each
  49. /// Realm.
  50. ///
  51. /// Session multiplexing reduces resources used and typically improves
  52. /// performance. When multiplexing is enabled, the connection is not immediately
  53. /// closed when the last session is closed, and instead remains open for
  54. /// ``RLMSyncTimeoutOptions.connectionLingerTime`` milliseconds (30 seconds by
  55. /// default).
  56. @property (nonatomic, assign) BOOL enableSessionMultiplexing;
  57. /**
  58. Options for the assorted types of connection timeouts for sync connections.
  59. If nil default values for all timeouts are used instead.
  60. */
  61. @property (nonatomic, nullable, copy) RLMSyncTimeoutOptions *syncTimeouts;
  62. /// :nodoc:
  63. - (instancetype)initWithBaseURL:(nullable NSString *)baseURL
  64. transport:(nullable id<RLMNetworkTransport>)transport
  65. localAppName:(nullable NSString *)localAppName
  66. localAppVersion:(nullable NSString *)localAppVersion
  67. __attribute__((deprecated("localAppName and localAppVersion are unused")));
  68. /// :nodoc:
  69. - (instancetype)initWithBaseURL:(nullable NSString *) baseURL
  70. transport:(nullable id<RLMNetworkTransport>)transport
  71. localAppName:(nullable NSString *)localAppName
  72. localAppVersion:(nullable NSString *)localAppVersion
  73. defaultRequestTimeoutMS:(NSUInteger)defaultRequestTimeoutMS
  74. __attribute__((deprecated("localAppName and localAppVersion are unused")));
  75. /**
  76. Create a new Realm App configuration.
  77. @param baseURL A custom base URL to request against.
  78. @param transport A custom network transport.
  79. */
  80. - (instancetype)initWithBaseURL:(nullable NSString *)baseURL
  81. transport:(nullable id<RLMNetworkTransport>)transport;
  82. /**
  83. Create a new Realm App configuration.
  84. @param baseURL A custom base URL to request against.
  85. @param transport A custom network transport.
  86. @param defaultRequestTimeoutMS A custom default timeout for network requests.
  87. */
  88. - (instancetype)initWithBaseURL:(nullable NSString *) baseURL
  89. transport:(nullable id<RLMNetworkTransport>)transport
  90. defaultRequestTimeoutMS:(NSUInteger)defaultRequestTimeoutMS;
  91. @end
  92. #pragma mark RLMApp
  93. /**
  94. The `RLMApp` has the fundamental set of methods for communicating with a Realm
  95. application backend.
  96. This interface provides access to login and authentication.
  97. */
  98. RLM_SWIFT_SENDABLE RLM_FINAL // internally thread-safe
  99. @interface RLMApp : NSObject
  100. /// The configuration for this Realm app.
  101. @property (nonatomic, readonly) RLMAppConfiguration *configuration;
  102. /// The `RLMSyncManager` for this Realm app.
  103. @property (nonatomic, readonly) RLMSyncManager *syncManager;
  104. /// Get a dictionary containing all users keyed on id.
  105. @property (nonatomic, readonly) NSDictionary<NSString *, RLMUser *> *allUsers;
  106. /// Get the current user logged into the Realm app.
  107. @property (nonatomic, readonly, nullable) RLMUser *currentUser;
  108. /// The app ID for this Realm app.
  109. @property (nonatomic, readonly) NSString *appId;
  110. /**
  111. A client for the email/password authentication provider which
  112. can be used to obtain a credential for logging in.
  113. Used to perform requests specifically related to the email/password provider.
  114. */
  115. @property (nonatomic, readonly) RLMEmailPasswordAuth *emailPasswordAuth;
  116. /**
  117. Get an application with a given appId and configuration.
  118. @param appId The unique identifier of your Realm app.
  119. */
  120. + (instancetype)appWithId:(NSString *)appId;
  121. /**
  122. Get an application with a given appId and configuration.
  123. @param appId The unique identifier of your Realm app.
  124. @param configuration A configuration object to configure this client.
  125. */
  126. + (instancetype)appWithId:(NSString *)appId
  127. configuration:(nullable RLMAppConfiguration *)configuration;
  128. /**
  129. Login to a user for the Realm app.
  130. @param credentials The credentials identifying the user.
  131. @param completion A callback invoked after completion.
  132. */
  133. - (void)loginWithCredential:(RLMCredentials *)credentials
  134. completion:(RLMUserCompletionBlock)completion NS_REFINED_FOR_SWIFT;
  135. /**
  136. Switches the active user to the specified user.
  137. 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.
  138. An exception will be throw if the user is not valid. The current user will remain logged in.
  139. @param syncUser The user to switch to.
  140. @returns The user you intend to switch to
  141. */
  142. - (RLMUser *)switchToUser:(RLMUser *)syncUser;
  143. /**
  144. A client which can be used to register devices with the server to receive push notificatons
  145. */
  146. - (RLMPushClient *)pushClientWithServiceName:(NSString *)serviceName
  147. NS_SWIFT_NAME(pushClient(serviceName:));
  148. /**
  149. RLMApp instances are cached internally by Realm and cannot be created directly.
  150. Use `+[RLMRealm appWithId]` or `+[RLMRealm appWithId:configuration:]`
  151. to obtain a reference to an RLMApp.
  152. */
  153. - (instancetype)init __attribute__((unavailable("Use +appWithId or appWithId:configuration:.")));
  154. /**
  155. RLMApp instances are cached internally by Realm and cannot be created directly.
  156. Use `+[RLMRealm appWithId]` or `+[RLMRealm appWithId:configuration:]`
  157. to obtain a reference to an RLMApp.
  158. */
  159. + (instancetype)new __attribute__((unavailable("Use +appWithId or appWithId:configuration:.")));
  160. @end
  161. #pragma mark - Sign In With Apple Extension
  162. API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
  163. /// Use this delegate to be provided a callback once authentication has succeed or failed
  164. @protocol RLMASLoginDelegate
  165. /// Callback that is invoked should the authentication fail.
  166. /// @param error An error describing the authentication failure.
  167. - (void)authenticationDidFailWithError:(NSError *)error NS_SWIFT_NAME(authenticationDidComplete(error:));
  168. /// Callback that is invoked should the authentication succeed.
  169. /// @param user The newly authenticated user.
  170. - (void)authenticationDidCompleteWithUser:(RLMUser *)user NS_SWIFT_NAME(authenticationDidComplete(user:));
  171. @end
  172. API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
  173. /// Category extension that deals with Sign In With Apple authentication.
  174. /// This is only available on OS's that support `AuthenticationServices`
  175. @interface RLMApp (ASLogin)
  176. /// Use this delegate to be provided a callback once authentication has succeed or failed.
  177. @property (nonatomic, weak, nullable) id<RLMASLoginDelegate> authorizationDelegate;
  178. /// Sets the ASAuthorizationControllerDelegate to be handled by `RLMApp`
  179. /// @param controller The ASAuthorizationController in which you want `RLMApp` to consume its delegate.
  180. - (void)setASAuthorizationControllerDelegateForController:(ASAuthorizationController *)controller NS_REFINED_FOR_SWIFT;
  181. @end
  182. RLM_HEADER_AUDIT_END(nullability, sendability)