Search code examples
iosnewsstand-kit

iOS Newsstand download failed when shut down the app


I'm having difficulty in the download using NewsstandKit when the app shut down.

My newsstand app download does starts in Background Mode, and exit to the background or push a download notification, the download issue everything is OK, but i shut down the app then the task is downloading,the downloading task failed.

Does it has to do some thing with the Server? or others?


Solution

  • You need to resume your downloads when the App Launches again.

    Something to the tune of this would work

    // Inside App Delegate Did Finish Launching
    NKLibrary *nkLib = [NKLibrary sharedLibrary];
    
    for(NKAssetDownload *asset in [nkLib downloadingAssets]){
        [asset downloadWithDelegate:newsstandDownloadDelegate];
    }
    

    If you read the documentation you will find the following paragraph detailing how you should handle app termination:

    While assets are being downloaded, the application could be suspended or even terminated entirely if, for instance, there is insufficient memory or the user force-quits the application. The application (assuming it has the newsstand-content property) is later relaunched into the background to handle the completion of the download or any authentication challenges or errors related to it. The procedure in this case is the following:

    When the application is launched, it can use the UIApplicationLaunchOptionsNewsstandDownloadsKey key to access an array in the launchOptions dictionary passed into the application:didFinishLaunchingWithOptions:. If there is an array, it contains the identifiers that caused the launch; these could be identifiers for downloads that have finished downloading that or could not complete downloading. It iterates through the array of NKAssetDownload objects from the downloadingAssets property of the NKLibrary object and calls downloadWithDelegate: on each of them. If it wants, the application can use the asset identifiers obtained in the previous step to check which asset downloads caused the relaunch. The NSURLConnectionDownloadDelegate object handles the asset downloads as it does normally. The Newsstand Kit framework expects all calls of its methods to be made on the main thread; the NSURLConnectionDelegate Protocol methods are also invoked on the main thread.