使用本地的js、css、html,但是使用相对路径的src及href。需要把相对路径转换为绝对路径。(如果路径不是/或者.开头的不进行转换)
Swift
func convertHtmlRelativePath(input: String, absolutPath: String) -> String {
do {
let regular = try NSRegularExpression(pattern: "(=['|\"])[/|.]+", options: .caseInsensitive)
return regular.stringByReplacingMatches(in: input, options: .reportCompletion, range: NSRange(location: 0, length: input.count), withTemplate: "$1\(absolutPath)/")
} catch {
return input
}
}
OC
- (NSString *)convertHtmlRelativePath:(NSString *)input absolutPath:(NSString *)absolutPath
{
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(=['|\"])[/|.]+" options:NSRegularExpressionCaseInsensitive error:&error];
if (error) {
return input;
}
return [regex stringByReplacingMatchesInString:input options:NSMatchingReportCompletion range:NSMakeRange(0, input.length) withTemplate:[@"$1" stringByAppendingString:absolutPath]];
}
网友评论