NKAssetDownload destinationURL不作为文件或目录存在

时间:2012-01-02 19:07:18

标签: nsurlconnection nsfilemanager newsstand-kit

我正在尝试迁移到NewsstandKit,以便从后台下载中受益。

我能够启动NKAssetDownload并设置委托。但是当涉及到获取下载的内容时,我无法从文件系统中获取它。

我正在查询CMS以获取具有某些参数的内容,并返回文件下载。下载正常进行,但之后无法找到该文件。

这是与下载委托相对应的代码:

- (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

这是控制台的输出

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 =======

在我甚至可以用它做某事之前,似乎不知何故正在清理下载。

任何线索?

3 个答案:

答案 0 :(得分:1)

淡水白鲳 尝试使用[destinationURL path]而不是[destinationURL absoluteString]。文件管理器期望路径不是字符串格式的URL

答案 1 :(得分:0)

这是我在转到NewsStand下载后面临的同样问题。 iOS取消任何未在应用程序启动时再次启动的待处理下载。这应该在didFinishLaunchingWithOptions方法

中完成
    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];

        }

答案 2 :(得分:0)

您需要恢复下载:

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

在UIApplication的didFinishLaunchingWithOptions中。

不幸的是,它只适用于UIView和Controller的初始化之前。我已尝试将此代码放入applicationDidBecomeActive,但当应用程序脱机运行时,它无法正常运行。