npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nuxstep/nativescript-spotify

v0.4.0

Published

Spotify SDK integration for NativeScript (iOS and Android)

Downloads

13

Readme

@nuxstep/nativescript-spotify

IMPORTANT: This project is in early development

About

This project surged from our own needs, as there's no Spotify plugin available for NativeScript (or at least updated).

For now, the goal of this plugin is simple: Integrate Spotify into your app through the Spotify App Remote SDK, so you can control playback and get the user recommended content items.

Later (maybe), we can implement the Spotify Authentication SDK and Web API for more features, but that will depend on our available time and needs, so, feel free to help the project through issue submissions and pull requests.

Setup

Add the plugin to your NativeScript project with:

ns plugin add @nuxstep/nativescript-spotify

Android

In your app project, head over to App_Resources/Android/src/main/res and open AndroidManifest.xml. Inside the <activity> tag with the name com.tns.NativeScriptActivity, add the property android:launchMode="singleTask", as follows:

<activity
  android:name="com.tns.NativeScriptActivity"
  android:label="@string/title_activity_kimera"
  android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode"
  android:theme="@style/LaunchScreenTheme"
  android:launchMode="singleTask">

Before the first <activity> tag is closed, add the following:

<!-- Custom URI schemes -->
<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />

  <data
    android:scheme="plugindemo"
    android:host="spotify-login-callback" />
</intent-filter>

Here you will set the Redirect URI for the Spotify App Remote authentication (it reads as plugindemo://spotify-login-callback). Change the android:scheme to something related to your app (like myapp) and host you can keep it as spotify-login-callback. Write down your Redirect URI, you will need it to set up the SpotifyAppRemote class.

Before the <application> tag closes, add the following:

<!-- Spotify Activity -->
<activity
  android:name="com.spotify.sdk.android.authentication.LoginActivity"
  android:theme="@android:style/Theme.Translucent.NoTitleBar" />

For Android 11 or higher, you also need to specify which other applications your application can query (read about it here). To do that, simply add the following snippet one level below the <manifest> tag:

<queries>
  <package android:name="com.spotify.music" />
</queries>

You'll need an App Fingerprint too. Refer to https://developer.spotify.com/documentation/android/quick-start/#register-app-fingerprints on how to create one. Write it down because you will need it later.

iOS

In your app project, head over to App_Resources/iOS and open Info.plist. At the end of the file, before the last </dict> tag closes, add the following:

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>spotify</string>
</array>

This allows the Spotify SDK to open the Spotify app through its URL scheme. After that, add the following:

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLName</key>
    <string>com.nuxstep.nativescript.plugindemo</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>plugindemo</string>
    </array>
  </dict>
</array>

Change com.nuxstep.nativescript.plugindemo to your app bundle name. Also, change plugindemo to your app URL scheme. Use the same URL scheme from Android for the sake of simplicity.

In your app entry point (usually app.ts), import isIOS and SpotifyAppRemote at the top of the file:

import { isIOS } from '@nativescript/core';
import { SpotifyAppRemote } from '@nuxstep/nativescript-spotify';

Then, implement a custom app delegate before your app starts, usually before Application.run() if you are using plain NativeScript.

/**
 * Implement a custom AppDelegate on iOS so we can get the access token
 * returned from Spotify and store it on SpotifyAppRemote class
 */
if (isIOS) {
	@NativeClass()
	class AppDelegate extends UIResponder implements UIApplicationDelegate {
		public static ObjCProtocols = [UIApplicationDelegate];

		applicationOpenURLOptions(_application: UIApplication, url: NSURL, _options: any): boolean {
			SpotifyAppRemote.setAuthorizationParameters(url);
			return true;
		}
	}
	Application.ios.delegate = AppDelegate;
}

If you are not using TypeScript, refer to https://v7.docs.nativescript.org/core-concepts/application-lifecycle#ios-uiapplicationdelegate on how to implement the app delegate with JavaScript.

Spotify Developer

Head over to https://developer.spotify.com/dashboard and register a developer account.

In the dashboard, click on CREATE AN APP and provide a name and an description.

Inside your app dashboard, click on EDIT SETTINGS. Set the Redirect URI as you have defined before (e.g. myapp://spotify-login-callback) and click on ADD.

On section Bundle IDs, insert your app bundle name (e.g. com.example.myapp) and click on ADD. This one is for iOS.

On section Android Packages, insert your app package name (e.g. com.example.myapp) and insert the App Fingerprint you've created before. Click on ADD and then on SAVE to finish.

Write down your Client ID, you will need it to set up the SpotifyAppRemote class.

Usage

First, you need to pass the Client ID and Redirect URI to the SpotifyAppRemote class. You can do that in your app entry point before the custom app delegate:

SpotifyAppRemote.setClientId('SPOTIFY_CLIENT_ID');
SpotifyAppRemote.setRedirectUri('APP_REDIRECT_URI');

In the page where you want to use SpotifyAppRemote, add to the top of the file:

import { isIOS } from '@nativescript/core';
import { SpotifyAppRemote } from '@nuxstep/nativescript-spotify';

Then, somewhere you want, connect to SpotifyAppRemote:

/**
 * If platform is iOS, we need to open the app and start playback
 * before connecting to SpotifyAppRemote. This is an iOS-specific
 * limitation.
 */
if (isIOS) {
	await SpotifyAppRemote.authorizeAndPlayURI();
}

await SpotifyAppRemote.connect();

You can pass any Spotify URI to authorizeAndPlayURI(). If you pass an empty string or no parameter at all, it'll try to play the user's last played song.

To understand how to use the plugin, refer to:

This README will be updated with the available methods at a later time.

License

Apache License Version 2.0