Skip to content

Commit 06d15c3

Browse files
Pablo Carrascalpablocarmu
Pablo Carrascal
authored andcommitted
- Implemented content blocking rules for unwanted requests inside the webview.
1 parent 2a00484 commit 06d15c3

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Owncloud iOs Client/Files/PreviewSupportedFiles/Office/OfficeFileView.h

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#import <UIKit/UIKit.h>
1717
#import <WebKit/WebKit.h>
18-
1918
@protocol OfficeFileDelegate
2019

2120
@optional

Owncloud iOs Client/Files/PreviewSupportedFiles/Office/OfficeFileView.m

+31-1
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,49 @@ - (void)addControlPanelToNavigate{
119119
[_webView addSubview:forwardButton];
120120
}
121121

122-
123122
- (void)configureWebView{
124123

125124
if (!_webView) {
126125
WKPreferences *webViewPreferences = [[WKPreferences alloc] init];
127126
[webViewPreferences setJavaScriptEnabled:false];
128127
WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];
128+
129+
if (@available(iOS 11.0, *)) {
130+
131+
NSMutableDictionary *urlFilter = [[NSMutableDictionary alloc] init];
132+
[urlFilter setObject:@".*" forKey:@"url-filter"];
133+
134+
NSMutableDictionary *type = [[NSMutableDictionary alloc] init];
135+
[type setObject:@"block" forKey:@"type"];
136+
137+
NSMutableDictionary *blockRules = [[NSMutableDictionary alloc] init];
138+
[blockRules setObject:urlFilter forKey:@"trigger"];
139+
[blockRules setObject:type forKey:@"action"];
140+
141+
NSMutableArray *blockRulesArray = [[NSMutableArray alloc] init];
142+
[blockRulesArray addObject:blockRules];
143+
144+
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:blockRulesArray options:NSJSONWritingPrettyPrinted error:nil];
145+
146+
NSString* newStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
147+
148+
[[WKContentRuleListStore defaultStore] compileContentRuleListForIdentifier:@"ContentBlockingRules" encodedContentRuleList:newStr completionHandler:^(WKContentRuleList *blockList, NSError *error) {
149+
150+
if (error != nil) {
151+
return ;
152+
}
153+
[webViewConfiguration.userContentController addContentRuleList:blockList];
154+
155+
}];
156+
}
157+
129158
webViewConfiguration.preferences = webViewPreferences;
130159
_webView = [[WKWebView alloc] initWithFrame:self.frame configuration:webViewConfiguration];
131160
_webView.navigationDelegate = self;
132161
_webView.scrollView.delegate = self;
133162
[self addSubview:_webView];
134163
}
164+
135165
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
136166
}
137167

0 commit comments

Comments
 (0)