Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add requestToken to get token for apple music api #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,42 @@ appleMusic.init(successFunction, failureFunction)
```
Run `init()` to listen to native events.

#### Request Authorization
#### Request Authorization
```js
appleMusic.requestAuthorization(function(isAuthorized){}, failureFunction)
appleMusic.requestAuthorization(function(isAuthorized){}, failureFunction)
```

#### Request Token

- The `DEVELOPER_TOKEN` is generated from [Create a MusicKit identifier and private key](https://help.apple.com/developer-account/#/devce5522674) | [中文版本](https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E7%94%9F%E6%88%90-musickit-app-%E9%9C%80%E8%A6%81%E7%9A%84-developer-token-1e4195f517e0)

```javascript
appleMusic.requestAuthorization(function(isAuthorized){
if (isAuthorized) {
appleMusic.requestToken(`${DEVELOPER_TOKEN}`, function(token) {}, failureFunction)
}
}, failureFunction)
```

#### Get Play Lists

```js
appleMusic.getPlayLists(function(playLists){}, failureFunction)
```
```
Return: playLists is an array with each index containing id, name fields;

#### Get Songs
```js
appleMusic.getSongs(playListId, function(songList){}, failureFunction)
```
Return: songList is an array with each index containing id, name fields;

#### Create Play List with Songs
```js
appleMusic.createPlayList(playListName, trackIds, function(status){}, failureFunction)
```
return: status success on play list creation.

#### Add Single Song to Playlist
```js
appleMusic.addSongstoPlayList(playListId, trackId, function(status){}, failureFunction)
Expand Down
1 change: 1 addition & 0 deletions src/ios/CordovaAppleMusic.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- (void) addSongtoPlayList:(CDVInvokedUrlCommand*)command;
- (void) createPlayList:(CDVInvokedUrlCommand*)command;
- (void) getSongs:(CDVInvokedUrlCommand*)command;
- (void) requestToken:(CDVInvokedUrlCommand*)command;
- (void) requestAuthorization:(CDVInvokedUrlCommand*)command;
- (void) resume:(CDVInvokedUrlCommand*)command;
- (void) seek:(CDVInvokedUrlCommand*)command;
Expand Down
47 changes: 39 additions & 8 deletions src/ios/CordovaAppleMusic.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,67 @@ @implementation CordovaAppleMusic
- (void)init:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = [command callbackId];

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(handlePlaybackStateChanged:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:[MPMusicPlayerController systemMusicPlayer]];

[[MPMusicPlayerController systemMusicPlayer] beginGeneratingPlaybackNotifications];

CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}

- (void)getStatus:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = [command callbackId];

SKCloudServiceAuthorizationStatus status = [SKCloudServiceController authorizationStatus];

int res = -1;
CDVCommandStatus callbackStatus = CDVCommandStatus_OK;

switch (status){
case SKCloudServiceAuthorizationStatusNotDetermined: res = 0; break;
case SKCloudServiceAuthorizationStatusDenied: res = 1; break;
case SKCloudServiceAuthorizationStatusAuthorized: res = 2; break;
case SKCloudServiceAuthorizationStatusRestricted: res = 3; break;
default: callbackStatus = CDVCommandStatus_ERROR;
}

CDVPluginResult* result = [CDVPluginResult resultWithStatus:callbackStatus messageAsInt:res];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}

- (void)requestToken:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = [command callbackId];
NSString* developerToken = [[command arguments] objectAtIndex:0];
SKCloudServiceController *serviceController = [[SKCloudServiceController alloc] init];
if (@available(iOS 11.0, *)) {
[serviceController requestUserTokenForDeveloperToken:developerToken completionHandler:^(NSString * _Nullable userToken, NSError * _Nullable error) {
if (error != nil) {
NSLog(@"userToken_Error :%@", error);
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
else{
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:userToken];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
}];
} else {
[serviceController requestPersonalizationTokenForClientToken:developerToken withCompletionHandler:^(NSString * _Nullable personalizationToken, NSError * _Nullable error) {
if (error != nil) {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
} else {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:personalizationToken];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
}];
}
}

- (void)requestAuthorization:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = [command callbackId];
Expand All @@ -53,6 +83,7 @@ - (void)requestAuthorization:(CDVInvokedUrlCommand*)command
}];
}];
}

- (void)playTrack:(CDVInvokedUrlCommand*)command
{
NSString* callbackId = [command callbackId];
Expand Down Expand Up @@ -416,4 +447,4 @@ - (void)handlePlaybackStateChanged:(NSNotification*)notification
[self.commandDelegate evalJs:@"window.appleMusicPluginPlaying()"];
}
}
@end
@end
3 changes: 3 additions & 0 deletions www/appleMusic.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ module.exports = {
playTrack: function (trackId, successCallback, errorCallback) {
exec(successCallback, errorCallback, "AppleMusic", "playTrack", [trackId]);
},
requestToken: function (developerToken, successCallback, errorCallback) {
exec(successCallback, errorCallback, "AppleMusic", "requestToken", [developerToken]);
},
requestAuthorization: function (successCallback, errorCallback) {
exec(successCallback, errorCallback, "AppleMusic", "requestAuthorization", []);
},
Expand Down