- December 30, 2024
- Mins Read
Controls the visibility of the network activity indicator on iOS using Alamofire.
URLSession
Instances Not Managed by AlamofireAlamofire 5.0+
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Alamofire into your Xcode project using CocoaPods, specify it in your Podfile
:
pod ‘AlamofireNetworkActivityIndicator’, ‘~> 3.1’
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate Alamofire into your Xcode project using Carthage, specify it in your Cartfile
:
github “Alamofire/AlamofireNetworkActivityIndicator” ~> 3.1
https://github.com/Alamofire/AlamofireNetworkActivityIndicator
in the “Choose Package Repository” dialog.If you prefer not to use either of the aforementioned dependency managers, you can integrate AlamofireNetworkActivityIndicator into your project manually.
cd
into your top-level project directory, and run the following command “if” your project is not initialized as a git repository:
$ git init
$ git submodule add https://github.com/Alamofire/AlamofireNetworkActivityIndicator.git
Open the new AlamofireNetworkActivityIndicator
folder, and drag the AlamofireNetworkActivityIndicator.xcodeproj
into the Project Navigator of your application’s Xcode project.
It should appear nested underneath your application’s blue project icon. Whether it is above or below all the other Xcode groups does not matter.
Select the AlamofireNetworkActivityIndicator.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target.
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the “Targets” heading in the sidebar.
In the tab bar at the top of that window, open the “General” panel.
Click on the +
button under the “Embedded Binaries” section.
You will see two different AlamofireNetworkActivityIndicator.xcodeproj
folders each with two different versions of the AlamofireNetworkActivityIndicator.framework
nested inside a Products
folder.
It does not matter which Products
folder you choose from.
Select the AlamofireNetworkActivityIndicator.framework
and add it to your project.
And that’s it!
The AlamofireNetworkActivityIndicator.framework
is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
The NetworkActivityIndicatorManager
manages the state of the network activity indicator. To begin using it, all that is required is to enable the shared
instance in application:didFinishLaunchingWithOptions:
in your AppDelegate
.
NetworkActivityIndicatorManager.shared.isEnabled = true
By enabling the shared
manager for the system, the network activity indicator will show and hide automatically as Alamofire requests start and complete.
The NetworkActivityIndicatorManager
manages the currently active network request count by observing notifications emitted from Alamofire. By observing the task state changes, the shared
manager always knows how many requests are currently active and updates the visibility of the activity indicator accordingly.
It is possible to have the shared
manager observe URLSession
instances not inside Alamofire. You will need to emit matching notifications from the URLSessionDelegate
matching those found in Alamofire.
In order to make the activity indicator experience for a user as pleasant as possible, there need to be start and stop delays added in to avoid flickering. There are two such delay timers built into the shared
manager.
The start delay is a time interval indicating the minimum duration of networking activity that should occur before the activity indicator is displayed. This helps avoid needlessly displaying the indicator for really fast network requests. The default value is 1.0
second. You can easily change the default value if needed.
NetworkActivityIndicatorManager.shared.startDelay = 1.0
The completion delay is a time interval indicating the duration of time that no networking activity should be observed before dismissing the activity indicator. This allows the activity indicator to be continuously displayed between multiple network requests. Without this delay, the activity indicator tends to flicker. The default value is 0.2
seconds. You can easily change the default value if needed.
NetworkActivityIndicatorManager.shared.completionDelay = 0.2
In order to allow Alamofire to continue to be used in App Extensions, this logic could not be included in the Alamofire framework. In order to submit an App Extension to the App Store, it can only be linked against frameworks that specify they only use App Extension safe APIs. Since we want users to be able to use Alamofire in App Extensions, we MUST set the Require Only App Extension Safe APIs
to true
. Because of this, we cannot call non-safe App Extension APIs in the Alamofire framework. Controlling the activity indicator on iOS is done through non-safe App Extension APIs. Because of this, a separate library needed to be created.
But what about availability you say? Doesn’t help in this case because availability checks still compile all the code. We could not use #if os(iOS)
either because you cannot compile out logic specifically for iOS, but not for an iOS App Extension.