NSURLDownload problem!

Discussion in 'Mac Programming' started by saleh.hi.62, Nov 13, 2011.

  1. saleh.hi.62 macrumors member

    Joined:
    Jul 25, 2011
    #1
    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];
    	 }
    }
    
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    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.
     
  3. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #3
    no its just a folder in root, i also tried other path, it does not work!
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    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?
     
  5. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #5
    thank you,
    how should i use these delegates? its confusing.
     
  6. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #6
    Magic Run Loop

    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.
     
  7. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #7
    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];
     
  8. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #8
    You don't call these methods. NSURLDownload will call these methods. You need to provide them in whatever class has this startDownloadingURL method.
     
  9. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #9
    Do Not Adjust Your Set . .

    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.
     
  10. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #10
    for the sake of GOD someone give me a sample code that works, i am getting confused more and more :(
     
  11. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #11
    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
    
     
  12. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #12
    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.
     
  13. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #13
    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!
     
  14. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #14
    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.
     
  15. chown33, Nov 14, 2011
    Last edited: Nov 14, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    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
     
  16. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #16
    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.
     
  17. chown33, Nov 15, 2011
    Last edited: Nov 15, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #17
    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.


    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.
     
  18. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #18
    ok here is my code, you check that and let me know the problem.
     

    Attached Files:

  19. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #19
    I think you mean "Can you please check that and let me know the problem?".
     
  20. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #20
    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.
     
  21. jiminaus, Nov 15, 2011
    Last edited: Nov 15, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #21
    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.
     
  22. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #22
    Thank you dear jiminaus,

    i appreciate you help :)
     
  23. wlh99 macrumors 6502

    Joined:
    Feb 7, 2008
    #23
    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
    
     

Share This Page