UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IOSBackgroundURLSessionHandler.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "CoreMinimal.h"
7
9
10//Included for FBackgroundHttpRequestMetricsExtended
12
13//Call backs called by the bellow FBackgroundURLSessionHandler so higher-level systems can respond to task updates.
14class UE_DEPRECATED(5.5, "Use new download methods in FBackgroundURLSessionHandler.") APPLICATIONCORE_API FIOSBackgroundDownloadCoreDelegates
15{
16public:
18 DECLARE_MULTICAST_DELEGATE_FourParams(FIOSBackgroundDownload_DidWriteData, NSURLSessionDownloadTask*, int64_t /*Bytes Written Since Last Call */, int64_t /*Total Bytes Written */, int64_t /*Total Bytes Expedted To Write */);
22
24 UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static FIOSBackgroundDownload_DidWriteData OnIOSBackgroundDownload_DidWriteData;
25 UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static FIOSBackgroundDownload_DidCompleteWithError OnIOSBackgroundDownload_DidCompleteWithError;
28};
29
30//Interface for wrapping a NSURLSession configured to support background downloading of NSURLSessionDownloadTasks.
31//This exists here as we can have to re-associate with our background session after app launch and need to re-associate with downloads
32//right away before the HttpModule is loaded.
34{
35public:
36 // Initializes a BackgroundSession with the given identifier. If the current background session already exists, returns true if the identifier matches. False if identifier doesn't match or if the session fails to create.
37 UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static bool InitBackgroundSession(const FString& SessionIdentifier) {return false;}
38
39 //bShouldInvalidateExistingTasks determines if the session cancells all outstanding tasks immediately and cancels the session immediately or waits for them to finish and then invalidates the session
40 UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static void ShutdownBackgroundSession(bool bShouldFinishTasksFirst = true) {}
41
42 //Gets a pointer to the current background session
43 UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static NSURLSession* GetBackgroundSession() {return nullptr;}
44
45 UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static void CreateBackgroundSessionWorkingDirectory() {}
46
47 //Function to mark if you would like for the NSURLSession to wait to call the completion handler when
48 //OnIOSBackgroundDownload_SessionDidFinishAllEvents is called for you to call the passed completion handler
49 //NOTE: Call DURING OnIOSBackgroundDownload_SessionDidFinishAllEvents
50 UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static void AddDelayedBackgroundURLSessionComplete() {}
51
52 //Function to handle calls to OnDelayedBackgroundURLSessionCompleteHandler
53 //The intention is to call this for every call to AddDelayedBackgroundURLSessionComplete.
54 //NOTE: Once calling this your task should be completely finished with work and ready to be backgrounded!
55 UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static void OnDelayedBackgroundURLSessionCompleteHandlerCalled();
56
57 // New API for background downloading
58
59 // Value of invalid download id which could be used to compare return value of CreateOrFindDownload.
60 static const uint64 InvalidDownloadId;
61
62 // Will be invoked from didFinishDownloadingToURL or didCompleteWithError.
63 DECLARE_MULTICAST_DELEGATE_TwoParams(FOnDownloadCompleted, const uint64 /*DownloadId*/, const bool /*bSuccess*/);
64 static FOnDownloadCompleted OnDownloadCompleted;
65
66 // Will be invoked from didFinishCollectingMetrics
68 DECLARE_MULTICAST_DELEGATE_ThreeParams(FOnDownloadMetrics, const uint64 /*DownloadId*/, const int32 /*TotalBytesDownloaded*/, const float /*DownloadDuration*/)
69 UE_DEPRECATED(5.7, "FBackgroundURLSessionHandler::OnDownloadMetrics is deprecated. Use OnDownloadMetricsExtended")
71
72 // Will be invoked from didFinishCollectingMetrics
74 static FOnDownloadMetricsExtended OnDownloadMetricsExtended;
75
76 // Will be invoked from handleEventsForBackgroundURLSession application delegate. Needs to be registered very early, e.g. from static constructor.
77 // handleEventsForBackgroundURLSession is only invoked if app was killed by OS while in background and then relaunched to notify that downloads were completed.
78 // Is not invoked in any other scenario.
80 static FOnDownloadsCompletedWhileAppWasNotRunning OnDownloadsCompletedWhileAppWasNotRunning;
81
82 // Will be used by IOSAppDelegate and ApplePlatformHttp
83 static NSMutableDictionary<NSString*,void(^)()>* BackgroundSessionEventCompleteDelegateMap;
84
85 // Sets if cellular is allowed to be used for new downloads.
86 // Existing downloads will be recreated to reflect new setting value.
87 static void AllowCellular(bool bAllow);
88
89 // Creates a new download or finds existing download matching URL path.
90 // All URL's should have same path and only differ in domain.
91 // Priority is a value between 0.0 to 1.0, see NSURLSessionTaskPriorityDefault.
92 // HelperRef is optional shared reference to BackgroundHttpFileHashHelperRef.
93 // In case of HandleEventsForBackgroundURLSession this subsystem will create it's own reference.
94 static uint64 CreateOrFindDownload(const TArray<FString>& URLs, const float Priority, BackgroundHttpFileHashHelperRef HelperRef, const uint64 ExpectedResultSize = 0);
95
96 static void PauseDownload(const uint64 DownloadId);
97
98 static void ResumeDownload(const uint64 DownloadId);
99
100 // Cancels and invalidates DownloadId.
101 static void CancelDownload(const uint64 DownloadId);
102
103 // Priority is a value between 0.0 to 1.0, see NSURLSessionTaskPriorityDefault.
104 static void SetPriority(const uint64 DownloadId, const float Priority);
105
106 static uint64 GetCurrentDownloadedBytes(const uint64 DownloadId);
107
108 static bool IsDownloadFinished(const uint64 DownloadId, int32& OutResultHTTPCode, FString& OutTemporaryFilePath);
109
110 // To be used by app delegate, call it from handleEventsForBackgroundURLSession.
111 static void HandleEventsForBackgroundURLSession(const FString& SessionIdentifier);
112
113 // To be used by app delegate, call it from applicationDidEnterBackground
114 static void HandleDidEnterBackground();
115
116 // To be used by app delegate, call it from applicationWillEnterForeground
117 static void HandleWillEnterForeground();
118
119 // To be used by ApplePlatformBackgroundHttpManager
120 static void SaveBackgroundHttpFileHashHelperState();
121
122 // Returns an ordered list of CDNs used to issue actual downloads, including AbosluteURL, ResponseTime (in ms) and Status
123 // A list of URLS provided to CreateOrFindDownload might change order if CDNReorderingTimeout > 0 to ensure better success rate.
124 // List is empty before first CreateOrFindDownload call.
125 static TArray<TTuple<FString, uint32, FString>> GetCDNAnalyticsData();
126
127#if !UE_BUILD_SHIPPING
128 static void GetDownloadDebugText(const uint64 DownloadId, TArray<FString>& Output);
129#endif
130};
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_MULTICAST_DELEGATE_TwoParams(DelegateName, Param1Type, Param2Type)
Definition DelegateCombinations.h:58
#define DECLARE_MULTICAST_DELEGATE_FourParams(DelegateName, Param1Type, Param2Type, Param3Type, Param4Type)
Definition DelegateCombinations.h:76
#define DECLARE_DELEGATE(DelegateName)
Definition DelegateCombinations.h:20
#define DECLARE_MULTICAST_DELEGATE_ThreeParams(DelegateName, Param1Type, Param2Type, Param3Type)
Definition DelegateCombinations.h:67
#define DECLARE_MULTICAST_DELEGATE_OneParam(DelegateName, Param1Type)
Definition DelegateCombinations.h:49
return true
Definition ExternalRpcRegistry.cpp:601
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition IOSBackgroundURLSessionHandler.h:34
UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static void ShutdownBackgroundSession(bool bShouldFinishTasksFirst
UE_DEPRECATED(5.5, "Use new API in FBackgroundURLSessionHandler.") static bool InitBackgroundSession(const FString &SessionIdentifier)
Definition IOSBackgroundURLSessionHandler.h:37
static void GetDownloadDebugText(const uint64 DownloadId, TArray< FString > &Output)
Definition Array.h:670
Definition BackgroundHttpMetrics.h:8
Definition Tuple.h:652