problem with connecting the server and downloading the data - objective-c

i am trying to download the data from the server for that i wrote code with in the action method but it is not at all help full.can any one tell me the exact way to connect and download the data from the server.
-(IBAction)download {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"books"];
NSURL *downloadUrl = [NSURL URLWithString:#"url contains .zip file"];
NSData *data = [NSData dataWithContentsOfURL:downloadUrl];
NSLog(#"%#",data);
[data writeToFile:path atomically:YES];
NSLog(#"final line");
NSLog(#"%#",data);
NSData *newdata=[NSData dataWithContentsOfFile:path];
NSLog(#"the new data %#",newdata);*/
}

NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; - (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData // Call when data is received from url
{
if (data==nil) data = [[NSMutableData alloc] init];
[data appendData:incrementalData];
}

Related

writeToFile not properly saving data

I have data from a url (.m4a) and I am trying to save the file so I can edit it's metadata (change background image). The code below doesn't seem to work and I have no idea why. It is saving a file, but the file is empty.
_previewData = [NSMutableData dataWithContentsOfURL:[NSURL URLWithString:#"http://a281.phobos.apple.com/us/r1000/119/Music/v4/f1/7b/d6/f17bd6e3-55c0-b7e0-9863-bc522900e950/mzaf_5153970109972844579.aac.m4a"]];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory=[paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat: #"file.mp4"]];
NSFileManager *filemanager;
filemanager = [NSFileManager defaultManager];
[_previewData writeToFile:path atomically:YES];
if ([filemanager fileExistsAtPath:path]) {
// Runs
NSLog(#"It worked");
}
NSLog(#"%#",path);
ANMovie* file = [[ANMovie alloc] initWithFile:path]; // or .mp4
NSData* jpegCover = UIImageJPEGRepresentation(artworkImage, 1.0);
_imageView.image = [UIImage imageWithData:jpegCover];
ANMetadata* metadata = [[ANMetadata alloc] init];
metadata.albumCover = [[ANMetadataImage alloc] initWithImageData:jpegCover type:ANMetadataImageTypeJPG];
[file setMovieMetadata:metadata];
[file close];
_previewData = [NSMutableData dataWithContentsOfFile:path];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSDictionary *imageItem=#{#"public.mpeg-4-audio":self.previewData};
NSDictionary *textItem=#{#"public.plain-text":self.linkData};
pasteboard.items=#[imageItem,textItem];

How to save and retrieve files to apps temp folder in ios

I'm new to IOS development. I'm developing an app which involves downloading files and saving that to apps temp folder and I dont know how to do that my current code is given below
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSLog(#"%#",tmpDirURL);
NSString *myString = [tmpDirURL absoluteString];
for(int i=0;i<responseArray.count;i++){
ASIHTTPRequest *saveUrl = [ASIHTTPRequest requestWithURL:responseArray[i]];
[saveUrl setDownloadDestinationPath:myString];
[request startSynchronous];
}
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myString error:&error];
NSLog(#"%#",directoryContents);
The response array contain a list of URL for downloading files. I know something wrong with my code but I cant find out that error please help me to solve this problem
I found the solution'
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
[data writeToFile:htmlFilePath atomically:YES];
When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location. If downloadDestinationPath is not set, download data will be stored in memory
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSLog(#"%#",tmpDirURL);
NSString *myString = [tmpDirURL absoluteString];
for(int i=0;i<responseArray.count;i++){
ASIHTTPRequest *saveUrl = [ASIHTTPRequest requestWithURL:responseArray[i]];
[saveUrl setDownloadDestinationPath:[myString stringByAppendingPathComponent:[NSString stringWithFormat:#"%i",i ]]; // for each file set a new location inside tmp
[request startSynchronous];
}
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myString error:&error];
NSLog(#"%#",directoryContents);

Comparing two NSData

In my project I need to compare if a website changed from now to 30 minutes later, so after a research, I arrived in this:
- (IBAction)saveHTML:(id)sender{
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
self.Surl= [self.meuWebView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
NSLog(#"URL: %#",self.Surl);
// Download and write to file
NSURL *url = [NSURL URLWithString:self.Surl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
// [web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
self.oldURL = urlData;
self.timerURLNew = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(saveHTMLNew:) userInfo:[NSNumber numberWithBool:YES] repeats:NO];
}
-(void)saveHTMLNew:(id)sender{
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
self.Surl= [self.meuWebView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
NSLog(#"%#",self.Surl);
NSURL *url = [NSURL URLWithString:self.Surl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
self.newURL = urlData;
if (![self.oldURL isEqualToData:self.newURL]) {
NSLog(#"Something changed");
}
else {
NSLog(#"Nothing changed");
}
}
The problem is that everytime I get "Something Changed". For testing I create a blog to post something to change its HTML.

NSURLConnection (Syncronous Request) returns (null)

I want to get HTML from website. I wrote below code but this returns (null).
NSString *strURL = #"http://www.googole.com";
- (NSString *)getHtml
{
NSURL *url = [NSURL URLWithString: strURL];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest : req
returningResponse : &response
error : &error];
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return strData;
}
I think you have mistyped the url.
It should be NSString *strURL = #"http://www.google.com";

How can I see components of NSData?

I am trying to write a PDF file from NSData I got from an http request and then display the pdf. When I call [response description], I get <30>, so I am at least getting something back. However, when I run this code, an error comes up: "failed to find PDF header: `%PDF' not found."
Is there some way to see what my NSData is if it is not a pdf? Am I going about this incorrectly? Thanks!
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if (response) {
NSString *description = [response description];
NSLog(#"description: %#", description); //this prints: <30>
} else {
NSLog(#"response is nil");
}
NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *filename = #"Test1.pdf";
NSString *dirPath = [dirs objectAtIndex:0];
NSError *error = nil;
NSString *path=[dirPath stringByAppendingPathComponent:filename];
[response writeToFile:path atomically:YES];
NSLog(#"Write returned error: %#", [error localizedDescription]);
NSURL *pdfUrl = [NSURL fileURLWithPath: path];
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];