I am trying to download an HTML file from the internet and store it in my app for later (offline) viewing. I use the following method to download the file but my NSLogs report that no data has been downloaded. Whats the problem here? (assume an active internet connection, assume a pre-defined path to local documents directory)
urlAddress = #"http://subdomain.somesite.com/profile.html";
// Create and escape the URL for the fetch
NSString *URLString = [NSString stringWithFormat:#"%#%#", #"ttp://subdomain.somesite.com/profile.html"];
NSURL *URL = [NSURL URLWithString:
[URLString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
// Do the fetch - blocks!
NSData *theData = [NSData dataWithContentsOfURL:URL];
if(theData == nil) {
NSLog(#"NO DATA DOWNLOADED");
}
// Do the write
NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:#"Profile/profile.html"];
[theData writeToFile:filePath atomically:YES];
NSLog(#"WRITING profile.html to path %#!", filePath);
Because you shouldn't do the stringByAddingPercentEscapesUsingEncoding: stuff, which turn the URL into an invalid one http%3a%2f%2f....
Directly use
NSData *theData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://subdomain.somesite.com/profile.html"]];
Related
NSJSONSerialization returns null when i try to implement a particular api from the server, but other api's from the server works fine , when i tried the api in web it returns data of json type , and i have checked it in json viewer also , and it worked fine , NSData also returns some data, can Anyone help me get through it ?
NSString *urlString = #"url";
NSURL *url = [NSURL URLWithString:urlString];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url];
_dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if(error != nil){
NSLog(#"JSON Error: %#", error);
return;
}
I have tried the above written code.
---Edited-----
Since it was a issue from server side as server was sending an Invalid Character along with response, In json validator it was passing but not in a actual Program , Hence closing the question.
The Problem is of Escape Sequences in your URL and you are not escaping them. You have to escape those sequences properly.
NSString *urlString = #"url";
NSString *encodedString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodedString];
Take a Look at Documentation.
Check also the Escape Sequences.
Try this :
NSString *urlString = #"url";
NSURL *myUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Make a NSURLConnection and request the JSON with NSURLRequest because I don't think the 'json' is an actual file that has content so you could use dataWithContentsOfURL:
I have an application in which i am loading a pdf from the bundle by using
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("hhhh.pdf"), NULL, NULL);.
Now i want to load it from my web server. I was done it with a string url using
NSString *strUlr = [NSString stringWithFormat:#"https://s3.amazonaws.com/hgjgj.pdf"];
CFStringRef aCFString = (CFStringRef)strUlr;
//CFURLRef pdfURL = aCFString;
// NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),aCFString, NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
NSLog(#"%#",pdf);
//CFRelease(pdfURL);
but with no success..Can anybody knows how to make this code working.I dont need to load it in to pdf because of some reasons.Can anybody help me?
You still try to get url to resource that is supposed to be in your application bundle, not on remote server. Correct method to create url will be CFURLCreateWithString:
CFURLRef pdfURL = CFURLCreateWithString(NULL, aCFString, NULL);
Keep in mind that it may be better to download your pdf and cache it locally before display - that approach will also give you more flexibility in error handling and probable authentication challenges for your url
For reading purpose only you can load pdf file in Uiwebview by url.
If i am not mistaken then you probably want to read pdf from web server in your iPhone, if so then you can have UIWebView for your requirements: Here's how you can read and store it locally :
First create UIWebView and include <<UIWebViewDelegate>>
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
I am trying to eneble a button to download a pdf file from a server.
(Onto the divice)
I'm not being successful with this code (to post this question, i've changed the address)
Would you give me some advice to make it work..?
Thanks in advance.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.mydomain.com/mypdffile.pdf"]];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"<Application_Home>/Documents/"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];
You are not building a correct NSURL object to reach a server. fileURLWithPath: is a method to build URL that points to files in the file system.
Use URLWithString: writing the complete url, that is for example "http://myserver.com/myfile.pdf"
In my app app i have to parsing a xml file downloaded from internet. How to download this xml and save in documents on iphone ? and then how can i start the parsing of XML saved in documents??
You mean something like that
NSString *URLString = #"http://www.example.com/XML/note.xml";
NSURL *URL = [NSURL URLWithString:
[URLString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
NSData *dataXML = [NSData dataWithContentsOfURL:URL];
NSString *applicationDocumentsDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:#"sample.xml"];
[dataXML writeToFile:storePath atomically:TRUE];
NSData *contenuto = [NSData dataWithContentsOfFile:storePath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:contenuto]
Download the contents to an NSData object when it's loaded for the first time. Save it to disk using either one of
– writeToFile:atomically:
– writeToFile:options:error:
Which are described in the NSData class reference here: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/instm/NSData/writeToFile:atomically:
When you want to load it from disk, use [NSData dataWithContentsOfFile:] to load it into an NSData object again.
As for parsing NSData into XML, look into other answers to this very, very common question, e.g. How do I parse an NSString containing XML in Objective-C?
My aim is to convert NSURL to NSData without any memory leaks... I searched lot and found more than one answer from the website but nothing works for me. Can anyone help me?
Below is the method I tried, but so far nothing works:
NSURL *url = [NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data;
data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSString* contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"] encoding:NSUTF8StringEncoding error:nil];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc]initWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data = [[NSData alloc]initWithContentsOfURL:url options:0 error:nil];
/*do something with data*/
[data release];
[url release];
Note:
When i change my url to http://www.wikipedia.org the code does not have any memory leak... Help me...
Thanks in advance.
Looking at the code samples, you're doing it right - there should be no leak.
The fact that your leak only appears with certain urls make me think that it's happening somewhere else in your code - what are you doing to the data once you have it?
Also, are you testing for leaks on the simulator or the device? The simulator sometimes reports leaks where there aren't any - you should always do a check on the device as well to make sure that it's a real leak and not just the simulator being odd.
Sam
PS To format code, just put 4 spaces in front of each line of code. (or select it and click on the code sample button!)
NSURL *url = [NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data ; data= [NSData dataWithContentsOfURL:url];
NSError error; NSString contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"] encoding:NSUTF8StringEncoding error:nil];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc]initWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
NSData *data = [[NSData alloc]initWithContentsOfURL:url options:0 error:nil];
/do something with data/
[data release];
[url release];