Platforms to show: All Mac Windows Linux Cross-Platform
/MacBase/NSURLDownload/NSURLDownload Example
Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacBase/NSURLDownload/NSURLDownload Example
This example is the version from Thu, 25th Feb 2015.
Project "NSURLDownload Example.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control URLTextField Inherits TextField
ControlInstance URLTextField Inherits TextField
End Control
Control URLLabel Inherits Label
ControlInstance URLLabel Inherits Label
End Control
Control StartPushButton Inherits PushButton
ControlInstance StartPushButton Inherits PushButton
EventHandler Sub Action()
If (Me.Caption = "Start") Then
download = new MyCustomNSURLDownloadMBS(URLTextField.Text, SpecialFolder.Desktop.Child("Test.zip"), True)
download.StartDownload()
PausePushButton.Enabled = True
Me.Caption = "Cancel"
Else
PausePushButton.Enabled = False
PausePushButton.Caption = "Pause"
download.CancelDownload(True)
Me.Caption = "Start"
End If
End EventHandler
End Control
Control StatusLabel Inherits Label
ControlInstance StatusLabel Inherits Label
End Control
Control TotalSizeLabel Inherits Label
ControlInstance TotalSizeLabel Inherits Label
End Control
Control DownloadedSizeLabel Inherits Label
ControlInstance DownloadedSizeLabel Inherits Label
End Control
Control PausePushButton Inherits PushButton
ControlInstance PausePushButton Inherits PushButton
EventHandler Sub Action()
If (Me.Caption = "Pause") Then
download.PauseDownload()
Me.Caption = "Resume"
Else
download.ResumeDownload()
Me.Caption = "Pause"
End If
End EventHandler
End Control
Note "About"
This example was created by Joris Vervuurt.
Website: http://www.joris-vervuurt.com/
Property download As MyCustomNSURLDownloadMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyCustomNSURLDownloadMBS Inherits CustomNSURLDownloadMBS
EventHandler Sub DownloadCancelled()
MainWindow.StatusLabel.Text = "Status: cancelled"
MainWindow.PausePushButton.Enabled = False
MainWindow.PausePushButton.Caption = "Pause"
MainWindow.StartPushButton.Caption = "Start"
End EventHandler
EventHandler Sub DownloadComplete(File as FolderItem)
MainWindow.StatusLabel.Text = "Status: complete"
MainWindow.PausePushButton.Enabled = False
MainWindow.PausePushButton.Caption = "Pause"
MainWindow.StartPushButton.Caption = "Start"
End EventHandler
EventHandler Sub DownloadFailed(ErrorCode as Integer, ErrorMessage as String)
MainWindow.StatusLabel.Text = "Status: failed"
MainWindow.TotalSizeLabel.Text = "Error code: " + Str(ErrorCode)
MainWindow.DownloadedSizeLabel.Text = "Error message: " + ErrorMessage
MainWindow.PausePushButton.Enabled = False
MainWindow.PausePushButton.Caption = "Pause"
MainWindow.StartPushButton.Caption = "Start"
End EventHandler
EventHandler Sub DownloadPaused(ResumeData as MemoryBlock)
MainWindow.StatusLabel.Text = "Status: paused"
End EventHandler
EventHandler Sub DownloadProgress(ReceivedBytes as UInt64, TotalBytes as UInt64, PercentComplete as Integer)
MainWindow.DownloadedSizeLabel.Text = "Downloaded size (in Bytes): " + Str(ReceivedBytes)
End EventHandler
EventHandler Sub DownloadResumed()
MainWindow.StatusLabel.Text = "Status: resumed"
End EventHandler
EventHandler Sub DownloadStarted(TotalBytes as UInt64)
MainWindow.StatusLabel.Text = "Status: started"
if TotalBytes = 18446744073709551615 then
MainWindow.TotalSizeLabel.Text = "Total size (in Bytes): unknown"
else
MainWindow.TotalSizeLabel.Text = "Total size (in Bytes): " + Str(TotalBytes)
end if
MainWindow.DownloadedSizeLabel.Text = "Downloaded size (in Bytes): 0"
End EventHandler
End Class
Class CustomNSURLDownloadMBS Inherits NSURLDownloadMBS
Event DownloadCancelled()
End Event
Event DownloadComplete(File as FolderItem)
End Event
Event DownloadFailed(ErrorCode as Integer, ErrorMessage as String)
End Event
Event DownloadPaused(ResumeData as MemoryBlock)
End Event
Event DownloadProgress(ReceivedBytes as UInt64, TotalBytes as UInt64, PercentComplete as Integer)
End Event
Event DownloadResumed()
End Event
Event DownloadStarted(TotalBytes as UInt64)
End Event
EventHandler Function canAuthenticateAgainstProtectionSpace(protectionSpace as NSURLProtectionSpaceMBS) As boolean
//hide this event
End EventHandler
EventHandler Sub decideDestinationWithSuggestedFilename(filename as string)
Me.setDestination(Me.File, Me.Overwrite)
End EventHandler
EventHandler Sub didBegin()
//hide this event
End EventHandler
EventHandler Sub didCancelAuthenticationChallenge(challenge as NSURLAuthenticationChallengeMBS)
//hide this event
End EventHandler
EventHandler Sub didCreateDestination(path as string, file as folderitem)
//hide this event
End EventHandler
EventHandler Sub didFailWithError(error as NSErrorMBS)
RaiseEvent DownloadFailed(error.code, error.localizedDescription)
End EventHandler
EventHandler Sub didFinish()
RaiseEvent DownloadComplete(Me.File)
End EventHandler
EventHandler Sub didReceiveAuthenticationChallenge(challenge as NSURLAuthenticationChallengeMBS)
//hide this event
End EventHandler
EventHandler Sub didReceiveDataOfLength(length as UInt64)
Me.ReceivedBytes = Me.ReceivedBytes + length
RaiseEvent DownloadProgress(Me.ReceivedBytes, Me.TotalBytes, (Me.ReceivedBytes / Me.TotalBytes) * 100)
End EventHandler
EventHandler Sub didReceiveResponse(response as NSURLResponseMBS)
Me.TotalBytes = response.expectedContentLength()
RaiseEvent DownloadStarted(Me.TotalBytes)
End EventHandler
EventHandler Function shouldDecodeSourceDataOfMIMEType(encodingType as string) As boolean
//hide this event
End EventHandler
EventHandler Function shouldUseCredentialStorage() As boolean
//hide this event
End EventHandler
EventHandler Sub willResumeWithResponse(response as NSURLResponseMBS, startingByte as Int64)
//hide this event
End EventHandler
EventHandler Function willSendRequest(request as NSURLRequestMBS, redirectResponse as NSURLResponseMBS) As NSURLRequestMBS
//hide this event
End EventHandler
Sub CancelDownload(DeleteFile as Boolean)
Me.deletesFileUponFailure = DeleteFile
Me.cancel()
RaiseEvent DownloadCancelled()
End Sub
Sub Constructor(URL as String, File as FolderItem, Overwrite as Boolean = True)
Me.Reset()
Me.URL = URL
Me.File = File
Me.Overwrite = Overwrite
End Sub
Sub PauseDownload()
Me.deletesFileUponFailure = False
Me.cancel()
RaiseEvent DownloadPaused(Me.resumeData())
End Sub
Private Sub Reset()
Me.deletesFileUponFailure = False
Me.cancel()
Me.File = Nil
Me.Overwrite = True
Me.ReceivedBytes = 0
Me.TotalBytes = 0
Me.URL = ""
End Sub
Sub ResumeDownload()
Super.Constructor(Me.ResumeData, Me.File)
RaiseEvent DownloadResumed()
End Sub
Sub StartDownload()
Super.Constructor(new NSURLRequestMBS(Me.URL))
End Sub
Property Private File As FolderItem
Property Private Overwrite As Boolean = True
Property Private ReceivedBytes As UInt64
Property Private ResumeData As MemoryBlock
Property Private TotalBytes As UInt64
Property Private URL As String
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacBase Plugin.