Small amount of unwanted data going to iCloud - iphone

I don't want anything to be backed up to iCloud. However, my data cannot be recreated, so I need to place it in my application's documents directory. For each file, I did the standard:
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
} else { // iOS >= 5.1
return [URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
}
}
I have 5mB of data in there. But my app is still registering 0.2kB in iCloud (Through settings->iCLoud->Manage Storage). So, just to be sure, I did this:
-(void)resetBackupAttributes {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *fileListAct = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *path in fileListAct) {
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
}
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths2 objectAtIndex:0];
NSArray *fileListCache = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:cacheDirectory error:nil];
for (NSString *path in fileListCache) {
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
}
NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES);
NSString *preferencesDirectory = [paths3 objectAtIndex:0];
NSArray *fileListPref = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:preferencesDirectory error:nil];
for (NSString *path in fileListPref) {
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
}
}
It still has 0.2kB! Is there something I am missing? Does a small amount of data gets backed up regardless... like a directory tree or something? What I really want to know is, will this 0.2kB get me rejected for not following the data storage guidelines?

ok so I will put my comment into an answer:
is it possible that your apps default plist gets backed up in the
cloud? - BUT you could hook up a proxy between your iOS (simulator)
and the internet. Just catch all outgoing data and see whats actually
get transmitted ;). e.g. SquidMan
here's the link to SquidMan just in case …
As you said you think its the plist too. You can verify that by setting a key with some junk data and see if the total amount rises. ;)

Related

DropboxSDK: error making request to /1/metadata/sandbox - (401) No auth method found. Can we trace this error using any dropbox delegate?

Using below code to upload file to dropbox, only change i made is to replace the file on dropbox server. But since then faced this problem for the first time got this warning in console only and not traceable in code but when user tries again it works. Can anyone guide why this happens?
-(void)uploadToDropbox
{
NSLog(#"Upload to dropbox from local DB");
if ([[DBSession sharedSession] isLinked])
{
StartLoadingIndicator:self];
[self addLoadingView];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localPath=[documentsDirectory stringByAppendingPathComponent:#"/MyApp/MyApp.sqlite"];
success =[fileManager fileExistsAtPath:localPath];
if (success)
{
NSLog(#"DBX_Path %#",[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] valueForKey:#"DBX_Path"]]);
}
else
{
NSLog(#"Not exist");
}
}
else
{
UIAlertView *Sessionalert=[[UIAlertView alloc]initWithTitle:#"You need to login first with valid credentials." message:nil delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[Sessionalert show];
Sessionalert=nil;
}
}
- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata
{
NSLog(#"metadata:%#", metadata);
NSEnumerator *e= [metadata.contents objectEnumerator];
DBMetadata *dbObject;
//NSUInteger numberOfFiles = [metadata.contents count];
while ((dbObject = [e nextObject])) {
if (!dbObject.isDirectory) {
NSString *fileName = [dbObject.path lastPathComponent];
if (![fileName isEqualToString:#"MyApp.sqlite"]) {
/* call dbupload if dbObject.lastModifiedDate > than your local file*/
revName = nil;
}
else{
revName = [dbObject.rev lastPathComponent];
NSLog(#"revName:%#", revName);
}
}
}
NSString *filename = #"MyApp.sqlite";
NSString *destDir = #"/";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localPath=[documentsDirectory stringByAppendingPathComponent:#"/MyApp/MyApp.sqlite"];
[[self restClient] uploadFile:filename toPath:destDir
withParentRev:revName fromPath:localPath];
}
Please guide why this happens and what's the reason behind this? Can this be traceable via any delegate method.
Finally i got..
- (void)restClient:(DBRestClient*)client loadMetadataFailedWithError:(NSError*)error {
NSLog(#"Error loading metadata: %#", error);
}
Got help from enter link description here

How to get SQLITE database file to sync in DropBox?

I am working on an IPad app.Here I want to upload whole database file to dropbox.I searched on google by but didnot found appropriate solution.I used the following code to create database.
-(BOOL) createDatabaseFile
{
NSString *docsDir;
NSArray *dirPaths;
NSFileManager *filemgr = [NSFileManager defaultManager];
BOOL successMsg = YES;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
self.detaildatabasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:#`"Database.sql"`]];
// Check For Existence of the database file
if ([filemgr fileExistsAtPath:self.detaildatabasePath])
{
//File Exists At The Path
}
else
{
//Since file is not available at the path create a Database File
successMsg = [filemgr createFileAtPath:self.detaildatabasePath contents:[NSData data] attributes:nil];
}
return successMsg;
}
So how can I get my "Database.sql" file.Please dont treat it as duplicate question.I had wasted a day for googling but didnot find solution..Please guide me.
Thanks in advance
To upload file to Dropbox you need Dropbox SDKs - https://www.dropbox.com/developers/sync/sdks/ios (how to add Dropbox.framework to your project)
Then you can upload file:
-(void) createBackupInDropbox {
NSString *filename = #"backup.sqlite"; //File name in DropBox
NSString *destDir = #"/backups"; //Destination path in DropBox
NSString *fromPath = #"..."; //local path to your DB file
/*
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
fromPath = [docsPath stringByAppendingPathComponent:#"my_database.sqlite"];
*/
[[self restClient] deletePath:[NSString stringWithFormat:#"%#/%#", destDir, filename]]; //delete file if you need to replace it
[[self restClient] uploadFile:filename toPath:destDir
withParentRev:nil fromPath:fromPath];
}
To control uploading process:
- (void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath
from:(NSString*)srcPath metadata:(DBMetadata*)metadata {
NSLog(#"File uploaded successfully to path: %#", metadata.path);
}
- (void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error {
[waitIndicator dismissWithClickedButtonIndex:0 animated:YES];
NSLog(#"File upload failed with error - %#", error);
}
- (void)restClient:(DBRestClient*)client uploadProgress:(CGFloat)progress forFile:(NSString *)destPath from:(NSString *)srcPath
{
}
And restClient method:
- (DBRestClient *)restClient {
if (!restClient) {
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
}
return restClient;
}

iOS 6: SQLite db doesn't work

I always use this code to open a sqlite db in my iPhone application, but now with iOS6 this code doesn't work. The function: sqlite3_prepare_v2 fails but I don't understand why!
I also don't understand why if I try to change the name: "gamer.sqlite" of sqlite db with another name of an not existent file, the function: 'sqlite3_open' return again SQLITE_OK value.
This is my code:
-(NSMutableArray*)view_table
{
const char *sql;
NSMutableArray *content=[[NSMutableArray alloc] initWithObjects:nil];
[self msgbox:#"eseguo"];
/*elementi da visualizzare nella tabella*/
if(sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK)
{
[self msgbox:#"opened"];
sql = "SELECT * FROM list_gamer";
if (sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
[self msgbox:#"query eseguita"];
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
[content addObject:[NSString stringWithFormat:#"%s",sqlite3_column_text(selectstmt,0)]];
}
}
}
return content;
}
- (void)viewDidLoad{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [[documentsDir stringByAppendingPathComponent:#"gamer.sqlite"] copy];
if(![[NSFileManager defaultManager] fileExistsAtPath:databasePath])
{
NSFileManager *fileManager = [NSFileManager defaultManager];
int success = [fileManager fileExistsAtPath:databasePath];
if(success)
{
[fileManager removeItemAtPath:databasePath error:nil];
return;
}
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"gamer.sqlite"];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [documentPaths objectAtIndex:0];
}
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
-(IBAction)start_game:(id)sender{
self.view=frmgame;
[self view_table];
}
Not exactly a direct answer but i personally really love using fmbd to help manage sqlite & error.
Fmdb is an Objective-C wrapper around SQLite: http://sqlite.org/
https://github.com/ccgus/fmdb

iPhone - Won’t save array to plist on iPhone

I’m trying to save an array to a .plist file on my iphone but it won’t save to it. When i try it on the simulator it works. But when i run it on the iphone it doesn’t work. I check if the file exists on the device and it does and i can read the data from the file, but it won’t write to it. Here is the code when i add the data to the .plist file.
//Sorts the arrays
[self sortArrays];
NSString *sString = [[NSString alloc] initWithFormat:#"Schema%i", currentIndex];
NSString *daPath = [[NSBundle mainBundle] pathForResource:sString ofType:#"plist"];
NSLog(#"Path: %#", daPath);
//Checks if the file exists
BOOL exists;
NSFileManager *fileManager = [NSFileManager defaultManager];
exists = [fileManager fileExistsAtPath:daPath];
if(!exists)
NSLog(#"PATH %# DOES NOT EXIST", daPath);
else
NSLog(#"PATH %# DO EXIST!!", daPath);
//Writes out the array to check if it gots any elements
for (int i = 0; i < [itemsArray count]; i++)
NSLog(#"%i: %#", i, [(NSMutableArray*)[itemsArray objectAtIndex:i] objectAtIndex:0]);
//Writes the array to the .plist
[itemsArray writeToFile:daPath atomically:YES];
//Gets the .plist file
NSMutableArray *tmpA2 = [[NSMutableArray alloc] initWithContentsOfFile:daPath];
//Checks if the new elements that got added is there.
for (int i = 0; i < [tmpA2 count]; i++)
NSLog(#"FILE: %i: %#", i, [(NSMutableArray*)[tmpA2 objectAtIndex:i] objectAtIndex:0]);
[sString release];
[itemsArray release];
sString = nil;
itemsArray = nil;
Does anyone know why it doesn’t work?
You can not write into the app bundle, you need to write to the app's document directory.
NSString *fileName = #"fileName";
NSArray *searchPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = [searchPath objectAtIndex:0];
NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
If there is initial data in a file in the app bundle on startup copy it to the document directory if it does not exist and then it can be read, modified and written..

Objective-c: How to handle errors when loading a simple txt file

I'm trying to load a simply TXT file into a NSMutableArray. My file is called NoteBook.txt. For the following purposes (handling errors), I deleted NoteBook.txt so that the App could actually NOT load it.
In the following code, I try to find out if the file exist in my Docs Folder which I'd like to load. The following code should actually NOT attempt to load the file as there isn't one. However, it does so nonetheless and I am wondering what I am doing wrong?
Imagine that the string #"NoteBook.txt" is passed to the following method and that there is no such file in the Docs Folder of the App:
-(void) loadNoteBook:(NSString *)nameOfNoteBook
{
NSLog(#"Starting method 'LoadNoteBook...'");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
//NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"NoteBook.txt"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:nameOfNoteBook];
NSError *error;
if (filePath) { // check if file exists - if so load it:
NSLog(#"Loading notebook: %#", nameOfNoteBook);
NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];
self.NoteBook = [[[tempTextOut componentsSeparatedByString: #"\n*----------*\n"] mutableCopy] autorelease];
}
else
{
// GENERATE mutable ARRAY
NSLog(#"Loading notebook failed, creating empty one...");
NoteBook = [[NSMutableArray alloc] init];
for (int temp = 0; temp < 6; temp++) {
[NoteBook insertObject:#"Empty" atIndex:temp];
}
}
}
Thanks for any suggestions, I'd really appreciate your help.
The problem is that you're checking if the NSString is set, not the path itself.
What you should probably do is check the path with NSFileManager fileExistsAtPath:isDirectory:
BOOL isDir;
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
if ([fileManager fileExistsAtPath:filePath isDirectory:&isDir] && !isDir) {
//file exists and is not a directory
}
You've got it already in your code:
NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];
if(!tempTextOut) {
if(error) {
// error specific code to execute
NSLog(#"error loading file %#: %#", filePath, error);
}
// GENERATE mutable ARRAY
NSLog(#"Loading notebook failed, creating empty one...");
NoteBook = [[NSMutableArray alloc] init];
for (int temp = 0; temp < 6; temp++) {
[NoteBook insertObject:#"Empty" atIndex:temp];
}
} else {
self.NoteBook = [[[tempTextOut componentsSeparatedByString: #"\n*----------*\n"] mutableCopy] autorelease];
}
You test on filePath, which is actually just a string you've created. You don't test if there is a file behind it. Even if
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
does return an empty string you still append nameOfNoteBook to it and will if put in an if statement, testing against a non empty string will evaluate to true.