forked from haoict/tiktok-god
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
173 lines (151 loc) · 7 KB
/
Tweak.xm
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
#include "Tweak.h"
/**
* Load Preferences
*/
BOOL noads;
BOOL unlimitedDownload;
BOOL downloadWithoutWatermark;
BOOL autoPlayNextVideo;
BOOL changeRegion;
NSDictionary *region;
static void reloadPrefs() {
NSDictionary *settings = [[NSMutableDictionary alloc] initWithContentsOfFile:@PLIST_PATH] ?: [@{} mutableCopy];
noads = [[settings objectForKey:@"noads"] ?: @(YES) boolValue];
unlimitedDownload = [[settings objectForKey:@"unlimitedDownload"] ?: @(YES) boolValue];
downloadWithoutWatermark = [[settings objectForKey:@"downloadWithoutWatermark"] ?: @(YES) boolValue];
autoPlayNextVideo = [[settings objectForKey:@"autoPlayNextVideo"] ?: @(NO) boolValue];
changeRegion = [[settings objectForKey:@"changeRegion"] ?: @(NO) boolValue];
region = [settings objectForKey:@"region"] ?: [@{} mutableCopy];
}
static void showAlertMessage(NSString *title, NSString *message) {
__block UIWindow* topWindow;
topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
topWindow.rootViewController = [UIViewController new];
topWindow.windowLevel = UIWindowLevelAlert + 1;
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title?:@"Alert" message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
topWindow.hidden = YES;
topWindow = nil;
}]];
[topWindow makeKeyAndVisible];
[topWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
%group CoreLogic
%hook AWEAwemeModel
- (id)initWithDictionary:(id)arg1 error:(id *)arg2 {
id orig = %orig;
return noads && self.isAds ? nil : orig;
}
- (id)init {
id orig = %orig;
return noads && self.isAds ? nil : orig;
}
- (BOOL)preventDownload {
return unlimitedDownload ? FALSE : %orig;
}
- (BOOL)disableDownload {
return unlimitedDownload ? FALSE : %orig;
}
%end
%hook AWEAwemePlayDislikeViewController
- (BOOL)shouldShowDownload:(id)arg1 {
return unlimitedDownload ? TRUE : %orig;
}
- (AWEAwemeDislikeNewReasonTableViewCell *)tableView:(id)arg1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AWEAwemeDislikeNewReasonTableViewCell *orig = %orig;
if (downloadWithoutWatermark && orig.model.dislikeType == 1) {
orig.titleLabel.text = [NSString stringWithFormat:@"%@%@", orig.titleLabel.text, @" - No Watermark"];
}
return orig;
}
- (void)tableView:(id)arg1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AWEAwemeDislikeNewReasonTableViewCell *cell = [self tableView:arg1 cellForRowAtIndexPath:indexPath];
if (downloadWithoutWatermark && cell.model.dislikeType == 1) {
[self didSelectDownloadCell];
[self dismissActionsWithExecutingBlock];
return;
}
%orig;
}
%new
- (void)didSelectDownloadCell {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch(status) {
case PHAuthorizationStatusNotDetermined:
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus authorizationStatus) {
if(authorizationStatus == PHAuthorizationStatusAuthorized) {
[self saveVideoToPhotoLibrary];
}
}];
break;
case PHAuthorizationStatusAuthorized:
[self saveVideoToPhotoLibrary];
break;
case PHAuthorizationStatusDenied:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Permission Required" message:@"TikTok needs permission to Photos" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Go To Settings" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
}]];
[self presentViewController:alert animated:YES completion:nil];
break;
}
}
%new
- (void)saveVideoToPhotoLibrary {
NSURL* videoUrl = [NSURL URLWithString:self.model.video.playURL.originURLList.firstObject];
NSURLSessionDownloadTask* downloadTask = [[NSURLSession sharedSession] downloadTaskWithURL:videoUrl completionHandler:^(NSURL* location, NSURLResponse* response, NSError* error) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
showAlertMessage(@"Download Error", [error localizedDescription]);
});
}
NSString* fileName = [[videoUrl lastPathComponent] stringByAppendingPathExtension:@"mp4"];
[location setResourceValue:fileName forKey:NSURLNameKey error:nil];
location = [[location URLByDeletingLastPathComponent] URLByAppendingPathComponent:fileName];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:location];
}
completionHandler:^(BOOL success, NSError* error) {
[[NSFileManager defaultManager] removeItemAtURL:location error:nil];
if(success) {
dispatch_async(dispatch_get_main_queue(), ^{
showAlertMessage(nil, @"Download Success");
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
showAlertMessage(@"Download Error", [error localizedDescription]);
});
}
}];
}];
[downloadTask resume];
}
%end
// Thanks chenxk-j for this
// https://github.com/chenxk-j/hookTikTok/blob/master/hooktiktok/hooktiktok.xm#L23
%hook CTCarrier
- (NSString *)mobileCountryCode {
return (changeRegion && region[@"mcc"] != nil) ? region[@"mcc"] : %orig;
}
- (NSString *)isoCountryCode {
return (changeRegion && region[@"code"] != nil) ? region[@"code"] : %orig;
}
- (NSString *)mobileNetworkCode {
return (changeRegion && region[@"mnc"] != nil) ? region[@"mnc"] : %orig;
}
%end
%hook AWEFeedGuideManager
- (BOOL)enableAutoplay {
return autoPlayNextVideo;
}
%end
%end
/**
* Constructor
*/
%ctor {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback) reloadPrefs, CFSTR(PREF_CHANGED_NOTIF), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
reloadPrefs();
%init(CoreLogic);
}