-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMXMLyricsAction.m
268 lines (203 loc) · 9.2 KB
/
MXMLyricsAction.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// MusixmatchExtension.m
// MXMLyricsActionDemo
//
// Created by Martino Bonfiglioli on 16/10/14.
// Copyright (c) 2014 Musixmatch. All rights reserved.
//
#import "MXMLyricsAction.h"
#import <StoreKit/StoreKit.h>
// Empty string check macro
#ifndef StringOrEmpty
#define StringOrEmpty(A) ({ __typeof__(A) __a = (A); __a ? __a : @""; })
#endif
// Available App Extension Actions
NSString *const kUTTypeAppExtensionFindSongLyric = @"org.appextension.find-song-lyric";
// Song Dictionary keys
NSString *const MusixmatchExtensionTitle = @"title";
NSString *const MusixmatchExtensionArtist = @"artist";
NSString *const MusixmatchExtensionAlbum = @"album";
NSString *const MusixmatchExtensionCoverArt = @"coverArt";
NSString *const MusixmatchExtensionDuration = @"duration";
NSString *const MusixmatchExtensionProgress = @"progress";
NSString *const MusixmatchExtensionStartDate = @"startDate";
// View Style
NSString *const MusixmatchExtensionStatusBarStyle = @"statusBar";
// Additional Infos
NSString *const MusixmatchExtensionHostBundleID = @"hostBundle";
NSString *musixmatchAppStoreURL = @"itms-apps://itunes.apple.com/app/id448278467";
NSString *musixmatchAppStoreAppID = @"448278467";
@interface MXMLyricsAction () < SKStoreProductViewControllerDelegate, UIAlertViewDelegate >
@property (nonatomic, strong) UIViewController *hostViewController;
@end
@implementation MXMLyricsAction
@synthesize hostViewController;
@synthesize nativeAppStoreView;
@synthesize showAlertBeforeAppStore;
#pragma mark - Singleton
+ (MXMLyricsAction*)sharedExtension {
static dispatch_once_t onceToken;
static MXMLyricsAction *__sharedExtension;
dispatch_once(&onceToken, ^{
__sharedExtension = [MXMLyricsAction new];
});
return __sharedExtension;
}
#pragma mark - Init
- (id)init {
self = [super init];
if (self) {
nativeAppStoreView = YES;
showAlertBeforeAppStore = YES;
}
return self;
}
#pragma mark - Util
- (BOOL)isSystemAppExtensionAPIAvailable {
#ifdef __IPHONE_8_0
return NSClassFromString(@"NSExtensionItem") != nil;
#else
return NO;
#endif
}
- (BOOL)isMusixmatchExtensionAvailable {
#ifdef __IPHONE_8_0
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mxm-ext:"]];
#else
return NO;
#endif
}
- (void)openAppStore {
if (nativeAppStoreView && self.hostViewController) {
[self showAppStoreForMusixmatchFromViewController:self.hostViewController];
}else {
[self openStoreWithOpenURL];
}
}
- (void)openStoreWithOpenURL {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:musixmatchAppStoreURL]];
}
- (void)showAppStoreForMusixmatchFromViewController:(UIViewController*)controller {
//Use the in-app StoreKit view if available (iOS 6) and imported. This works in the simulator.
if (NSStringFromClass([SKStoreProductViewController class]) != nil) {
SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init];
NSNumber *appId = [NSNumber numberWithInteger:musixmatchAppStoreAppID.integerValue];
[storeViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:appId} completionBlock:nil];
storeViewController.delegate = self;
[controller presentViewController:storeViewController animated:YES completion:nil];
}
}
#pragma mark - Lyrics action
- (void)findLyricsForSongWithTitle:(NSString*)title
artist:(NSString*)artist
album:(NSString*)album
artWork:(UIImage*)image
currentProgress:(NSTimeInterval)progress
trackDuration:(NSTimeInterval)duration
forViewController:(UIViewController *)viewController
sender:(id)sender
competionHandler:(void (^)(NSError *error))completion {
NSMutableDictionary *dict = [NSMutableDictionary new];
[dict setObject:StringOrEmpty(title)
forKey:MusixmatchExtensionTitle];
[dict setObject:StringOrEmpty(artist)
forKey:MusixmatchExtensionArtist];
[dict setObject:StringOrEmpty(album)
forKey:MusixmatchExtensionAlbum];
if (image) {
[dict setObject:image
forKey:MusixmatchExtensionCoverArt];
}
[dict setObject:[NSString stringWithFormat:@"%f", progress]
forKey:MusixmatchExtensionProgress];
[dict setObject:[NSString stringWithFormat:@"%f", duration]
forKey:MusixmatchExtensionDuration];
[dict setObject:[NSDate date]
forKey:MusixmatchExtensionStartDate];
[dict setObject:[NSNumber numberWithInt:[[UIApplication sharedApplication] statusBarStyle]]
forKey:MusixmatchExtensionStatusBarStyle];
[dict setObject:[[NSBundle mainBundle] bundleIdentifier]
forKey:MusixmatchExtensionHostBundleID];
[self findLyricsForSong:dict
forViewController:viewController
sender:sender
competionHandler:completion];
}
- (void)findLyricsForSong:(NSDictionary*)dict
forViewController:(UIViewController *)viewController
sender:(id)sender
competionHandler:(void (^)(NSError *error))completion
{
NSAssert(dict != nil, @"dict must not be nil");
NSAssert(viewController != nil, @"viewController must not be nil");
if (![self isSystemAppExtensionAPIAvailable]) {
NSLog(@"Failed to findLoginForURLString, system API is not available");
if (completion) {
completion(nil);
}
return;
}
if (![self isMusixmatchExtensionAvailable]) {
if (completion) {
completion(nil);
}
[self setHostViewController:viewController];
if (showAlertBeforeAppStore) {
UIAlertController *storeAlert = [UIAlertController alertControllerWithTitle:@"Musixmatch app is required"
message:@"To read the lyrics you need to install Musixmatch from the AppStore."
preferredStyle:UIAlertControllerStyleAlert];
[storeAlert addAction:[UIAlertAction actionWithTitle:@"No, thanks"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
}]];
[storeAlert addAction:[UIAlertAction actionWithTitle:@"Get the app"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[self openAppStore];
}]];
[viewController presentViewController:storeAlert
animated:YES
completion:nil];
}else {
[self openAppStore];
}
return;
}
#ifdef __IPHONE_8_0
UIActivityViewController *activityViewController = [self activityViewControllerForItem:dict viewController:viewController sender:sender typeIdentifier:kUTTypeAppExtensionFindSongLyric];
activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (completion) {
completion(nil);
}
[self setHostViewController:nil];
};
[viewController presentViewController:activityViewController animated:YES completion:^{
}];
#endif
}
- (UIActivityViewController *)activityViewControllerForItem:(NSDictionary *)item viewController:(UIViewController*)viewController sender:(id)sender typeIdentifier:(NSString *)typeIdentifier {
#ifdef __IPHONE_8_0
NSItemProvider *itemProvider = [[NSItemProvider alloc] initWithItem:item typeIdentifier:typeIdentifier];
NSExtensionItem *extensionItem = [[NSExtensionItem alloc] init];
extensionItem.attachments = @[ itemProvider ];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[ extensionItem ] applicationActivities:nil];
if ([sender isKindOfClass:[UIBarButtonItem class]]) {
controller.popoverPresentationController.barButtonItem = sender;
}
else if ([sender isKindOfClass:[UIView class]]) {
controller.popoverPresentationController.sourceView = [sender superview];
controller.popoverPresentationController.sourceRect = [sender frame];
}
else {
NSLog(@"sender can be nil on iPhone");
}
return controller;
#else
return nil;
#endif
}
#pragma mark - SKStoreProductViewController Delegate
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[viewController dismissViewControllerAnimated:YES completion:nil];
}
@end