第 1 步:下载文件并保存
NSError *err = nil;
NSString *url = [[NSString stringWithFormat:@"http://www.example.com/playlist.m3u8"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *the_file = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
if(err != nil) {
// do some error handling, e.g. NSLog(@"Fail: %@", [err localizedDescription]);
} else {
// save the file ( see Step 2 )
}
第 2 步:保存文件
您可以将其保存为文件,也可以将其保存为对象。
// save as file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/playlist.m3u8", docDirectory];
[the_file writeToFile:fileName atomically:NO encoding:NSASCIIStringEncoding error:nil];
// OR, save as object
[[NSUserDefaults standardUserDefaults] setObject:the_file forKey:@"MY_M3U8"];
第三步:读取文件
// read from file
NSString *textPath = [docDirectory stringByAppendingPathComponent:@"playlist.m3u8"];
// the FILE is loaded
// OR, read from object
NSString *content = [[NSUserDefaults standardUserDefaults] stringForKey:@"MY_M3U8"];
// it will contain the CONTENT of file
第 4 步:在播放器中播放
你应该这样做的。不用我写了吧?