读取txt内容
NSError *error;
NSString *textContents=[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];
if (textContents ==nil) {
NSLog(@"---error--%@",[error localizedDescription]);
}
base64 解码存储到本地
NSData *decodedImgData = [[NSData alloc] initWithBase64EncodedString:pdfStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSFileManager *fileManger = [NSFileManager defaultManager];
NSString *signDirectory = [documentsDirectory stringByAppendingString:@"/sign"];
// 创建目录
[fileManger createDirectoryAtPath:signDirectory withIntermediateDirectories:YES attributes:nil error:nil];
// 创建文件
self.signPath = [signDirectory stringByAppendingPathComponent:@"sign.pdf"];
// 写入文件
BOOL isfinish = [fileManger createFileAtPath:_signPath contents:decodedImgData attributes:nil];
// NSLog(@"存储isfinish === %d",isfinish);
WkWebview 加载本地文件
if (@available(iOS 9.0, *)) {
[self.myWebView loadFileURL:[NSURL fileURLWithPath:localFileStr] allowingReadAccessToURL:[NSURL fileURLWithPath:localFileStr]];
} else {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:_localFileStr]];
[self.myWebView loadRequest:request];
}
删除code编码
[str stringByRemovingPercentEncoding]
进行code编码
+ (NSString *)URLEncodedString:(NSString *)orString
{
NSString *unencodedString = orString;
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedString;
}
IMG_1719.PNG
网友评论