I created a method that first save the json data to the file
and then I am reading the data from the file.
I am able to save and read data when I run on simulator but when I try to run the application on iPhone and debug thru, it does not save or retrieve the data.
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableData *retrievedData = [NSMutableData data];
[retrievedData appendData:data];
NSMutableString *allInfo = [[NSMutableString alloc] initWithData:retrievedData encoding:NSASCIIStringEncoding];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"credentials" ofType:#"json"];
NSData *userCredentials = allInfo;
[userCredentials writeToFile:filePath atomically:YES];
NSError *error;
NSData* JSONData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:&error];
//get the log in information from the credentials file
NSDictionary *login = [JSONDictionary objectForKey:#"login"];
//get the auth_token
NSDictionary *loginInfo = login;
if(loginInfo == NULL)
{
errorMessage.text = #"Invalid username or password";
errorMessage.hidden = NO;
}
else
{
NSString *authCode = [loginInfo objectForKey:#"auth_token"];
[self saveUserCredentials:authCode];
}
}
You should never write file to NSBundle. For the simulator, it seems a bug. Simulator have a lot more bugs than device. It's better to test your app on device instead of on the device if it's possible. Other guys also have seen this problem. See Apple's doc:
Application_Home /AppName.app
This is the bundle directory containing the app itself. Do not write
anything to this directory. To prevent tampering, the bundle directory
is signed at installation time. Writing to this directory changes the
signature and prevents your app from launching again.
I basically did this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
NSString *filepath = [NSString stringWithFormat:#"%#/%#", directory,#"credentials.json"];
And now my code is able to retrieve data on iPhone and as well as on simulator.
Related
I have developed iOS App, in which i am downloading image from server and saving it in my Document directory.
But problem which i am facing is, sometimes my images getting corrupted when i download, even if the server response isSuccessful.
Here is my code snippet,
urlFile is path of UIImage which is on server e.g: www.abcd.com/images/pqr.jpg
fileName which i am using for saving image name in my DocDirectory.
- (void)downloadFile:(NSString *)urlFile withName:(NSString *)fileName
{
NSString *trimmedString = [urlFile stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"trimmedString=%#",trimmedString);
if ([trimmedString length]>0)
{
HTTPEaterResponse *response = [HTTPEater get:[NSString stringWithFormat:#"%#",trimmedString]];
if ([response isSuccessful])
{
NSLog(#"isSuccessful");
[self saveImage:[[UIImage alloc] initWithData:[response body]] withName:fileName];
} else {
NSLog(#"Url response failed %#", [response description]);
}
}
}
- (void)saveImage:(UIImage *)image withName:(NSString *)name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSData *data = UIImagePNGRepresentation(image);
NSLog(#"image =%#",image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}
When i see my Log, it shows:
trimmedString= www.abcd.com/images/pqr.jpg
isSuccessful
image =null
Thanks in advance.
I use the following code and it works for me.
NSData *thumbImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:ImageURL]];
[thumbImageData writeToFile:cachedThumbnailFileName atomically:YES];
UIImage * image = [UIImage imageWithContentsOfFile:cachedThumbnailFileName];
imageView.image = image;
Hope it helps you. :)
To save image on directory use:
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.jpg"];
[UIImagePNGRepresentation(selectedImage) writeToFile:pngPath atomically:YES];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
have you logged the response [response body]?
Just try to convert that response as NSData and then save as image.
Using Base64Encoding class,you can convert the response as base64Data
NSData *imageData = [NSString base64DataFromString:[response body]];
[self saveImage:[[UIImage alloc] initWithData:imageData] withName:fileName];
i suggest yo use SDWebimage Class SDWebimage download.. i think its very helpful to you even i always prefer SDWebimage Library.
this are the some things.
An UIImageView category adding web image and cache management to the Cocoa Touch framework
An asynchronous image downloader
An asynchronous memory + disk image caching with automatic cache expiration handling
Animated GIF support
WebP format support
A background image decompression
A guarantee that the same URL won't be downloaded several times
A guarantee that bogus URLs won't be retried again and again
A guarantee that main thread will never be blocked
Performances!
Use GCD and ARC
instead u can use, "NSURLRequest" and "NSURLConnection"
for example if u get image URL then,
NSURL* aURL = [NSURL URLWithString:trimmedString];//get the url of string
NSURLRequest *aReq = [NSURLRequest requestWithURL:aURL];
NSURLConnection *aConnection = [NSURLConnection connectionWithRequest:aReq delegate:self]; //get a connection to url , since it confirms to delegate u need to implement delegate methods
if(aConnection == nil) //if it is nil then cancel and close the request
{
aConnection = nil;
[aConnection cancel];
}
// after this implement this delegate methods
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)received_data
{
if(image_data == nil) //if data is not initialised initialise it
{
image_data = [[NSMutableData alloc]init];
}
[self.image_data appendData:received_data];//append the received data
}
//this method is called after successful download of the image
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
{
// UIImage* sampleImage = [UIImage imageWithData:self.image_data];
// self.image = sampleImage; //convert the data to image
//since u hav complete downloaded date in "self.image_data"
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"/myImage.png"];//set the name of the image and path may it saving in application directory check it out
[self.image_data writeToFile:imagePath atomically:YES]; //saving the data with name
}
Hope this helps u :)
I need to make an offline browser on my iOS application, which allows the following:
When you have an internet connection (wifi, 3g, 4g, ...) you can download all the web pages you need to read during the day, then you can browse the content also when you have no internet connection.
I import the data from JSON file.
How can I do this?
If you have the data downloaded, save it to the documents directory and check if no internet connection, then load the content from the documents directory.
Get Documentpath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
If you have an NSDictionary:
NSString *path = [NSString stringWithFormat:#"%#",[paths objectAtIndex:0]];
NSDictionary *dict = [[NSDictionary alloc] init];
[dict writeToFile:[path stringByAppendingPathComponent:#"myOfflineData"] atomically:NO];
And to get it:
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:#"myOfflineData"]];
Or save it with a NSString etc.
To catch up your Example:
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http:......."]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; jsonResults = [jsonObjects objectForKey:#"nodes"];
[jsonData writeToFile:[path stringByAppendingPathComponent:#"myOfflineData"] atomically:NO];
And if you are offline:
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:#"myOfflineData"]];
I am using following code to download an slqite file and storing it
self.responseData = NSMutableData
I receive responseData = 2048bytes. working well.
However white writing it does create a file myFile.sqlite but it is of Zero bytes.
What is wrong?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
if(self.responseData)
{
dbPath = [dbPath stringByAppendingPathComponent:[self.selectedBtn.dbPath lastPathComponent]];
[self.responseData writeToFile:dbPath atomically:YES];
[self performSegueWithIdentifier:#"list" sender:self];
}
}
[self.alertView removeFromSuperview];
}
-(NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSURL *url = [[NSURL alloc] initWithString:[self.selectedBtn.dbPath lastPathComponent]];
return [documentsDir stringByAppendingPathComponent:[url lastPathComponent]];
}
I am getting error
`The operation couldn’t be completed. (Cocoa error 4.)`
dbPath : /Users/umar/Library/Application Support/iPhone Simulator/6.1/Applications/00027635-CE9C-48C3-8000-64CA1E6532F1/Documents/music.sqlite/music.sqlite
You are appending [self.selectedBtn.dbPath lastPathComponent] twice, once in getDBPath, and again ion connctionDidFinishLoading.
Pick one of those instances to remove.
Documents/music.sqlite/music.sqlite
Nah, this is not a valid path... You meant Documents/music.sqlite, didn't you? Also, I don't see why you're raping poor NSURL for an unrelated task.
return [documentsDir stringByAppendingPathComponent:[self.selectedBtn.dbPath lastPathComponent]];
Also, make sure self.selectedBtn.dbPath also is indeed a valid path and not junk (I could imagine it is).
As my above screen shot that i have a json file (productDetail.json) in my app then i write file from json data like below code
else if([(MultipleURLConnection *)connection tag]==2)
{
if([fn writeToFile:responseData fileName:#"productDetail.json"])
[self performSelector:#selector(loadingJSON) withObject:nil afterDelay:0.1];
}
in above code responseData is NSMutableData and fn is function Class object name where i have below method
- (BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:myFile]) // if file doesn't exist, create it
return ([fm createFileAtPath:myFile contents:data attributes:nil]);
else
return ([data writeToFile:myFile atomically:YES]);
}
Now i decide to remove json file from app and to put it on server and then fetch it .so after putting json file on server i tried to fetch it like below coding
else if([(MultipleURLConnection *)connection tag]==2)
{
NSURL *url = [NSURL URLWithString:#"http://mysite.com/Infomist/productDetail.json"];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
{
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error == nil)
NSLog(#"show data %#",result);
}
Now above method successfully fetching my data from server it show all my data in NSlog but problem for me is now how to modify this Method now
if([fn writeToFile:responseData fileName:#"])
Beacuse after putting the json file on server i don't have any file to pass file name in above method to call the
- (BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName .
I already have App flow for local json file so i need only to write json data in my app.
- (BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName
the above requires NSData as a parameter
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
the above gives id as a return type object..you need to check which type of object it is using isKindOfClass
and then later convert it to data using decoding/NSkeyedArchiver or other techniques
I'm currently building quite a large iPhone application. Bigger than I expected anyway. But that is beside the point, the overall idea of the application is to grab JSON from a web service, sort it all into custom NSObject's that are linked together and then present.
This goes all well and good. But, because I want the user to be able to see this information on the device without an internet connection, I need to save the information that I am presenting into the Documents folder that each Application has.
I basically implemented the NSCoding protocol into all the custom NSObject's that would need it in order to save it into a subdirectory of the Documents directory.
This is all achieved through this function here.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:#"Company.archive"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataFileString]) //Does directory already exist?
{
MACompany *company = [MACompany sharedMACompany];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"Data"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) //Does directory already exist?
{
if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(#"Create directory error: %#", error);
}
}
NSString *dataFileString = [dataPath stringByAppendingPathComponent:#"Company.archive"];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:company forKey:#"MACompany"];
[archiver finishEncoding];
[[NSFileManager defaultManager] createFileAtPath:dataFileString
contents:data
attributes:nil];
[archiver release];
[data release];
} else {
NSLog(#"File already exists, no need to recreate, not yet anyway");
}
}
I do the following request when the user first loads the application (application didFinishLaunchingWithOptions:) and when the user opens the application after being in the background (applicationWillEnterForeground:).
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:#"Company.archive"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataFileString]) //Does directory already exist?
{
NSLog(#"Create a new Company Request");
MAWebRequests *companyReq = [[MAWebRequests alloc] init];
[companyReq getCompanyDetails];
[companyReq release];
} else {
NSLog(#"Saved Company Needs to be Decoded applicationWillEnterForeground");
MACompany *company = [MACompany sharedMACompany];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:#"Company.archive"];
NSData *data = [[NSData alloc] initWithContentsOfFile:dataFileString];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[data release];
company = [unarchiver decodeObjectForKey:#"MACompany"];
[unarchiver finishDecoding];
[unarchiver release];
}
Now this works all well and good and I can pull from this file also. But, I can only grab the data stored in this file when I have Xcode's debugger attached to the application. As soon as is stopped, the data is corrupted and doesn't include the original data.
The data is still stored there, I can see the created file, but the actual data itself that is stored within the file is wrong...
Should I not be using the above logic to save the data to the file and then pull recreate the shared object?
Has anyone else tried to do such a thing and had success?
Is there any reason as to why I'm running into this weird issue?
Has anyone else had this issue?
Any help would be greatly appreciated, have been trying all sorts of different methods to get it to work and nothing has been able to get there. All I need to be able to do is be able to store the data there permanently until I need to update it...
I have resolved this issue by saving the MACompany object well before applicationDidEnterBackground:
Major thanks to #OleBegemann for his aid in finding where the issue nested.