-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTweak.x
75 lines (63 loc) · 2.29 KB
/
Tweak.x
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
#import <SafariServices/SafariServices.h>
#import <Cephei/HBPreferences.h>
// MARK: Preferences
static NSString* const kPrefsBundleId = @"com.andrewabosh.safari-in-messages-prefs";
static NSString* const kPrefsEnabledKey = @"IsEnabled";
static NSString* const kPrefsOpenUniversalLinksKey = @"OpenUniversalLinks";
HBPreferences *preferences;
BOOL isEnabled;
BOOL openUniversalLinks;
%ctor {
preferences = [[HBPreferences alloc] initWithIdentifier:kPrefsBundleId];
[preferences registerDefaults:@{
kPrefsEnabledKey: @YES,
kPrefsOpenUniversalLinksKey: @NO,
}];
[preferences registerBool:&isEnabled default:YES forKey:kPrefsEnabledKey];
[preferences registerBool:&openUniversalLinks default:NO forKey:kPrefsOpenUniversalLinksKey];
}
// MARK: The Magic
@interface SMSApplication: UIApplication
- (void)presentUrlInSafariVC:(NSURL*)url;
- (void)openLink:(NSURL*)url;
@end
%hook SMSApplication
%new
- (void)presentUrlInSafariVC:(NSURL*)url {
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:url];
[topController presentViewController:safariViewController animated:YES completion:nil];
}
%new
- (void)openLink:(NSURL*)url {
if (openUniversalLinks) {
[self presentUrlInSafariVC:url];
return;
}
// We only want to open URLs that aren't universal links in the SFSafariViewController
[[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @YES} completionHandler:^(BOOL success) {
if (!success) {
[self presentUrlInSafariVC:url];
}
}];
}
- (BOOL)openURL:(NSURL*)url {
if (isEnabled && ([url.scheme isEqual:@"http"] || [url.scheme isEqual:@"https"])) {
[self openLink:url];
return YES;
} else {
return %orig;
}
}
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options completionHandler:(void (^)(BOOL success))completion {
BOOL universalLinksOnly = [[options objectForKey:UIApplicationOpenURLOptionUniversalLinksOnly] boolValue];
if (isEnabled && ([url.scheme isEqual:@"http"] || [url.scheme isEqual:@"https"]) && !universalLinksOnly) {
[self openLink:url];
} else {
%orig;
}
}
%end