|
| 1 | +#import <Foundation/Foundation.h> |
| 2 | + |
| 3 | +@class SPTAppRemote; |
| 4 | +@class SPTAppRemoteConnectionParams; |
| 5 | +@class SPTConfiguration; |
| 6 | + |
| 7 | +@protocol SPTAppRemoteImageAPI; |
| 8 | +@protocol SPTAppRemotePlayerAPI; |
| 9 | +@protocol SPTAppRemoteUserAPI; |
| 10 | +@protocol SPTAppRemoteContentAPI; |
| 11 | + |
| 12 | +NS_ASSUME_NONNULL_BEGIN |
| 13 | + |
| 14 | +extern NSString * const SPTAppRemoteAccessTokenKey; |
| 15 | +extern NSString * const SPTAppRemoteErrorDescriptionKey; |
| 16 | + |
| 17 | +/// The severity of log messages that the App Remote should log to console. |
| 18 | +typedef NS_ENUM(NSUInteger, SPTAppRemoteLogLevel) { |
| 19 | + /// Do not log at all. |
| 20 | + SPTAppRemoteLogLevelNone = 0, |
| 21 | + /// Log debug, info and error messages. |
| 22 | + SPTAppRemoteLogLevelDebug = 1, |
| 23 | + /// Log info and error messages. |
| 24 | + SPTAppRemoteLogLevelInfo = 2, |
| 25 | + /// Log only error messages. |
| 26 | + SPTAppRemoteLogLevelError = 3, |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * The `SPTAppRemoteDelegate` receives updates from the `SPTAppRemote` whenever something has |
| 31 | + * happened with the connection. |
| 32 | + */ |
| 33 | +@protocol SPTAppRemoteDelegate <NSObject> |
| 34 | + |
| 35 | +/** |
| 36 | + * Called when the App Remote has established connection with the Spotify app. |
| 37 | + * |
| 38 | + * @param appRemote The transport that has connected. |
| 39 | + */ |
| 40 | +- (void)appRemoteDidEstablishConnection:(SPTAppRemote *)appRemote; |
| 41 | + |
| 42 | +/** |
| 43 | + * Called when the connection attempt made by the App Remote failed. |
| 44 | + * |
| 45 | + * @param appRemote The App Remote that failed to connect. |
| 46 | + * @param error The error that occurred. |
| 47 | + */ |
| 48 | +- (void)appRemote:(SPTAppRemote *)appRemote didFailConnectionAttemptWithError:(nullable NSError *)error; |
| 49 | + |
| 50 | +/** |
| 51 | + * Called when the App Remote has disconnected. |
| 52 | + * |
| 53 | + * @note All APIs will be released by the App Remote at this point. The will no longer be usable, |
| 54 | + * and so you should release them as well. |
| 55 | + * |
| 56 | + * @param appRemote The App Remote that disconnected. |
| 57 | + * @param error The error that caused the disconnect, or `nil` if the disconnect was explicit. |
| 58 | + */ |
| 59 | +- (void)appRemote:(SPTAppRemote *)appRemote didDisconnectWithError:(nullable NSError *)error; |
| 60 | + |
| 61 | +@end |
| 62 | + |
| 63 | +/** |
| 64 | + * The `SPTAppRemote` is the main entry point for interacting with the Spotify app using the Spotify App Remote for iOS. |
| 65 | + */ |
| 66 | +@interface SPTAppRemote : NSObject |
| 67 | + |
| 68 | +#pragma mark Lifecycle |
| 69 | + |
| 70 | +/** |
| 71 | + * Convenience Initializer for a new App Remote instance |
| 72 | + * |
| 73 | + * @param configuration The `SPTConfiguration` to use for client-id's and redirect URLs |
| 74 | + * @param logLevel The lowest severity to log to console. |
| 75 | + * |
| 76 | + * @return A fresh new App Remote, ready to connect. |
| 77 | + */ |
| 78 | +- (instancetype)initWithConfiguration:(SPTConfiguration *)configuration logLevel:(SPTAppRemoteLogLevel)logLevel; |
| 79 | + |
| 80 | +/** |
| 81 | + * Designated Initializer for a new App Remote instance |
| 82 | + * |
| 83 | + * @param configuration The `SPTConfiguration` to use for client-id's and redirect URLs |
| 84 | + * @param connectionParameters `SPTAppRemoteConnectionParams` for custom image sizes and types, and to hold the accessToken |
| 85 | + * @param logLevel The lowest severity to log to console. |
| 86 | + * |
| 87 | + * @return A fresh new App Remote, ready to connect. |
| 88 | + */ |
| 89 | +- (instancetype)initWithConfiguration:(SPTConfiguration *)configuration |
| 90 | + connectionParameters:(SPTAppRemoteConnectionParams *)connectionParameters |
| 91 | + logLevel:(SPTAppRemoteLogLevel)logLevel NS_DESIGNATED_INITIALIZER; |
| 92 | +#pragma mark Class Methods |
| 93 | + |
| 94 | +/** |
| 95 | + * Checks if the Spotify app is active on the user's device. You can use this to determine if maybe you should prompt |
| 96 | + * the user to connect to Spotify (because you know they are already using Spotify if it is active). The Spotify app |
| 97 | + * will be considered active if music is playing or the app is active in the background. |
| 98 | + * |
| 99 | + * @param completion Completion block for determining the result of the check. YES if Spotify is active, othewise NO. |
| 100 | + */ |
| 101 | ++ (void)checkIfSpotifyAppIsActive:(void (^)(BOOL active))completion; |
| 102 | + |
| 103 | +/** |
| 104 | + * Determine the current version of the Spotify App Remote |
| 105 | + * |
| 106 | + * @return The current version of the Spotify App Remote |
| 107 | + */ |
| 108 | ++ (NSString *)appRemoteVersion; |
| 109 | + |
| 110 | +/** |
| 111 | + * The Spotify app iTunes item identifier for use with `SKStoreProductViewController` for installing Spotify from the App Store. |
| 112 | + * |
| 113 | + * @return An `NSNumber` representing the Spotify iTunes item identifier to be used for the `SKStoreProductParameterITunesItemIdentifier` key |
| 114 | + */ |
| 115 | ++ (NSNumber *)spotifyItunesItemIdentifier; |
| 116 | + |
| 117 | +#pragma mark Connection |
| 118 | + |
| 119 | +/** |
| 120 | + * The parameters to use during connection. |
| 121 | + */ |
| 122 | +@property (nonatomic, strong, readonly) SPTAppRemoteConnectionParams *connectionParameters; |
| 123 | + |
| 124 | +/** |
| 125 | + * `YES` if the App Remote is connected to the Spotify application, otherwise `NO`. |
| 126 | + * |
| 127 | + * @note Not KVO’able. |
| 128 | + * |
| 129 | + * See The `SPTAppRemoteDelegate` in order to receive updates when the connection status changes. |
| 130 | + */ |
| 131 | +@property (nonatomic, assign, readonly, getter=isConnected) BOOL connected; |
| 132 | + |
| 133 | +/** |
| 134 | + * The delegate to notify for connection status changes and other events originating from the App Remote. |
| 135 | + */ |
| 136 | +@property (nonatomic, weak) id<SPTAppRemoteDelegate> delegate; |
| 137 | + |
| 138 | +/** |
| 139 | + * Attempts to connect to the Spotify application. |
| 140 | + * |
| 141 | + * @discussion If the Spotify app is not running you will need to use authorizeAndPlayURI: to wake it up |
| 142 | + */ |
| 143 | +- (void)connect; |
| 144 | + |
| 145 | +/** |
| 146 | + * Disconnect from the Spotify application |
| 147 | + */ |
| 148 | +- (void)disconnect; |
| 149 | + |
| 150 | +/** |
| 151 | + * Open Spotify app to obtain access token and start playback. |
| 152 | + * |
| 153 | + * @param URI The URI to play. Use a blank string to attempt to play the user's last song |
| 154 | + * |
| 155 | + * @return `YES` if the Spotify app is installed and an authorization attempt can be made, otherwise `NO`. |
| 156 | + * Note: The return `BOOL` here is not a measure of whether or not authentication succeeded, only a check if |
| 157 | + * the Spotify app is installed and can attempt to handle the authorization request. |
| 158 | + */ |
| 159 | +- (BOOL)authorizeAndPlayURI:(NSString *)URI; |
| 160 | + |
| 161 | +/** |
| 162 | + * Open Spotify app to obtain access token and start playback. |
| 163 | + * |
| 164 | + * @param playURI The URI to play. Use a blank string to attempt to play the user's last song |
| 165 | + * @param asRadio `YES` to start radio for the given URI. |
| 166 | + * |
| 167 | + * @return `YES` if the Spotify app is installed and an authorization attempt can be made, otherwise `NO`. |
| 168 | + * Note: The return `BOOL` here is not a measure of whether or not authentication succeeded, only a check if |
| 169 | + * the Spotify app is installed and can attempt to handle the authorization request. |
| 170 | + */ |
| 171 | +- (BOOL)authorizeAndPlayURI:(NSString *)playURI asRadio:(BOOL)asRadio; |
| 172 | + |
| 173 | +/** |
| 174 | +* Open Spotify app to obtain access token and start playback. |
| 175 | +* |
| 176 | +* @param playURI The URI to play. Use a blank string to attempt to play the user's last song |
| 177 | +* @param asRadio `YES` to start radio for the given URI. |
| 178 | +* @param additionalScopes An array of scopes in addition to `app-remote-control`. Can be nil if you only need `app-remote-control` |
| 179 | +* |
| 180 | +* @return `YES` if the Spotify app is installed and an authorization attempt can be made, otherwise `NO`. |
| 181 | +* Note: The return `BOOL` here is not a measure of whether or not authentication succeeded, only a check if |
| 182 | +* the Spotify app is installed and can attempt to handle the authorization request. |
| 183 | +*/ |
| 184 | +- (BOOL)authorizeAndPlayURI:(NSString *)playURI |
| 185 | + asRadio:(BOOL)asRadio |
| 186 | + additionalScopes:(nullable NSArray<NSString *> *)additionalScopes; |
| 187 | + |
| 188 | +/** |
| 189 | + * Parse out an access token or error description from a url passed to application:openURL:options: |
| 190 | + * |
| 191 | + * @param url The URL returned from the Spotify app after calling authorizeAndPlayURI |
| 192 | + * |
| 193 | + * @return A dictionary containing the access token or error description from the provided URL. |
| 194 | + * Will return nil if the URL Scheme does not match the redirect URI provided. |
| 195 | + * Use `SPTAppRemoteAccessTokenKey` and `SPTAppRemoteErrorDescriptionKey` to get the appropriate values. |
| 196 | + */ |
| 197 | +- (nullable NSDictionary<NSString *, NSString *> *)authorizationParametersFromURL:(NSURL *)url; |
| 198 | + |
| 199 | +#pragma mark APIs |
| 200 | + |
| 201 | +/** |
| 202 | + * The API used to control the Spotify player. |
| 203 | + * |
| 204 | + * @note Will only be populated when the App Remote is connected. If you retain this object you must release it on |
| 205 | + * disconnect. |
| 206 | + */ |
| 207 | +@property (nullable, nonatomic, strong, readonly) id<SPTAppRemotePlayerAPI> playerAPI; |
| 208 | + |
| 209 | +/** |
| 210 | + * The API used to fetch images from the Spotify app. |
| 211 | + * |
| 212 | + * @note Will only be populated when the App Remote is connected. If you retain this object you must release it on |
| 213 | + * disconnect. |
| 214 | + */ |
| 215 | +@property (nullable, nonatomic, strong, readonly) id<SPTAppRemoteImageAPI> imageAPI; |
| 216 | + |
| 217 | +/** |
| 218 | + * The API used to fetch user data from the Spotify app. |
| 219 | + * |
| 220 | + * @note Will only be populated when the App Remote is connected. If you retain this object you must release it on |
| 221 | + * disconnect. |
| 222 | + */ |
| 223 | +@property (nullable, nonatomic, strong, readonly) id<SPTAppRemoteUserAPI> userAPI; |
| 224 | + |
| 225 | +/** |
| 226 | + * The API used to fetch content from the Spotify app. |
| 227 | + * |
| 228 | + * @note Will only be populated when the App Remote is connected. If you retain this object you must release it on |
| 229 | + * disconnect. |
| 230 | + */ |
| 231 | +@property (nullable, nonatomic, strong, readonly) id<SPTAppRemoteContentAPI> contentAPI; |
| 232 | + |
| 233 | +#pragma mark Unavailable initializers |
| 234 | + |
| 235 | +- (instancetype)init NS_UNAVAILABLE; |
| 236 | ++ (instancetype)new NS_UNAVAILABLE; |
| 237 | + |
| 238 | +@end |
| 239 | + |
| 240 | +NS_ASSUME_NONNULL_END |
0 commit comments