gausam / ios-auth

Spotify authentication and authorization for iOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SpotifyLogin

Overview

SpotifyLogin is a lightweight framework that enables your application to obtain the authentication code from the Spotify app. Please note that this framework is currently under development and only supports a subset of the ios-sdk's functionalities. If you wish to use all features related to authentication, please utilize ios-sdk.

Minimum Requirement

iOS 11

Usage

Prepare Your Environment

  • Install the latest version of Spotify from the App Store onto the device you will be using for development. Run the Spotify app and login or sign up.
  • Register Your Application. You will need to register your application at My Applications and obtain a client ID. When you register your app you will also need to allowlist a redirect URI that the Spotify app will use to callback to your app after authorization.

Installation

  • Add SpotifyLogin.xcframework to your project by dragging and dropping it in Framworks, Libraries, and Embedded Content
  • In your info.plist add the following changes:
    • Add your redirect URI you registered at My Applications. You will need to add your redirect URI under "URL types" and "URL Schemes". Be sure to set a unique "URL identifier" as well. More info on URL Schemes
    • Declare the Spotify’s URL scheme spotify by adding the LSApplicationQueriesSchemes key.

Get authentication code

1. Initialise Configuration with your client ID and redirect URI.

Swift
import SpotifyLogin

let configuration = Configuration(clientID: "your_client_id", redirectURLString: "your_redirect_uri")
Objective-C
#import <SpotifyLogin/SpotifyLogin.h>

SPTConfiguration* configuration = [[SPTConfiguration alloc] initWithClientID:@"your_client_id" redirectURLString:@"your_redirect_uri"];

2. Initialise SessionManager with your configuration and set an object that conforms SessionManagerDelegate to the delegate of the instance.

Swift
let sessionManager = SessionManager(configuration: configuration)
sessionManager.delegate = <#delegate object#>
Objective-C
_sessionManager = [[SPTSessionManager alloc] initWithConfiguration:configuration];
_sessionManager.delegate = <#delegate object#>;

3. Implement application(_:open:options:) method to your UIApplicationDelegate and call sessionManager's application(_:open:options:) there.

Swift
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return sessionManager.openURL(url)
    }
}
Objective-C
@implementation AppDelegate
   
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    return [_sessionManager openURL:url];
}

@end

4. Start the authrization process with the scopes you need.

Swift
sessionManager.startAuthorizationCodeProcess(with: [.playlistModifyPublic, .playlistModifyPrivate])
Objective-C
[_sessionManager startAuthorizationCodeProcessWith:SPTScopePlaylistModifyPublic|SPTScopePlaylistModifyPublic];

About

Spotify authentication and authorization for iOS.

License:Apache License 2.0


Languages

Language:Objective-C 90.5%Language:Swift 7.6%Language:Ruby 1.9%