Home Forums iOS Development Networking in Swift: How to download a file with NSURLSession

This topic contains 9 replies, has 2 voices, and was last updated by  Malek 4 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2085 Reply

    Malek
    Keymaster

    This is the official thread to discuss the following tutorial post: Networking in Swift: How to download a file with NSURLSession

    #2496 Reply

    Brandon

    I saw your tutorial and love it. I have a project I’m working on that opens a PDF that was emailed to the user. The incoming PDF comes as an NSURL to the Document Directory Inbox. I’m trying to save this PDF and view it later in a WebView. I tried to implement your code and setting the URL to my NSURL instead and it did not like it. Is there a way to adjust the URL to accept URLs without being in HTML format, not from a website but an NSURL of a location already on your phone?

    Thank you,
    Brandon

    #2502 Reply

    Malek
    Keymaster

    Hi Brandon,

    Once the file is downloaded, you can save it to the document directory in your bundle. Later, you will:

    – Construct a request object using the url path of the file (in the Document directory)
    – Load the request object inside the UIWebView using the loadRequest instance method.

    If you can, please paste your relevant code here so that I can help you better.

    Happy coding 🙂

    • This reply was modified 1 year ago by  Malek.
    #2980 Reply

    Jetzael

    thanks for the tutorial, unique in its category. I have a question, if I wanted more of a download button in the same viewcontroller.swift, that part of the code I need to change? regards

    #2984 Reply

    John

    When I change URL to another online pdf it does not show the new pdf, but continues to show the old one. Do you know why this could be? All I am doing is changing the URL string, nothing more.

    #2985 Reply

    Malek
    Keymaster

    Hi John, this is because the app doesn’t override the file path if it does already exist, that’s why you will always get shown the first saved PDF file.

    To fix this, just replace the following code inside the URLSession:downloadTask:didFinishDownloadingToURL protocol method:

    With this:

    • This reply was modified 7 months, 3 weeks ago by  Malek.
    #3208 Reply

    astha

    downloadTask = backgroundSession.downloadTaskWithURL(url)
    getting crash here ????

    #3264 Reply

    Andy

    The app compiles and runs on an iPhone 6 Plus and downloads the PDF but unfortunately the progress bar shows nothing. What can I do to correct this?

    #3278 Reply

    Andy

    Hi Malek
    Great tutorial again. The app compiles, runs on an iPhone 6 Plus and downloads the PDF but unfortunately the progress bar shows nothing. What can I do to correct this please?

    #3279 Reply

    Malek
    Keymaster

    Hi Andy,

    The progress bar is running correctly while testing with Xcode 7 and Xcode 8. Are you using a different code than in the tutorial?

    Malek

Viewing 10 posts - 1 through 10 (of 10 total)
Reply To: Networking in Swift: How to download a file with NSURLSession
Your information: