Search code examples
nsurlconnectionnsfilemanagernewsstand-kit

NKAssetDownload destinationURL does not exist as file or directory


I'm trying to migrate to NewsstandKit to benefit from background download.

I'm being able to start the NKAssetDownload and set a delegate. But when it comes to get the downloaded content I'm not able to get it from the file system.

I'm querying a CMS for content with certain parameters and it returns a file download. The download progresses normally BUT the file is nowhere to be found afterwards.

This is the code that corresponds to the download delegate:

- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL {

#if DEBUG 
    NSLog(@"NewsstandTracker.m connectionDidFinishDownloading destinationURL:%@",destinationURL);

    NSLog(@"========= destination url contents =======");


    NSFileManager *manager = [[[NSFileManager alloc] init] autorelease];

    NSError *error = nil;
    BOOL isdir = 0;
    BOOL exists = [manager fileExistsAtPath:[destinationURL absoluteString] isDirectory:&isdir];
    NSLog(@"file exists: %d isDir:%d",exists,isdir);
    NSLog(@"error: %@",error);
    NSString *f = [[[assetDownload URLRequest] URL] lastPathComponent];
    NSDirectoryEnumerator * enumerator = [manager enumeratorAtPath:[[destinationURL absoluteString]stringByAppendingPathComponent:f]];

    for (NSString *url in enumerator) {

        NSLog(@"%@",url);
    }



    NSLog(@"======= contentURL contents =======");

    manager = [[[NSFileManager alloc] init] autorelease];
    NSDirectoryEnumerator * en = [manager enumeratorAtPath:[[[self getNewsstandIssue] contentURL] absoluteString]];


    for (NSString *url in en) {

        NSLog(@"%@",url);
    }

#endif
//do more stuff according to NewsstandKit docs

This is the output of the console

2012-01-02 16:01:45.843[499:707] NewsstandKit: cleaning up abandoned asset downloads: (
    "<NKAssetDownload: 0x36dc510> -> {identifier: '75F48F44-ADF9-4F83-ABF6-5C03F57524C1/508E80A2-A8AA-4DD1-A979-715395E4E8DD'  request: <NSURLRequest http://server-content/getFreeIssue/a_product_id>  downloading: NO}"
)
2012-01-02 16:02:43.613[499:707] NewsstandTracker.m connectionDidFinishDownloading destinationURL:file://localhost/private/var/mobile/Applications/992490C5-2607-4942-B06D-4EE9CD6226E4/Library/Caches/bgdl-499-2e4c509b06864f5c.issue23
2012-01-02 16:02:43.614[499:707] ========= destination url contents =======
2012-01-02 16:02:43.614[499:707] file exists: 0 isDir:0
2012-01-02 16:02:43.615[499:707] error: (null)
2012-01-02 16:02:43.616 GSMagazine[499:707] ======= contentURL contents =======

It seem that somehow is clearing up the download before I can even do something with it.

Any clues?


Solution

  • This is the same problem I faced after moving to NewsStand download. iOS cancel any pending downloads which you do not start again when application starts. This should be done in didFinishLaunchingWithOptions method

        NSUInteger count  = [[NKLibrary sharedLibrary] downloadingAssets].count;
            for(NSUInteger i = 0 ; i < count ; i++)
            {
                MAZDebugLog(@"We have some pending downloads");
                NKAssetDownload *asset = [[[NKLibrary sharedLibrary] downloadingAssets] objectAtIndex:i];
    
    [asset downloadWithDelegate:self];
    
            }