can't copy .sqlite file in documents directory - sqlite

I'm not able to copy this sqlite file in documents directory: nothing has been copied.... please anyone knows what I'm I missing? Thank you!
BOOL success;
//documents path
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:#"verbi2.sqlite"];
NSFileManager *fileManager=[NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:dbPath];
if (success) return;
//bundle path
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"verbi2.sqlite"];
[fileManager copyItemAtPath:databasePathFromApp toPath:dbPath error:nil];
I've solved creating a new .sqlite file (verbi5.sqlite); probably something was corrupted.

Related

How to get a video PHAsset disk file size?

I'm trying to determine the file size on disk of a PHAsset video.
The following code is returning am error "The file “IMG_0188.mov” couldn’t be opened because you don’t have permission to view it."
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
videoURL = [(AVURLAsset*)avAsset URL];
NSNumber *videoDiskFileSize;
NSError *error;
[videoURL getResourceValue:&videoDiskFileSize forKey:NSURLFileSizeKey error:&error];
if(error){
NSLog(#"ERROR %#", error.localizedDescription);
}
NSLog(#"size is %f",[videoDiskFileSize floatValue]/(1024.0*1024.0));
}];
Here's the correct way to do it.
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession *exportSession, NSDictionary *info) {
CMTime videoDuration = CMTimeMultiplyByFloat64(exportSession.asset.duration, 1);
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, videoDuration);
NSLog(#"Byyte size = %lld", exportSession.estimatedOutputFileLength);
videoSize = exportSession.estimatedOutputFileLength;
}];

How to store server authentification in source code

In my app I'm doing various request on server API. When I'm doing those requests, part of my requester class is:
NSString *authString = [[[NSString stringWithFormat:#"serverUsername:serverPass"]dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSString *verifString = [NSString stringWithFormat:#"Basic %#",authString];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSData *myRequestData = [NSData dataWithBytes:[ms UTF8String] length:[ms length]];
[request setHTTPBody:myRequestData];
I'm not including all of source code, but then I have another strings, that I don't want to be visible in source code (fingerprints etc).
I was searching for code obfuscating, but to no success. Is there any way, to prevent my credential strings to be visible in x-code?
I think the best way is to store it in a plist file.
- (NSString *)saveRecommendUserData {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Userdata.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"Userdata" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
return path;
}

Retrieving data from a plist

`Okay so I am trying to pull questions and possible answers from a plist file I have created
<key>Question1</key>
<string></string>
<key>Answer1</key>
<array/>
<key>4</key>
<string>A</string>
<key>2</key>
<string>B</string>
<key>3</key>
<string>C</string>
So I have tried to do this in code from looking a various tutorials, I am only new to this so I know I have it all wrong
so then I want to place the question in an label created in the storyboard and the same with the possible answers????
`
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *listPath = [[self docDir]stringByAppendingPathComponent:#"work.plist"];
if (![[NSFileManager defaultManager]fileExistsAtPath:listPath]) {
[[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle]pathForResource:#"work" ofType:#"plist"]toPath:listPath error:nil];
}
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:listPath];NSLog(#"count: %i", [array count]);
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:#"work.plist";
self.firstQuestion = [temp objectForKey:#"Question1"];
self.firstanswer = [NSMutableArray arrayWithArray:[temp objectForKey:#"Answer"]];`
TRY this too.
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [path objectAtIndex:0];
NSString *plistpath = [documentsPath stringByAppendingPathComponent:#"question.plist"];
NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile:plistFilePath];
for (int i=0; i<[savedStock count]; i++)
{
[A-array addObject:[[savedStock objectAtIndex:i] valueForKey:#"A"]];
[B-array addObject:[[savedStock objectAtIndex:i] valueForKey:#"B"]];
}

remember user selected folder and reopen it by NSOpenPanel under Sandbox

Apple review require my app to remember the folder that user selected last time. But I can not make it under Sandbox. the -setDirectoryURL seems not working under Sandbox.
What should I do? Thank you for your help!
// read string saveFolder from NSUserDefaults
NSOpenPanel * myPanel = [NSOpenPanel openPanel];
[myPanel setTitle:#"Select Output Folder"];
[myPanel setCanChooseDirectories:YES];
[myPanel setCanCreateDirectories:YES];
[myPanel setAllowsMultipleSelection:NO];
[myPanel setCanChooseFiles:NO];
NSLog(#"before openpanel:folder=%#",saveFolder); // <== return normal
[myPanel setDirectoryURL:[NSURL URLWithString:saveFolder.stringValue]];
NSLog(#"readback:folder=%#",[[myPanel URL] path]); // <== return nil here
if ([myPanel runModal] == NSOKButton)
{
//
saveFolder = [[myPanel URL] path]];
// then save the saveFolder string to NSUserDefaults
//
}
it seems we should add and use this entitlement:
com.apple.security.files.bookmarks.app-scope
SAVE THE URL:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSError *error = nil;
NSData *bookmarkData = [[myPanel URL] bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
if (!error)
{
[defaults setObject:bookmarkData forKey:#"iData"];
[defaults synchronize];
}
READ BACK:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bookmarkFileURL = nil;
NSData *bookmarkData = [defaults objectForKey:#"iData"];
if (bookmarkData != nil)
{
NSError *error=nil;
bookmarkFileURL = [NSURL URLByResolvingBookmarkData:bookmarkData options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:&error];
if (error != nil) bookmarkFileURL = nil;
}
if (bookmarkFileURL)
{
[bookmarkFileURL startAccessingSecurityScopedResource];
}
//
//
[bookmarkFileURL stopAccessingSecurityScopedResource];

Get text file from document directory ipad

All,In my requirement need to send a mail with attached text file, which is in document directory,Here i can get the path of file which is in document directory,How do i attach the file from my ipad to send.
here is the code which i have tried
MFMailComposeViewController *picker=[[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate=self;
if ([MFMailComposeViewController canSendMail]){
// Create and show composer
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSLog(#"directry path %#",paths);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:#"Logs/log-858743.txt"];
NSLog(#"directry full path %#",fullPath);
NSData *data = [NSData dataWithContentsOfFile: fullPath];
NSLog(#"Data value %#",data);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];
if (fileExists) {
NSLog(#"file is there");
}else{
NSLog(#"file is not there");
}
[picker setSubject:#"Backup"];
[picker addAttachmentData:data mimeType:#"text/rtf" fileName:#"log.txt"];
[picker setMessageBody:#"testing." isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release]; }
else{
// Show some error message here
}
Here a sample, try this
MFMailComposeViewController *mViewController = [[MFMailComposeViewController alloc] init];
mViewController.mailComposeDelegate = self;
//Imposto l'oggetto
[mViewController setSubject:#"Invio Copia Commissione"];
//Preparo l' A: Devo spezzare gli indirizzi E-mail
NSArray *toRecipients = [NSArray arrayWithArray:[Globals.AEmail componentsSeparatedByString:#";"]];
[mViewController setToRecipients:toRecipients];
//Preparo il Cc: Devo spezzare gli indirizzi E-mail
if ([Config.EmailAgg length] != 0) {
NSArray *CcRecipients = [NSArray arrayWithArray:[Config.EmailAgg componentsSeparatedByString:#";"]];
[mViewController setCcRecipients:CcRecipients];
}
//Setto il corpo dell'E-mail
[mViewController setMessageBody:Globals.TestoEmail isHTML:NO];
//A questo punto visto che ho fatto 30 salvo il testo anche come file allegato dell'E-mail
[Globals.TestoEmail writeToFile:[NSString stringWithFormat:#"%#/CopiaCommissione.txt", Globals.DocumentsPath] atomically:TRUE encoding:NSUTF8StringEncoding error:nil];
NSData *Allegato = [NSData dataWithContentsOfFile:[NSString stringWithFormat:#"%#/CopiaCommissione.txt", Globals.DocumentsPath]];
[mViewController addAttachmentData:Allegato mimeType:#"text/plain" fileName:#"CopiaCommissione.txt"];
[self presentModalViewController:mViewController animated:YES];
[mViewController release];
try to change mimeType in your code like this
[picker addAttachmentData:data mimeType:#"text/plain" fileName:#"log.txt"];

Resources