Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
Hello guys,

here is the code that i run, no error but it does not download the URL to the specified destination!

what is wrong here?

Code:
- (void)startDownloadingURL
{
	NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/index.html"]
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
										 timeoutInterval:60.0];
	
    // Create the download with the request and start loading the data.
	NSURLDownload  *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
	
	 if (theDownload) {
		 // Set the destination file.
		 [theDownload setDestination:@"/saleh" allowOverwrite:YES];
	 }
}
 
That is an unusual pathname. Are you really trying to write to a directory on off the root directory of your disk? Or are you trying to write to your home directory? Because that's not the proper pathname for your home directory.
 
That is an unusual pathname. Are you really trying to write to a directory on off the root directory of your disk? Or are you trying to write to your home directory? Because that's not the proper pathname for your home directory.

no its just a folder in root, i also tried other path, it does not work!
 
Implement some of the NSURLDownloadDelegate methods to investigate.

In particular, implement and log downloadDidBegin:, downloadDidFinish:, and download:didFailWithError:.

What is called?

If download:didFailWithError: is called, what is the error and what do you think it means?
 
Implement some of the NSURLDownloadDelegate methods to investigate.

In particular, implement and log downloadDidBegin:, downloadDidFinish:, and download:didFailWithError:.

What is called?

If download:didFailWithError: is called, what is the error and what do you think it means?
thank you,
how should i use these delegates? its confusing.
 
Magic Run Loop

thank you,
how should i use these delegates? its confusing.

Cocoa (OS X) and CocoaTouch (iOS) have a magic, hidden, secret run loop. That run loop is what's actually running, your program is simply getting called by the run loop when the run loop feels like it. Run loop.

So the run loop is calling downloadDidBegin:, downloadDidFinish:, and download:didFailWithError, (along with many other methods). Since you don't have those methods (downloadDidBegin:, downloadDidFinish:, and download:didFailWithError) implemented in your code, Run Loop (let's capitalize his name and anthropomorphize him while we're at it) will just skip them.

But if you put:
Code:
-(void) downloadDidBegin
{
  NSLog(@"Hey, the download began");
}
in your code, your friend Run Loop will call downloadDidBegin, then execute the code in it.

So jiminaus' suggestion is to implement several methods associated with downloaded URLs, put log statements in them, and look at your log statements in your debugger output to see which methods actually get called. The you can see where (in the several step process) your attempt breaks down.
 
Cocoa (OS X) and CocoaTouch (iOS) have a magic, hidden, secret run loop. That run loop is what's actually running, your program is simply getting called by the run loop when the run loop feels like it. Run loop.

So the run loop is calling downloadDidBegin:, downloadDidFinish:, and download:didFailWithError, (along with many other methods). Since you don't have those methods (downloadDidBegin:, downloadDidFinish:, and download:didFailWithError) implemented in your code, Run Loop (let's capitalize his name and anthropomorphize him while we're at it) will just skip them.

But if you put:
Code:
-(void) downloadDidBegin
{
  NSLog(@"Hey, the download began");
}
in your code, your friend Run Loop will call downloadDidBegin, then execute the code in it.

So jiminaus' suggestion is to implement several methods associated with downloaded URLs, put log statements in them, and look at your log statements in your debugger output to see which methods actually get called. The you can see where (in the several step process) your attempt breaks down.
thank you for explanation,

but i am wondering why we have these methods (downloadDidBegi, downloadDidFinish) available, when i use them in this way my program errors!

Code:
[self downloadDidBegin:myNSURLDownloadObject];
 
thank you for explanation,

but i am wondering why we have these methods (downloadDidBegi, downloadDidFinish) available, when i use them in this way my program errors!

Code:
[self downloadDidBegin:myNSURLDownloadObject];

You don't call these methods. NSURLDownload will call these methods. You need to provide them in whatever class has this startDownloadingURL method.
 
Do Not Adjust Your Set . .

You don't call these methods. NSURLDownload will call these methods. You need to provide them in whatever class has this startDownloadingURL method.

To reiterate the above point, you are not in control. You do not call many of the methods. Run Loop calls those methods. You may write the code that is in those methods, but Run Loop calls whatever methods it wants whenever it wants to.
 
for the sake of GOD someone give me a sample code that works, i am getting confused more and more :(
 
here is my complete class, but i get nothing after running this !



Code:
#import "download.h"


@implementation download

- (void)startDownloadingURL
{
	NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/index.html"]
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
										 timeoutInterval:60.0];
	
    // Create the download with the request and start loading the data.
	NSURLDownload  *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
	 
	if (theDownload) {
        // Set the destination file.
        [theDownload setDestination:@"/saleh" allowOverwrite:YES];
    } else {
        // inform the user that the download failed.
		NSLog(@"download has fialed!");
    }
    
}
	

- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
    // Release the connection.
    
	
    // Inform the user.
    NSLog(@"eeeee");
}

- (void)downloadDidFinish:(NSURLDownload *)download
{
    NSLog(@"The download %@ has finished.", download);
	
    // Release the download connection.
    
}



@end
 
What kind of app is this running in? A command-line app or a GUI? If it's a command-line app, you won't have the run loop necessary for the delegates to work.
 
What kind of app is this running in? A command-line app or a GUI? If it's a command-line app, you won't have the run loop necessary for the delegates to work.

i am writing a library, it will be used for both matters, but as of now i am testing it based on command-line program.

but the main problem is that it does not download the webpage regardless of delegates!
 
i am writing a library, it will be used for both matters, but as of now i am testing it based on command-line program.

but the main problem is that it does not download the webpage regardless of delegates!

Actually I don't think any of NSURLDownload will work without a run loop. The actually downloading happens in the background. The call to initWithRequest:delegate: will kickstart the downloading, but it won't be until "some time later" that the download has completed.

For testing, make a simple GUI app. In your app delegate's applicationDidFinishLaunching:, call startDownloadingURL.
 
for the sake of GOD someone give me a sample code that works, i am getting confused more and more :(
Did you read the NSURLDownload class reference doc? Not just glance at it. I mean really read it. Because there's a link to sample code called QuickLookDownloader.

Did you read the Using NSURLDownload section of the Programming Guide? Because it has links to important concepts like delegate. It also has example code.


i am writing a library, it will be used for both matters, but as of now i am testing it based on command-line program.

but the main problem is that it does not download the webpage regardless of delegates!
The main problem is you haven't posted your command-line program.

We need to know what the whole program is.

Not just a few lines of it.

Posted over multiple replies.

We need something useful to work with.

Like a complete compilable program that someone else can try.

If you don't show complete code, and you don't explain exactly what you're doing (like using a command-line program for testing), it shouldn't be a surprise if no one is able to give you an answer.


EDIT
Two documents you should read thoroughly:
Cocoa Core Competencies
Cocoa Fundamentals Guide
 
Last edited:
Did you read the NSURLDownload class reference doc? Not just glance at it. I mean really read it. Because there's a link to sample code called QuickLookDownloader.

Did you read the Using NSURLDownload section of the Programming Guide? Because it has links to important concepts like delegate. It also has example code.



The main problem is you haven't posted your command-line program.

We need to know what the whole program is.

Not just a few lines of it.

Posted over multiple replies.

We need something useful to work with.

Like a complete compilable program that someone else can try.

If you don't show complete code, and you don't explain exactly what you're doing (like using a command-line program for testing), it shouldn't be a surprise if no one is able to give you an answer.


EDIT
Two documents you should read thoroughly:
Cocoa Core Competencies
Cocoa Fundamentals Guide

what i mean of command line program is that what you have seen above is my download class, in my program i make an instance of this class then send the link and destination to download it to that path, this what i want to do.

as i said i am writing a library, downloader is one part of that. i send URL and PATH then it download webpage for me, this is all i want.
 
what i mean of command line program is that what you have seen above is my download class, in my program i make an instance of this class then send the link and destination to download it to that path, this what i want to do.

The purpose of asking for complete code is so someone else other than you can see your code, compile it, and attempt to debug it.

If you don't post code, then all anyone else can do is guess at what your code really is. How could guessing at code solve your problem? The problem is in your actual code, not in someone else's guessed-at code.


as i said i am writing a library, downloader is one part of that. i send URL and PATH then it download webpage for me, this is all i want.
How is that relevant?

If there is sample code which runs, then you can look at it, compile it, and see it run. You can also modify it and see what happens. This would presumably teach you what what's needed in order to make a library that works. Even if the sample code isn't a library, it works. That's far closer to succeeding at your goal (something that works) than what you have now.

If your complaint is that it's not exactly what you want, then you've missed the point. Sample code isn't a made-to-order library that solves everyone's problem. And this forum isn't a code-factory where you place your order and simply wait for someone else to solve your problem for you.

Sample code is teaching code. It shows you what works, and shows you what is needed to make things work. Starting with working sample code and modifying it is far simpler, and far more likely to succeed, since it starts with working code. This is more likely to produce something usable than trying to invent code from scratch, especially if you don't seem to know about fundamentals such as delegates or run-loops in command-line programs.
 
Last edited:
ok here is my code, you check that and let me know the problem.
 

Attachments

  • nsurlconnection.zip
    14.5 KB · Views: 99
I think you mean "Can you please check that and let me know the problem?".

i am so sorry, apology!
these days my study, projects, etc made my mind very stormy. that is the reason even i can't focus that much even on this problem here.

kindly check my code and help me with this problem.
regard.
 
i am so sorry, apology!
these days my study, projects, etc made my mind very stormy. that is the reason even i can't focus that much even on this problem here.

kindly check my code and help me with this problem.
regard.

So I repeat again. NSURLDownload won't work at all without an NSRunLoop. I've confirmed that now. Do as I said before, put this into a GUI app so you've got a run loop.

You need to be exactly right with your delegate method names. For example, it's not downloadDidStart: but downloadDidBegin:. These are all documented in the NSURLDownloadDelegate protocol reference. Don't type in the delegate signatures, copy and paste them from the protocol reference. (Note that while the protocol itself is new in Mac OS X 10.7, it existed as an informal protocol before that.)

Implement download:didFailWithError: and log the error. This is the only way you're going to know if and why the download fails. You'll only get nil from initWithRequest:delegate: if there's an immediately recognisable problem with the given NSURLRequest. The vast majority of the possible error will be discovered and reported later via download:didFailWithError:.


BTW I hope /saleh isn't a directory. You need to give NSURLDownload the path of the file to download to; eg /saleh/index.html, if /saleh is a directory.
 
Last edited:
Read up on NSXMLDocument, and NSData.

NSXMLDocument
Code:
- (id)initWithContentsOfURL:(NSURL *)url options:(NSUInteger)mask error:(NSError **)error

- (NSData *)XMLData
NSData

Code:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.