1

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?

Pacu
  • 1,985
  • 20
  • 33

3 Answers3

1

Pacu Try using [destinationURL path] instead of [destinationURL absoluteString]. The file manager is expecting a path not a URL in string format

Martin Lockett
  • 2,275
  • 27
  • 31
0

You need to restore downloads:

    NKLibrary *nkLib = [NKLibrary sharedLibrary];
for(NKAssetDownload *asset in [nkLib downloadingAssets]) {
    NSLog(@"Asset to downlaod: %@",asset);
    [asset downloadWithDelegate:Downloader];
}

in the UIApplication's didFinishLaunchingWithOptions.

Unfortunately for me it works only before UIView and Controller's initializing. I've tried to put this code to the applicationDidBecomeActive but it doesn't work properly when app runs offline.

slatvick
  • 1,207
  • 2
  • 16
  • 25
0

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];

        }
msk
  • 8,885
  • 6
  • 41
  • 72