xcode - IOS: copy a file in documents folder -
in project have 2 file .txt (in resources folder), how can copy them inside documents folder?
copies txtfile
resource document if not present.
nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nserror *error; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *txtpath = [documentsdirectory stringbyappendingpathcomponent:@"txtfile.txt"]; if ([filemanager fileexistsatpath:txtpath] == no) { nsstring *resourcepath = [[nsbundle mainbundle] pathforresource:@"txtfile" oftype:@"txt"]; [filemanager copyitematpath:resourcepath topath:txtpath error:&error]; }
if want overwrite every time try this:
if ([filemanager fileexistsatpath:txtpath] == yes) { [filemanager removeitematpath:txtpath error:&error]; } nsstring *resourcepath = [[nsbundle mainbundle] pathforresource:@"txtfile" oftype:@"txt"]; [filemanager copyitematpath:resourcepath topath:txtpath error:&error];
Comments
Post a Comment