-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathMFSRootViewController.m
More file actions
394 lines (366 loc) · 14.5 KB
/
Copy pathMFSRootViewController.m
File metadata and controls
394 lines (366 loc) · 14.5 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#import "MFSRootViewController.h"
#import "MFSVersionPickerViewController.h"
#import "CoreServices.h"
#import <SystemConfiguration/SystemConfiguration.h>
@interface SKUIItemStateCenter : NSObject
+ (id)defaultCenter;
- (id)_newPurchasesWithItems:(id)items;
- (void)_performPurchases:(id)purchases hasBundlePurchase:(_Bool)purchase withClientContext:(id)context completionBlock:(id /* block */)block;
- (void)_performSoftwarePurchases:(id)purchases withClientContext:(id)context completionBlock:(id /* block */)block;
@end
@interface SKUIItem : NSObject
- (id)initWithLookupDictionary:(id)dictionary;
@end
@interface SKUIItemOffer : NSObject
- (id)initWithLookupDictionary:(id)dictionary;
@end
@interface SKUIClientContext : NSObject
+ (id)defaultContext;
@end
@interface MFSRootViewController ()
@property (nonatomic, strong) UIAlertController* progressAlert;
@end
@implementation MFSRootViewController
- (void)loadView
{
[super loadView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadSpecifiers) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (NSMutableArray*)specifiers
{
if (!_specifiers)
{
_specifiers = [NSMutableArray new];
PSSpecifier* downloadGroupSpecifier = [PSSpecifier emptyGroupSpecifier];
downloadGroupSpecifier.name = @"Download";
[_specifiers addObject:downloadGroupSpecifier];
PSSpecifier* downloadSpecifier = [PSSpecifier preferenceSpecifierNamed:@"Download" target:self set:nil get:nil detail:nil cell:PSButtonCell edit:nil];
downloadSpecifier.identifier = @"download";
[downloadSpecifier setProperty:@YES forKey:@"enabled"];
downloadSpecifier.buttonAction = @selector(downloadApp);
[_specifiers addObject:downloadSpecifier];
NSString* aboutText = [self getAboutText];
[downloadGroupSpecifier setProperty:aboutText forKey:@"footerText"];
PSSpecifier* installedGroupSpecifier = [PSSpecifier emptyGroupSpecifier];
installedGroupSpecifier.name = @"Installed Apps";
[_specifiers addObject:installedGroupSpecifier];
NSMutableArray* appSpecifiers = [NSMutableArray new];
[[LSApplicationWorkspace defaultWorkspace] enumerateApplicationsOfType:0 block:^(LSApplicationProxy* appProxy)
{
PSSpecifier* appSpecifier = [PSSpecifier preferenceSpecifierNamed:appProxy.localizedName target:self set:nil get:nil detail:nil cell:PSButtonCell edit:nil];
[appSpecifier setProperty:appProxy.bundleURL forKey:@"bundleURL"];
[appSpecifier setProperty:@YES forKey:@"enabled"];
appSpecifier.buttonAction = @selector(downloadAppShortcut:);
[appSpecifiers addObject:appSpecifier];
}];
[appSpecifiers sortUsingComparator:^NSComparisonResult(PSSpecifier* a, PSSpecifier* b)
{
return [a.name compare:b.name];
}];
[_specifiers addObjectsFromArray:appSpecifiers];
}
[(UINavigationItem*)self.navigationItem setTitle:@"MuffinStore"];
return _specifiers;
}
- (BOOL)isNetworkReachable
{
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, "apis.bilin.eu.org");
SCNetworkReachabilityFlags flags;
BOOL reachable = NO;
if (SCNetworkReachabilityGetFlags(reachability, &flags))
{
BOOL isReachable = (flags & kSCNetworkFlagsReachable) != 0;
BOOL needsConnection = (flags & kSCNetworkFlagsConnectionRequired) != 0;
reachable = isReachable && !needsConnection;
}
CFRelease(reachability);
return reachable;
}
- (void)downloadAppShortcut:(PSSpecifier*)specifier
{
if (![self isNetworkReachable])
{
[self showAlert:@"No Internet" message:@"Please check your internet connection and try again."];
return;
}
NSURL* bundleURL = [specifier propertyForKey:@"bundleURL"];
NSDictionary* infoPlist = [NSDictionary dictionaryWithContentsOfFile:[bundleURL.path stringByAppendingPathComponent:@"Info.plist"]];
NSString* bundleId = infoPlist[@"CFBundleIdentifier"];
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/lookup?bundleId=%@&limit=1&media=software", bundleId]];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask* task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
if (error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self showAlert:@"Error" message:error.localizedDescription];
});
return;
}
NSError* jsonError = nil;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self showAlert:@"JSON Error" message:jsonError.localizedDescription];
});
return;
}
NSArray* results = json[@"results"];
if (results.count == 0)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self showAlert:@"Error" message:@"No results found for this app."];
});
return;
}
NSDictionary* app = results[0];
[self getAllAppVersionIdsAndPrompt:[app[@"trackId"] longLongValue]];
}];
[task resume];
}
- (NSString*)getAboutText
{
return @"MuffinStore v1.3\nMade by Mineek\nApp Icon designed by Kate\nhttps://github.com/mineek/MuffinStore";
}
- (void)showAlert:(NSString*)title message:(NSString*)message
{
dispatch_async(dispatch_get_main_queue(), ^
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
});
}
- (void)showDownloadProgressWithMessage:(NSString*)message
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (self.progressAlert)
{
self.progressAlert.message = message;
return;
}
self.progressAlert = [UIAlertController alertControllerWithTitle:@"Downloading" message:message preferredStyle:UIAlertControllerStyleAlert];
UIActivityIndicatorView* indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
indicator.translatesAutoresizingMaskIntoConstraints = NO;
[indicator startAnimating];
[self.progressAlert.view addSubview:indicator];
[NSLayoutConstraint activateConstraints:@[
[indicator.centerXAnchor constraintEqualToAnchor:self.progressAlert.view.centerXAnchor],
[indicator.bottomAnchor constraintEqualToAnchor:self.progressAlert.view.bottomAnchor constant:-20]
]];
[self presentViewController:self.progressAlert animated:YES completion:nil];
});
}
- (void)dismissDownloadProgress
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (self.progressAlert)
{
[self.progressAlert dismissViewControllerAnimated:YES completion:nil];
self.progressAlert = nil;
}
});
}
- (void)getAllAppVersionIdsFromServer:(long long)appId
{
if (![self isNetworkReachable])
{
[self showAlert:@"No Internet" message:@"Please check your internet connection and try again."];
return;
}
NSString* serverURL = @"https://apis.bilin.eu.org/history/";
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%lld", serverURL, appId]];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask* task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
if (error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self showAlert:@"Error" message:error.localizedDescription];
});
return;
}
NSError* jsonError = nil;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self showAlert:@"JSON Error" message:jsonError.debugDescription];
});
return;
}
NSArray* versionIds = json[@"data"];
if (versionIds.count == 0)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self showAlert:@"Error" message:@"No version IDs found. The server may not have records for this app."];
});
return;
}
dispatch_async(dispatch_get_main_queue(), ^
{
MFSVersionPickerViewController* picker = [[MFSVersionPickerViewController alloc] initWithVersions:versionIds completion:^(NSDictionary* selectedVersion)
{
[self downloadAppWithAppId:appId versionId:[selectedVersion[@"external_identifier"] longLongValue]];
}];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:picker];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
if (@available(iOS 15.0, *))
{
id sheet = [nav performSelector:@selector(sheetPresentationController)];
Class detentClass = NSClassFromString(@"UISheetPresentationControllerDetent");
if (sheet && detentClass)
{
id medium = [detentClass performSelector:@selector(mediumDetent)];
id large = [detentClass performSelector:@selector(largeDetent)];
if (medium && large)
{
[sheet setValue:@[medium, large] forKey:@"detents"];
[sheet setValue:@YES forKey:@"prefersGrabberVisible"];
}
}
}
[self presentViewController:nav animated:YES completion:nil];
});
}];
[task resume];
}
- (void)promptForVersionId:(long long)appId
{
dispatch_async(dispatch_get_main_queue(), ^
{
UIAlertController* versionAlert = [UIAlertController alertControllerWithTitle:@"Version ID" message:@"Enter the version ID of the app you want to download" preferredStyle:UIAlertControllerStyleAlert];
[versionAlert addTextFieldWithConfigurationHandler:^(UITextField* textField)
{
textField.placeholder = @"Version ID";
textField.keyboardType = UIKeyboardTypeNumberPad;
}];
UIAlertAction* downloadAction = [UIAlertAction actionWithTitle:@"Download" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{
long long versionId = [versionAlert.textFields.firstObject.text longLongValue];
[self downloadAppWithAppId:appId versionId:versionId];
}];
[versionAlert addAction:downloadAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[versionAlert addAction:cancelAction];
[self presentViewController:versionAlert animated:YES completion:nil];
});
}
- (void)getAllAppVersionIdsAndPrompt:(long long)appId
{
dispatch_async(dispatch_get_main_queue(), ^
{
UIAlertController* promptAlert = [UIAlertController alertControllerWithTitle:@"Version Selection" message:@"Choose how to select the app version to download." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* serverAction = [UIAlertAction actionWithTitle:@"Browse Version List" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{
[self getAllAppVersionIdsFromServer:appId];
}];
[promptAlert addAction:serverAction];
UIAlertAction* manualAction = [UIAlertAction actionWithTitle:@"Enter Version ID Manually" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{
[self promptForVersionId:appId];
}];
[promptAlert addAction:manualAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[promptAlert addAction:cancelAction];
[self presentViewController:promptAlert animated:YES completion:nil];
});
}
- (void)downloadAppWithAppId:(long long)appId versionId:(long long)versionId
{
if (![self isNetworkReachable])
{
[self showAlert:@"No Internet" message:@"Please check your internet connection and try again."];
return;
}
[self showDownloadProgressWithMessage:@"Initiating download…"];
NSString* adamId = [NSString stringWithFormat:@"%lld", appId];
NSString* pricingParameters = @"pricingParameter";
NSString* appExtVrsId = [NSString stringWithFormat:@"%lld", versionId];
NSString* installed = @"0";
NSString* offerString = nil;
if (versionId == 0)
{
offerString = [NSString stringWithFormat:@"productType=C&price=0&salableAdamId=%@&pricingParameters=%@&clientBuyId=1&installed=%@&trolled=1", adamId, pricingParameters, installed];
}
else
{
offerString = [NSString stringWithFormat:@"productType=C&price=0&salableAdamId=%@&pricingParameters=%@&appExtVrsId=%@&clientBuyId=1&installed=%@&trolled=1", adamId, pricingParameters, appExtVrsId, installed];
}
NSDictionary* offerDict = @{@"buyParams": offerString};
NSDictionary* itemDict = @{@"_itemOffer": adamId};
SKUIItemOffer* offer = [[SKUIItemOffer alloc] initWithLookupDictionary:offerDict];
SKUIItem* item = [[SKUIItem alloc] initWithLookupDictionary:itemDict];
[item setValue:offer forKey:@"_itemOffer"];
[item setValue:@"iosSoftware" forKey:@"_itemKindString"];
if (versionId != 0)
{
[item setValue:@(versionId) forKey:@"_versionIdentifier"];
}
SKUIItemStateCenter* center = [SKUIItemStateCenter defaultCenter];
NSArray* items = @[item];
dispatch_async(dispatch_get_main_queue(), ^
{
[self showDownloadProgressWithMessage:@"Purchase request sent. The download will begin in the background."];
[center _performPurchases:[center _newPurchasesWithItems:items] hasBundlePurchase:0 withClientContext:[SKUIClientContext defaultContext] completionBlock:^(id arg1)
{
[self dismissDownloadProgress];
}];
});
}
- (void)downloadAppWithLink:(NSString*)link
{
if (![self isNetworkReachable])
{
[self showAlert:@"No Internet" message:@"Please check your internet connection and try again."];
return;
}
NSString* targetAppIdParsed = nil;
if ([link containsString:@"id"])
{
NSArray* components = [link componentsSeparatedByString:@"id"];
if (components.count < 2)
{
[self showAlert:@"Error" message:@"Invalid link"];
return;
}
NSArray* idComponents = [components[1] componentsSeparatedByString:@"?"];
targetAppIdParsed = idComponents[0];
}
else
{
[self showAlert:@"Error" message:@"Invalid link"];
return;
}
dispatch_async(dispatch_get_main_queue(), ^
{
[self getAllAppVersionIdsAndPrompt:[targetAppIdParsed longLongValue]];
});
}
- (void)downloadApp
{
UIAlertController* linkAlert = [UIAlertController alertControllerWithTitle:@"App Link" message:@"Enter the App Store link to the app you want to download" preferredStyle:UIAlertControllerStyleAlert];
[linkAlert addTextFieldWithConfigurationHandler:^(UITextField* textField)
{
textField.placeholder = @"https://apps.apple.com/app/idXXXXXXXXX";
}];
UIAlertAction* downloadAction = [UIAlertAction actionWithTitle:@"Continue" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{
[self downloadAppWithLink:linkAlert.textFields.firstObject.text];
}];
[linkAlert addAction:downloadAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[linkAlert addAction:cancelAction];
[self presentViewController:linkAlert animated:YES completion:nil];
}
@end