Skip to content

Commit 995a85b

Browse files
committed
Simplify some code
1 parent 93b67c9 commit 995a85b

File tree

1 file changed

+45
-128
lines changed

1 file changed

+45
-128
lines changed

AudioShareSDK.m

Lines changed: 45 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ + (AudioShare*) sharedInstance {
1616
return a;
1717
}
1818

19+
+ (void)showAlertController:(UIAlertController*)alertController {
20+
UIViewController *presentingViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
21+
22+
while(presentingViewController.presentedViewController != nil) {
23+
presentingViewController = presentingViewController.presentedViewController;
24+
}
25+
26+
[presentingViewController presentViewController:alertController animated:YES completion:nil];
27+
}
28+
29+
+ (void)openURL:(NSURL *)URL {
30+
UIApplication *application = [UIApplication sharedApplication];
31+
32+
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
33+
[application openURL:URL options:@{}
34+
completionHandler:nil];
35+
} else {
36+
[application openURL:URL];
37+
}
38+
}
39+
1940
- (NSString*)escapeString:(NSString*)string {
2041
NSString *s = [string stringByAddingPercentEncodingWithAllowedCharacters:
2142
[NSCharacterSet URLQueryAllowedCharacterSet]];
@@ -32,23 +53,9 @@ - (BOOL)addSoundFromData:(NSData*)data withName:(NSString*)name {
3253

3354
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action")
3455
style:UIAlertActionStyleDefault
35-
handler:^(UIAlertAction *action)
36-
{
37-
NSLog(@"OK action");
38-
}];
39-
56+
handler:^(UIAlertAction *action){}];
4057
[alertController addAction:okAction];
41-
42-
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
43-
if([rootViewController isKindOfClass:[UINavigationController class]])
44-
{
45-
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
46-
}
47-
if([rootViewController isKindOfClass:[UITabBarController class]])
48-
{
49-
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
50-
}
51-
[rootViewController presentViewController:alertController animated:YES completion:nil];
58+
[AudioShare showAlertController:alertController];
5259

5360
return NO;
5461
}
@@ -75,36 +82,24 @@ - (BOOL)addSoundFromData:(NSData*)data withName:(NSString*)name {
7582

7683
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
7784
style:UIAlertActionStyleCancel
78-
handler:^(UIAlertAction *action)
79-
{
80-
NSLog(@"Cancel action");
81-
}];
85+
handler:^(UIAlertAction *action){}];
8286

8387
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action")
8488
style:UIAlertActionStyleDefault
8589
handler:^(UIAlertAction *action)
8690
{
87-
NSLog(@"OK action");
88-
[self openScheme:@"http://kymatica.com/audioshare/download.php"];
91+
[AudioShare openURL:[NSURL URLWithString:@"http://kymatica.com/audioshare/download.php"]];
8992
}];
9093

9194
[alertController addAction:cancelAction];
9295
[alertController addAction:okAction];
9396

94-
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
95-
if([rootViewController isKindOfClass:[UINavigationController class]])
96-
{
97-
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
98-
}
99-
if([rootViewController isKindOfClass:[UITabBarController class]])
100-
{
101-
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
102-
}
103-
[rootViewController presentViewController:alertController animated:YES completion:nil];
97+
[AudioShare showAlertController:alertController];
10498

10599
return NO;
106100
}
107-
return [[UIApplication sharedApplication] openURL:asURL];
101+
[AudioShare openURL:asURL];
102+
return YES;
108103
}
109104

110105
- (BOOL)addSoundFromURL:(NSURL*)url withName:(NSString*)name {
@@ -121,24 +116,12 @@ - (BOOL)addSoundFromPath:(NSString*)path withName:(NSString*)name {
121116

122117
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action")
123118
style:UIAlertActionStyleDefault
124-
handler:^(UIAlertAction *action)
125-
{
126-
NSLog(@"OK action");
127-
}];
119+
handler:^(UIAlertAction *action){}];
128120

129121
[alertController addAction:okAction];
130122

131-
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
132-
if([rootViewController isKindOfClass:[UINavigationController class]])
133-
{
134-
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
135-
}
136-
if([rootViewController isKindOfClass:[UITabBarController class]])
137-
{
138-
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
139-
}
140-
[rootViewController presentViewController:alertController animated:YES completion:nil];
141-
123+
[AudioShare showAlertController:alertController];
124+
142125
return NO;
143126
}
144127
NSData *dataFile = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:path] options:NSDataReadingMappedIfSafe error:NULL];
@@ -175,24 +158,12 @@ - (BOOL)initiateSoundImport {
175158

176159
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action")
177160
style:UIAlertActionStyleDefault
178-
handler:^(UIAlertAction *action)
179-
{
180-
NSLog(@"OK action");
181-
}];
161+
handler:^(UIAlertAction *action){}];
182162

183163
[alertController addAction:okAction];
184164

185-
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
186-
if([rootViewController isKindOfClass:[UINavigationController class]])
187-
{
188-
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
189-
}
190-
if([rootViewController isKindOfClass:[UITabBarController class]])
191-
{
192-
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
193-
}
194-
[rootViewController presentViewController:alertController animated:YES completion:nil];
195-
165+
[AudioShare showAlertController:alertController];
166+
196167
return NO;
197168
}
198169
NSURL *asURL = [NSURL URLWithString:[NSString stringWithFormat:@"audioshare.import://%@",callback]];
@@ -208,41 +179,27 @@ - (BOOL)initiateSoundImport {
208179

209180
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
210181
style:UIAlertActionStyleCancel
211-
handler:^(UIAlertAction *action)
212-
{
213-
NSLog(@"Cancel action");
214-
}];
182+
handler:^(UIAlertAction *action){}];
215183

216184
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action")
217185
style:UIAlertActionStyleDefault
218186
handler:^(UIAlertAction *action)
219187
{
220-
NSLog(@"OK action");
221-
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://kymatica.com/audioshare/download.php"]];
222-
[self openScheme:@"http://kymatica.com/audioshare/download.php"];
188+
[AudioShare openURL:[NSURL URLWithString:@"http://kymatica.com/audioshare/download.php"]];
223189
}];
224190

225191
[alertController addAction:cancelAction];
226192
[alertController addAction:okAction];
227-
228-
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
229-
if([rootViewController isKindOfClass:[UINavigationController class]])
230-
{
231-
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
232-
}
233-
if([rootViewController isKindOfClass:[UITabBarController class]])
234-
{
235-
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
236-
}
237-
[rootViewController presentViewController:alertController animated:YES completion:nil];
193+
194+
[AudioShare showAlertController:alertController];
238195

239196
if(hasAudio)
240197
asURL = [NSURL URLWithString:[callback stringByAppendingString:@"://PastedAudio"]];
241198
else
242199
return NO;
243200
}
244-
245-
return [[UIApplication sharedApplication] openURL:asURL];
201+
[AudioShare openURL:asURL];
202+
return YES;
246203
}
247204

248205
- (NSString*)writeSoundImport:(NSString*)filename {
@@ -294,24 +251,12 @@ - (BOOL)checkPendingImport:(NSURL *)url withBlock:(AudioShareImportBlock)block {
294251

295252
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action")
296253
style:UIAlertActionStyleDefault
297-
handler:^(UIAlertAction *action)
298-
{
299-
NSLog(@"OK action");
300-
}];
254+
handler:^(UIAlertAction *action){}];
301255

302256
[alertController addAction:okAction];
303257

304-
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
305-
if([rootViewController isKindOfClass:[UINavigationController class]])
306-
{
307-
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
308-
}
309-
if([rootViewController isKindOfClass:[UITabBarController class]])
310-
{
311-
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
312-
}
313-
[rootViewController presentViewController:alertController animated:YES completion:nil];
314-
258+
[AudioShare showAlertController:alertController];
259+
315260
return NO;
316261
}
317262
block(path);
@@ -337,24 +282,11 @@ + (void)appDidLaunch {
337282

338283
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action")
339284
style:UIAlertActionStyleDefault
340-
handler:^(UIAlertAction *action)
341-
{
342-
NSLog(@"OK action");
343-
}];
285+
handler:^(UIAlertAction *action){}];
344286

345287
[alertController addAction:okAction];
346288

347-
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
348-
if([rootViewController isKindOfClass:[UINavigationController class]])
349-
{
350-
rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
351-
}
352-
if([rootViewController isKindOfClass:[UITabBarController class]])
353-
{
354-
rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
355-
}
356-
[rootViewController presentViewController:alertController animated:YES completion:nil];
357-
289+
[AudioShare showAlertController:alertController];
358290
}
359291
}
360292

@@ -405,19 +337,4 @@ + (NSString*)findFileType:(NSString*)path {
405337
}
406338
}
407339

408-
- (void)openScheme:(NSString *)scheme {
409-
UIApplication *application = [UIApplication sharedApplication];
410-
NSURL *URL = [NSURL URLWithString:scheme];
411-
412-
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
413-
[application openURL:URL options:@{}
414-
completionHandler:^(BOOL success) {
415-
NSLog(@"Open %@: %d",scheme,success);
416-
}];
417-
} else {
418-
BOOL success = [application openURL:URL];
419-
NSLog(@"Open %@: %d",scheme,success);
420-
}
421-
}
422-
423340
@end

0 commit comments

Comments
 (0)