Skip to content

Commit 438b155

Browse files
committed
Updated install script to request url scheme and minor fixes
1 parent f097c6f commit 438b155

File tree

10 files changed

+69
-56
lines changed

10 files changed

+69
-56
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
test:
2-
@bin/test-all
2+
@export PLUGIN_ENV='test'; bin/test-all
33

44
test-ios:
55
@bin/test-ios
66

77
test-js:
8-
@bin/test-js
8+
@export PLUGIN_ENV='test'; bin/test-js
99

1010
test-appium:
1111
@bin/test-appium

README.md

+11-14
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,25 @@
22

33
[![Build Status](https://travis-ci.org/timflapper/cordova-spotify-plugin.svg?branch=master)](https://travis-ci.org/timflapper/cordova-spotify-plugin)
44

5-
This plugin provides a javascript API to Spotify's iOS SDK.
5+
This plugin provides a javascript API to Spotify's iOS SDK for Cordova applications.
66

7-
**Beware! this plugin is in a very early (alpha) stage. Please try it out but do not use for production.**
8-
9-
## Do not use this plugin in a production environment!
10-
Currently the Spotify iOS SDK is in beta and breaking changes can occur without prior notice.
7+
_Android integration is planned for a future release_
118

129
## Installation
1310

1411
1. Install the plugin:
1512

1613
`cordova plugin add https://github.com/timflapper/cordova-spotify-plugin`
1714

18-
2. Run the install script to download the `Spotify.framework`
19-
20-
`./plugins/com.timflapper.spotify/install.sh`
21-
22-
3. Add the iOS playform to your project.
15+
2. Add the iOS platform to your project (if needed):
2316

2417
`cordova platform add ios`
2518

26-
Or, if you already have ios as a platform please run:
27-
28-
`cordova prepare`
19+
3. The install script will start automatically. It will do two things:
20+
- Ask you for a [custom URL scheme](http://bit.ly/1u11ZUz).
21+
- Download and extract the Spotify iOS SDK.
2922

30-
4. Try it out!
23+
That's it!
3124

3225
## API
3326

@@ -39,3 +32,7 @@ You can use the Ruby script that is included in the Spotify iOS SDK Demo Project
3932

4033
- [Download the Spotify iOS SDK](https://github.com/spotify/ios-sdk/releases)
4134
- Follow the instructions from the [Spotify iOS SDK beginner's tutorial](https://developer.spotify.com/technologies/spotify-ios-sdk/tutorial/).
35+
36+
## License
37+
38+
[MIT](LICENSE)

install.sh

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
#!/bin/sh
22

3+
if [ "$PLUGIN_ENV" == "test" ]; then
4+
exit
5+
fi
6+
7+
DEFAULT_SCHEME="spotify-cordova"
8+
9+
echo "The Spotify SDK Plugin needs a URL scheme for authentication."
10+
echo "See http://bit.ly/1u11ZUz for more information"
11+
printf "Specify your URL scheme [$DEFAULT_SCHEME]: "
12+
13+
read urlscheme
14+
15+
if [ "$urlscheme" == "" ]; then
16+
urlscheme="$DEFAULT_SCHEME"
17+
fi
18+
19+
echo "Writing URL scheme to plugin.xml"
20+
21+
mv plugins/com.timflapper.spotify/plugin.xml plugins/com.timflapper.spotify/plugin.bak.xml
22+
sed "s/{{URL_SCHEME}}/$urlscheme/g" plugins/com.timflapper.spotify/plugin.bak.xml > plugins/com.timflapper.spotify/plugin.xml
23+
rm plugins/com.timflapper.spotify/plugin.bak.xml
24+
325
echo "Removing placeholder"
426
rm -rf plugins/com.timflapper.spotify/src/ios/Spotify.framework
527

@@ -9,9 +31,9 @@ cd plugins/com.timflapper.spotify/src/ios/tmp
931
curl -OL "https://github.com/spotify/ios-sdk/archive/beta-6.tar.gz"
1032

1133
echo "Extracting"
12-
tar xzvf beta-5.tar.gz
34+
tar xzvf beta-6.tar.gz
1335
cd ..
14-
mv tmp/ios-sdk-beta-5/Spotify.framework .
36+
mv tmp/ios-sdk-beta-6/Spotify.framework .
1537
rm -rf tmp
1638

1739
echo "Finished!"

plugin.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
</js-module>
3434

3535
<platform name="ios">
36-
<config-file parent="/*" target="config.xml">
36+
<hook type="before_plugin_install" src="install.sh" />
37+
38+
<config-file target="config.xml" parent="/*">
3739
<feature name="SpotifyPlugin">
3840
<param name="ios-package" value="SpotifyPlugin"/>
3941
</feature>
@@ -48,7 +50,7 @@
4850
<string>Spotify Auth</string>
4951
<key>CFBundleURLSchemes</key>
5052
<array>
51-
<string>spotify-ios-sdk-beta</string>
53+
<string>{{URL_SCHEME}}</string>
5254
</array>
5355
</dict>
5456
</array>

src/ios/SpotifyPlugin.h

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ NSDate *stringToDate(NSString *dateString);
1111

1212
@interface SpotifyPlugin : CDVPlugin
1313

14-
@property NSURL *callbackUrl;
15-
1614
/* Linked to SPTAuth */
1715
- (void)authenticate:(CDVInvokedUrlCommand*)command;
1816
- (void)isSessionValid:(CDVInvokedUrlCommand*)command;

src/ios/SpotifyPlugin.m

+7-12
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,23 @@
2929
return [getDateFormatter() dateFromString:dateString];
3030
}
3131

32-
@interface SpotifyPlugin()
33-
@end
34-
3532
@implementation SpotifyPlugin
36-
@synthesize callbackUrl;
37-
- (void)pluginInitialize
38-
{
39-
callbackUrl = [NSURL URLWithString:@"spotify-ios-sdk-beta://callback"];
40-
}
41-
4233
- (void)authenticate:(CDVInvokedUrlCommand*)command
4334
{
4435
__weak SpotifyPlugin* weakSelf = self;
4536

4637
NSArray *validResponseTypes = @[@"code", @"token"];
4738

48-
NSString *clientId = [command.arguments objectAtIndex:0];
49-
NSString *responseType = [command.arguments objectAtIndex:1];
39+
NSString *urlScheme = [command.arguments objectAtIndex:0];
40+
NSString *clientId = [command.arguments objectAtIndex:1];
41+
NSString *responseType = [command.arguments objectAtIndex:2];
42+
43+
NSURL *callbackUrl = [NSURL URLWithString: [NSString stringWithFormat:@"%@://callback", urlScheme]];
44+
5045
NSURL *tokenSwapUrl;
5146
NSArray *scopes;
5247

53-
int argumentIndex = 2;
48+
int argumentIndex = 3;
5449

5550
if ([validResponseTypes indexOfObject:responseType] == NSNotFound) {
5651
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Invalid responseType"];

test/ios/SpotifyPluginTests.m

+7-7
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ - (void)tearDown
7575

7676
- (void)testAuthenticateSuccessWithCode
7777
{
78-
NSArray *args = @[@"someRandomClientId", @"code", @"http://foo.bar:1234/swap", @[@"streaming"]];
78+
NSArray *args = @[@"test-url-scheme", @"someRandomClientId", @"code", @"http://foo.bar:1234/swap", @[@"streaming"]];
7979

8080
__block BOOL responseArrived = NO;
8181

@@ -105,7 +105,7 @@ - (void)testAuthenticateSuccessWithCode
105105
double delayInSeconds = 0.005;
106106
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
107107
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
108-
NSURL *callbackURL = [NSURL URLWithString:@"spotify-ios-sdk-beta://callback?code=NQpvC5h6MnausBFRG2hJjXifw2CZrXzQIh4S_SgBfpcVi6svpZKXpwYyoLRYhWN8g4L-zoZqYK0hfFNFgMqTpESGvodAuXGngZFiKc16y7oeMRJTZaY3-_1BgnSO9cLwzgMOztqUCRJV23LjtmEurM9_BEhSm-smLgqQHUrLtXldCz-JpDOkckA"];
108+
NSURL *callbackURL = [NSURL URLWithString:@"test-url-scheme://callback?code=NQpvC5h6MnausBFRG2hJjXifw2CZrXzQIh4S_SgBfpcVi6svpZKXpwYyoLRYhWN8g4L-zoZqYK0hfFNFgMqTpESGvodAuXGngZFiKc16y7oeMRJTZaY3-_1BgnSO9cLwzgMOztqUCRJV23LjtmEurM9_BEhSm-smLgqQHUrLtXldCz-JpDOkckA"];
109109

110110
[[NSNotificationCenter defaultCenter]
111111
postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification
@@ -120,7 +120,7 @@ - (void)testAuthenticateSuccessWithCode
120120

121121
- (void)testAuthenticateSuccessWithToken
122122
{
123-
NSArray *args = @[@"someRandomClientId", @"token", @[@"streaming"]];
123+
NSArray *args = @[@"test-url-scheme", @"someRandomClientId", @"token", @[@"streaming"]];
124124

125125
__block BOOL responseArrived = NO;
126126

@@ -142,7 +142,7 @@ - (void)testAuthenticateSuccessWithToken
142142
double delayInSeconds = 0.005;
143143
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
144144
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
145-
NSURL *callbackURL = [NSURL URLWithString:@"spotify-ios-sdk-beta://callback?access_token=NQpvC5h6MnausBFRG2hJjXifw2CZrXzQIh4S_SgBfpcVi6svpZKXpwYyoLRYhWN8g4L-zoZqYK0hfFNFgMqTpESGvodAuXGngZFiKc16y7oeMRJTZaY3&token_type=Bearer&expires_in=3600"];
145+
NSURL *callbackURL = [NSURL URLWithString:@"test-url-scheme://callback?access_token=NQpvC5h6MnausBFRG2hJjXifw2CZrXzQIh4S_SgBfpcVi6svpZKXpwYyoLRYhWN8g4L-zoZqYK0hfFNFgMqTpESGvodAuXGngZFiKc16y7oeMRJTZaY3&token_type=Bearer&expires_in=3600"];
146146

147147
[[NSNotificationCenter defaultCenter]
148148
postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification
@@ -159,7 +159,7 @@ - (void)testAuthenticateInvalidResponseType
159159
{
160160
__block BOOL responseArrived = NO;
161161

162-
[plugin authenticate:[self createTestURLCommand:@[@"spotify-ios-sdk-beta", @"bla", @[@"streaming"]]]];
162+
[plugin authenticate:[self createTestURLCommand:@[@"test-url-scheme", @"aClientId", @"bla", @[@"streaming"]]]];
163163

164164
[commandDelegate mockPluginResult:^(CDVPluginResult *result, NSString *callbackId) {
165165
XCTAssertEqual(result.status.intValue, CDVCommandStatus_ERROR, @"Command status should be ERROR");
@@ -184,7 +184,7 @@ - (void)testAuthenticateFailed
184184
return [OHHTTPStubsResponse responseWithError: errorForTesting()];
185185
}];
186186

187-
[plugin authenticate:[self createTestURLCommand:@[@"spotify-ios-sdk-beta", @"code", @"http://foo.bar:1234/swap", @[@"streaming"]]]];
187+
[plugin authenticate:[self createTestURLCommand:@[@"test-url-scheme", @"clientId", @"code", @"http://foo.bar:1234/swap", @[@"streaming"]]]];
188188

189189
[commandDelegate mockPluginResult:^(CDVPluginResult *result, NSString *callbackId) {
190190
XCTAssertEqual(result.status.intValue, CDVCommandStatus_ERROR, @"Command status should be ERROR");
@@ -194,7 +194,7 @@ - (void)testAuthenticateFailed
194194
double delayInSeconds = 0.005;
195195
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
196196
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
197-
NSURL *callbackURL = [NSURL URLWithString:@"spotify-ios-sdk-beta://callback?error=access_denied"];
197+
NSURL *callbackURL = [NSURL URLWithString:@"test-url-scheme://callback?error=access_denied"];
198198

199199
[[NSNotificationCenter defaultCenter]
200200
postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification

test/js/spec/spotify/auth.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ describe('auth', function() {
3131

3232
describe('without scope', function() {
3333
beforeEach(function() {
34-
spotify.authenticate('aRandomClientId1234', 'http://tok.en', this.callback);
34+
spotify.authenticate('test-scheme', 'aRandomClientId1234', 'code', 'http://tok.en', this.callback);
3535
});
3636

3737
it('should send the "streaming" scope to native', function() {
3838
expect(this.onRequest).to.have.been.calledWith([
39-
'SpotifyPlugin', 'authenticate', ['aRandomClientId1234', 'http://tok.en', ['streaming']]
39+
'SpotifyPlugin', 'authenticate', ['test-scheme', 'aRandomClientId1234', 'code', 'http://tok.en', ['streaming']]
4040
]);
4141
});
4242

@@ -47,12 +47,12 @@ describe('auth', function() {
4747

4848
describe('with scope', function() {
4949
beforeEach(function() {
50-
spotify.authenticate('aRandomClientId1234', 'http://tok.en', ['somescope'], this.callback);
50+
spotify.authenticate('test-scheme', 'aRandomClientId1234', 'code', 'http://tok.en', ['somescope'], this.callback);
5151
});
5252

5353
it('should send the "somescope" scope to native', function() {
5454
expect(this.onRequest).to.have.been.calledWith([
55-
'SpotifyPlugin', 'authenticate', ['aRandomClientId1234', 'http://tok.en', ['somescope']]
55+
'SpotifyPlugin', 'authenticate', ['test-scheme', 'aRandomClientId1234', 'code', 'http://tok.en', ['somescope']]
5656
]);
5757
});
5858

@@ -65,7 +65,7 @@ describe('auth', function() {
6565
describe('failed authentication', function() {
6666
beforeEach(function() {
6767
mockExec(9, 'Login to Spotify failed because of invalid credentials.', this.onRequest);
68-
spotify.authenticate('aRandomClientId1234', 'http://tok.en', this.callback);
68+
spotify.authenticate('test-scheme', 'aRandomClientId1234', 'code', 'http://tok.en', this.callback);
6969
});
7070

7171
it('should send back "Login to Spotify failed because of invalid credentials."', function() {

www/spotify.js

-8
This file was deleted.

www/spotify/auth.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var utils = require('./utils')
44

55
var auth = exports;
66

7-
auth.authenticate = function(clientId, tokenExchangeURL, scopes, callback) {
7+
auth.authenticate = function(urlScheme, clientId, responseType, tokenExchangeURL, scopes, callback) {
88
if (callback === undefined) {
99
if (scopes === undefined) {
1010
callback = tokenExchangeURL;
@@ -16,7 +16,14 @@ auth.authenticate = function(clientId, tokenExchangeURL, scopes, callback) {
1616
scopes = ['streaming'];
1717
}
1818

19-
exec('authenticate', clientId, tokenExchangeURL, scopes, callback);
19+
args = ['authenticate', urlScheme, clientId, responseType];
20+
21+
if(responseType == 'code') args.push(tokenExchangeURL);
22+
23+
args.push(scopes);
24+
args.push(callback);
25+
26+
exec.apply(this, args);
2027
};
2128

2229
auth.renewSession = function(session, tokenRefreshURL, forceRenew, callback) {

0 commit comments

Comments
 (0)