美文网首页
iOS读取txt内容、base64 解码存储到本地、WkWebv

iOS读取txt内容、base64 解码存储到本地、WkWebv

作者: 香烟不灭 | 来源:发表于2022-08-31 16:23 被阅读0次

    读取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

    相关文章

      网友评论

          本文标题:iOS读取txt内容、base64 解码存储到本地、WkWebv

          本文链接:https://www.haomeiwen.com/subject/capdnrtx.html