Skip to content

Commit ec0ecba

Browse files
authored
Fixing an issue with WK not loading referenced resources. (#320)
* Fixing an issue with WK not loading referenced resources. * Switching to use NSURL to load html content
1 parent cf62137 commit ec0ecba

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

Leanplum-SDK/Classes/Features/Actions/LPActionContext.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ - (NSString *)fillTemplate:(NSString *)value
243243
return value;
244244
}
245245

246-
- (NSString *)htmlWithTemplateNamed:(NSString *)templateName
246+
- (NSURL *)htmlWithTemplateNamed:(NSString *)templateName
247247
{
248248
if ([LPUtils isNullOrEmpty:templateName]) {
249249
[Leanplum throwError:@"[LPActionContext htmlWithTemplateNamed:] "
@@ -298,9 +298,19 @@ - (NSString *)htmlWithTemplateNamed:(NSString *)templateName
298298
}
299299
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"##Vars##"
300300
withString:jsonString];
301-
return [self fillTemplate:htmlString];
301+
302+
htmlString = [self fillTemplate:htmlString];
303+
304+
// Save filled template to temp file which will be used by WebKit
305+
NSString *randomUUID = [[[NSUUID UUID] UUIDString] lowercaseString];
306+
NSString *tmpPath = [LPFileManager fileRelativeToDocuments:randomUUID createMissingDirectories:YES];
307+
NSURL *tmpURL = [[NSURL fileURLWithPath:tmpPath] URLByAppendingPathExtension:@"html"];
308+
309+
[htmlString writeToURL:tmpURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
310+
311+
return tmpURL;
302312
LP_END_TRY
303-
return @"";
313+
return nil;
304314
}
305315

306316
- (NSString *)getDefaultValue:(NSString *)name

Leanplum-SDK/Classes/LPActionContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- (NSDictionary *)dictionaryNamed:(NSString *)name;
3636
- (NSArray *)arrayNamed:(NSString *)name;
3737
- (UIColor *)colorNamed:(NSString *)name;
38-
- (NSString *)htmlWithTemplateNamed:(NSString *)templateName;
38+
- (NSURL *)htmlWithTemplateNamed:(NSString *)templateName;
3939

4040
/**
4141
* Runs the action given by the "name" key.

Leanplum-SDK/Classes/LPMessageTemplates.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,8 +1219,20 @@ - (void)refreshPopupContent
12191219
[webView loadRequest:[NSURLRequest requestWithURL:
12201220
[NSURL URLWithString:[context stringNamed:LPMT_ARG_URL]]]];
12211221
} else {
1222-
NSString *html = [context htmlWithTemplateNamed:LPMT_ARG_HTML_TEMPLATE];
1223-
[webView loadHTMLString:html baseURL:nil];
1222+
if (@available(iOS 9.0, *))
1223+
{
1224+
NSURL *htmlURL = [context htmlWithTemplateNamed:LPMT_ARG_HTML_TEMPLATE];
1225+
// Allow access to base folder.
1226+
NSString *path = [LPFileManager documentsPath];
1227+
NSURL* baseURL = [NSURL fileURLWithPath:path isDirectory:YES];
1228+
1229+
[webView loadFileURL:htmlURL allowingReadAccessToURL:baseURL];
1230+
}
1231+
else
1232+
{
1233+
NSURL *htmlURL = [context htmlWithTemplateNamed:LPMT_ARG_HTML_TEMPLATE];
1234+
[webView loadRequest:[NSURLRequest requestWithURL:htmlURL]];
1235+
}
12241236
}
12251237
}
12261238
}

0 commit comments

Comments
 (0)