A Short Note – In Which Order Custom Code Executes At Launch Time In iOS ?

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (In Which Order Custom Code Executes At Launch Time In iOS ?).

In this note series, we will learn about the App Launch Sequence in iOS. We will discuss the order that our custom code executes at launch time in iOS.

So Let’s begin.


Overview

Launching an app involves a complex sequence of steps, most of which UIKit handles automatically. During the launch sequence, UIKit calls methods of our app delegate so we can perform custom tasks. 

In Figure illustrates the sequence of below steps that occur from the time the app is launched until it is considered initialized.

  1. The app is launched, either explicitly by the user or implicitly by the system.
  2. The XCode-provided main function calls UIKit’s UIApplicationMain(_:_:_:_:) function.
  3. The UIApplicationMain(_:_:_:_:) function creates the UIApplication object and our app delegate.
  4. UIKit loads our app’s default interface from the main storyboard or nib file.
  5. UIKit calls our app delegate’s application(_:willFinishLaunchingWithOptions:) method.
  6. This UIKit performs state restoration, which calls for additional methods of our app delegate and view controllers.
  7. UIKit calls our app delegate’s application(_:didFinishLaunchingWithOptions:) method .

When initialization is complete, the system uses either our scene delegates or app delegate to display our UI and manage the life cycle for our app.


Conclusion

In this note series, we understood about the App Launch Sequence in iOS. We discussed the order that our custom code executes at launch time in iOS.

Thanks for reading! I hope you enjoyed and learned about App Launch Sequence concepts in iOS. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other website and tutorials of iOS as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

A Short Note – How To Perform One-Time Setup For iOS App ?

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (How To Perform One-Time Setup For iOS App ?).

In this note series, we will learn about how to perform one-time setup for iOS application. We will discuss how to ensure proper configuration of our app environment.

So Let’s begin.


Overview

When the user launches our app for the first time, we might want to prepare our app environment by performing some one-time tasks. For example, we might want to:

  • Download required data from our server.
  • Copy document templates or modifiable data files from our app bundle to a writable directory.
  • Configure default preferences for the user.
  • Set up user accounts or gather other required data.

Perform any one-time tasks in our app delegate’s application(_:willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions:) method. Never block the app’s main thread for tasks that do not require user input. Instead, start tasks asynchronously using a dispatch queue, and let them run in the background while our app finishes launching. For tasks that require user input, make all changes to our user interface in the application(_:didFinishLaunchingWithOptions:) method.


Install Files in the Proper Locations

Our app has its own container directory for storing files, and we should always place app-specific files in the ~/Library subdirectory. Specifically, store our files in the following ~/Library subdirectories:

  • ~/Library/Application Support/—Store app-specific files that we want backed up with the user’s other content. (We can create custom subdirectories here as needed.) Use this directory for data files, configuration files, document templates, and so on.
  • ~/Library/Caches/—Store temporary data files that can be easily regenerated or downloaded.

To obtain a URL for one of the directories in our app’s container, use the urls(for:in:) method of FileManager.

let appSupportURL = FileManager.default.urls(for: 
      .applicationSupportDirectory, in: .userDomainMask)

let cachesURL = FileManager.default.urls(for: 
      .cachesDirectory, in: .userDomainMask)

Place any temporary files in our app’s tmp/ directory. Temporary files might include compressed files that we intend to delete once their contents have been extracted and installed elsewhere. Retrieve the URL for our app’s temporary directory using the temporaryDirectory method of FileManager.


Conclusion

In this note series, we understood how to perform one-time setup for iOS application. We also discussed how to ensure proper configuration of our app environment.

Thanks for reading! I hope you enjoyed and learned about Proper App Environment Configuration concepts in iOS. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other website and tutorials of iOS as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

iOS – How To Respond To The Application Launch In iOS ?

Hello Readers, CoolMonkTechie heartily welcomes you in this article (How To Respond To The Application Launch In iOS ?).

In this article, We will learn how to respond to the application launch in iOS. We will discuss about initialize our app’s data structures, prepare our app to run, and respond to any launch-time requests from the system in iOS. We also describe why the app launched in iOS.

A famous quote about learning is :

” Research shows that you begin learning in the womb and go right on learning until the moment you pass on. Your brain has a capacity for learning that is virtually limitless, which makes every human a potential genius. “

So Let’s begin.


Overview

The system launches our app when the user taps our app’s icon on the Home screen. If our app requested specific events, the system might also launch our app in the background to handle those events. For a scene-based app, the system similarly launches the app when one of our scenes needs to appear onscreen or do some work.

All apps have an associated process, which the UIApplication object represents. Apps also have an app delegate object—an object that conforms to the UIApplicationDelegate protocol—which responds to important events happening within that process. Even a scene-based app uses an app delegate to manage fundamental events like launch and termination. At launch time, UIKit automatically creates the UIApplication object and our app delegate. It then starts our app’s main event loop.


Provide a Launch Storyboard

When the user first launches the app on a device, the system displays the launch storyboard until the app is ready to display its UI. Displaying the launch storyboard assures the user that our app launched and is doing something. If our app initializes itself and readies its UI quickly, the user may see the launch storyboard only briefly.

Xcode projects automatically include a default launch storyboard for us to customize, and we can add more launch storyboards as necessary. To add new launch storyboards to our project, do the following:

  1. Open your project in Xcode.
  2. Choose File > New > File.
  3. Add a Launch Screen resource to our project.

Add views to our launch storyboard and use Auto Layout constraints to size and position them so that they adapt to the underlying environment. UIKit displays exactly what we provide, using our constraints to fit our views into the available space. We can use  Human Interface Guidelines for for design guidance.

In iOS 13 and later, always provide a launch storyboard for your app. Don’t use static launch images.


Initialize App’s Data Structures

We put our app’s launch-time initialization code in one or both of the following methods:

  • application(_:willFinishLaunchingWithOptions:)
  • application(_:didFinishLaunchingWithOptions:)

UIKit calls these methods at the beginning of our app’s launch cycle. We use them to:

  • Initialize our app’s data structures.
  • Verify that our app has the resources it needs to run.
  • Perform any one-time setup when our app launches for the first time. For example, install templates or user-modifiable files in a writable directory.
  • Connect to any critical services that our app uses. For example, connect to the Apple Push Notification service if our app supports remote notifications.
  • Check the launch options dictionary for information about why our app launched.

For apps that aren’t scene-based, UIKit loads our default user interface automatically at launch time. Use the application(_:didFinishLaunchingWithOptions:) method to make additional changes to that interface before it appears onscreen. For example, we might install a different view controller to reflect what the user was doing the last time they used the app.


Move Long-Running Tasks off the Main Thread

When the user launches the app, make a good impression by launching quickly. UIKit doesn’t present the app’s interface until after the application(_:didFinishLaunchingWithOptions:) method returns. Performing long-running tasks in that method or the application(_:willFinishLaunchingWithOptions:) method might make the app appear sluggish to the user. Returning quickly is also important when launching to the background because the system limits the app’s background execution time.

Move tasks that are not critical to the app’s initialization out of the launch-time sequence. For example:

  • Defer the initialization of features that the app doesn’t need immediately.
  • Move important, long-running tasks off the app’s main thread. For example, run them asynchronously on a global dispatch queue.


Determine Why App Launched

When UIKit launches the app, it passes along a launch options dictionary to the application(_:willFinishLaunchingWithOptions:) and application(_:didFinishLaunchingWithOptions:) methods with information about why the app launched. The keys in that dictionary indicate important tasks to perform immediately. For example, they might reflect actions that the user started elsewhere and wants to continue in the app. Always check the contents of the launch options dictionary for keys that we expect, and respond appropriately to their presence.

For a scene-based app, examine the options that UIKit passes to the application(_:configurationForConnecting:options:) method to determine why it created the scene.

The example shows the app delegate method for an app that handles background location updates. When the location key is present, the app starts location updates immediately instead of deferring them until later. Starting location updates allows the Core Location framework to deliver the new location event.

class AppDelegate: UIResponder, UIApplicationDelegate, 
               CLLocationManagerDelegate {
    
   let locationManager = CLLocationManager()
   func application(_ application: UIApplication,
              didFinishLaunchingWithOptions launchOptions:
              [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
      // If launched because of new location data,
      //  start the visits service right away.
      if let keys = launchOptions?.keys {
         if keys.contains(.location) {
            locationManager.delegate = self
            locationManager.startMonitoringVisits()
         }
      }
       
      return true
   }
   // other methods…
}

The system doesn’t include a key unless our app supports the corresponding feature. For example, the system doesn’t include the remoteNotification key for an app that doesn’t support remote notifications.

That’s all about in this article.


Conclusion

In this article, We understood how to respond to the application launch in iOS. This article described about initialize our app’s data structures, prepare our app to run, and respond to any launch-time requests from the system in iOS.

Thanks for reading ! I hope you enjoyed and learned about the Application Launch Concepts in iOS. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other website and tutorials of iOS as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING!!???

iOS – How To Prepare UI To Run In The Background In iOS ?

Hello Readers, CoolMonkTechie heartily welcomes you in this article (How To Prepare UI To Run In The Background In iOS ?).

In this article, we will learn about how to prepare UI to run in the background in iOS. We will discuss how to prepare our app to be suspended.

A famous quote about Learning is :

” You don’t understand anything until you learn it more than one way. “

So Let’s begin.


Overview

Apps move to the background state for many reasons. When the user exits a foreground app, that app moves to the background state briefly before UIKit suspends it. The system may also launch an app directly into the background state, or move a suspended app into the background, and give it time to perform important tasks.

When our app is in the background in iOS, it should do as little as possible, and preferably nothing. If our app was previously in the foreground, use the background transition to stop tasks and release any shared resources. If our app enters the background to process an important event, process the event and exit as quickly as possible.

All state transitions result in UIKit sending notifications to the appropriate delegate object:

  • In iOS 13 and later—A UISceneDelegate object.
  • In iOS 12 and earlier—The UIApplicationDelegate object.

We can support both types of delegate objects, but UIKit always uses scene delegate objects when they are available. UIKit notifies only the scene delegate associated with the specific scene that is entering the background.


Quiet App upon Deactivation

The system deactivates apps for several reasons. When the user exits the foreground app, the system deactivates that app immediately before moving it to the background. The system also deactivates apps when it needs to interrupt them temporarily—for example, to display system alerts. In the case of a system panel, the system reactivates the app when the user dismisses the panel.

During deactivation, UIKit calls one of the following methods of our app:

  • For apps that support scenes—The sceneWillResignActive(_:) method of the appropriate scene delegate object.
  • For all other apps—The applicationWillResignActive(_:) method of the app delegate object.

Use deactivation to preserve the user’s data and put our app in a quiet state by pausing all major work; specifically:

  • Save user data to disk and close any open files.
  • Suspend dispatch and operation queues.
  • Don’t schedule any new tasks for execution.
  • Invalidate any active timers.
  • Pause gameplay automatically.
  • Don’t commit any new Metal work to be processed.
  • Don’t commit any new OpenGL commands.


Release Resources upon Entering the Background

When our app transitions to the background, release memory and free up any shared resources our app is holding. For an app transitioning from the foreground to the background, freeing up memory is especially important. The foreground has priority over memory and other system resources, and the system terminates background apps as needed to make those resources available. Even if our app wasn’t in the foreground, perform checks to ensure that it consumes as few resources as possible.

Upon entering the background, UIKit calls one of the following methods of our app:

  • For apps that support scenes—The sceneDidEnterBackground(_:) method of the appropriate scene delegate object.
  • For all other apps—The applicationDidEnterBackground(_:) method of the app delegate object.

During a background transition, perform as many of the following tasks as makes sense for our app:

  • Discard any images or media that we read directly from files.
  • Discard any large, in-memory objects that we can recreate or reload from disk.
  • Release access to the camera and other shared hardware resources.
  • Hide sensitive information (such as passwords) in our app’s user interface.
  • Dismiss alerts and other temporary interfaces.
  • Close connections to any shared system databases.
  • Unregister from Bonjour services and close any listening sockets associated with them.
  • Ensure that all Metal command buffers have been scheduled.
  • Ensure that all OpenGL commands, we previously submitted have finished.

We don’t need to discard named images that we loaded from our app’s asset catalog. Similarly, we don’t need to release objects that adopt the NSDiscardableContent protocol or that we manage using an NSCache object. The system automatically handles the cleanup of those objects.

Make sure our app is not holding any shared system resources when it transitions to the background. If it continues accessing resources like the camera or a shared system database after transitioning to the background, the system terminates our app to free up that resource. If we use a system framework to access a resource, check the framework’s documentation for guidelines about what to do.


Prepare UI for the App Snapshot

After our app enters the background and our delegate method returns, UIKit takes a snapshot of our app’s current user interface. The system displays the resulting image in the app switcher. It also displays the image temporarily when bringing our app back to the foreground.

Our app’s UI must not contain any sensitive user information, such as passwords or credit card numbers. If our interface contains such information, remove it from our views when entering the background. Also, dismiss alerts, temporary interfaces, and system view controllers that obscure our app’s content. The snapshot represents our app’s interface and should be recognizable to users. When our app returns to the foreground, we can restore data and views as appropriate.

For apps that support state preservation and restoration, the system begins the preservation process shortly after our delegate method returns. Removing sensitive data also prevents that information from being saved in our app’s preservation archive.


Respond to Important Events in the Background

Apps don’t normally receive any extra execution time after they enter the background. However, UIKit does grant execution time to apps that support any of the following time-sensitive capabilities:

  • Audio communication using AirPlay, or Picture in Picture video.
  • Location-sensitive services for users.
  • Voice over IP.
  • Communication with an external accessory.
  • Communication with Bluetooth LE accessories, or conversion of the device into a Bluetooth LE accessory.
  • Regular updates from a server.
  • Support for Apple Push Notification service (APNs).

Enable the Background Modes capability in Xcode if our app supports background features.

That’s all about in this article.


Conclusion

In this article, We understood how to prepare UI to run in the Background in iOS. We also discussed how to prepare our app to be suspended.

Thanks for reading ! I hope you enjoyed and learned about UI Preparation concepts during Background in iOS. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other website and tutorials of iOS as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

A Short Note – How To Prepare UI To Run In The Foreground In iOS ?

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (How To Prepare UI To Run In The Foreground In iOS ?).

In this note series, we will learn about how to prepare UI to run in the foreground in iOS. This note series shows how to configure our app to appear on-screen.

So Let’s begin.


Overview

We use foreground transitions to prepare our app’s UI to appear on-screen. An app’s transition to the foreground is usually in response to a user’s action. For example, when the user taps the app’s icon, the system launches the app and brings it to the foreground. We use a foreground transition to update our app’s UI, gain resources, and start the services we need to handle user requests.

All state transitions result in UIKit sending notifications to the delegate object:

  • In iOS 13 and later—A UISceneDelegate object.
  • In iOS 12 and earlier—The UIApplicationDelegate object.

We can support both types of delegate objects, but UIKit always uses scene delegate objects when they are available. UIKit notifies only the scene delegate associated with the specific scene that is entering the foreground.


Update Our App’s Data Model when Entering the Foreground

At launch time, the system starts our app in the inactive state before transitioning it to the foreground. We use our app’s launch-time methods to perform any work needed. UIKit moves our background app to the inactive state by calling one of the following methods:

  • For apps that support scenes — The sceneWillEnterForeground(_:) method of the scene delegate object.
  • For all other apps — The applicationWillEnterForeground(_:) method.

When transitioning from the background to the foreground, we use these methods to load resources from disk and fetch data from the network.


Configure User Interface and Initial Tasks at Activation

The system moves our app to the active state immediately before displaying the app’s UI. Activation is a good time to configure our app’s UI and runtime behavior; specifically:

  • Show our app’s windows if needed.
  • Change the currently visible view controller, if needed.
  • Update the data values and state of views and controls.
  • Display controls to resume a paused game.
  • Start or resume any dispatch queues that you use to execute tasks.
  • Update data source objects.
  • Start timers for periodic tasks.

We can put our configuration code in one of the following methods:

  • For a scene-based UI—The sceneDidBecomeActive(_:) method of the scene delegate object.
  • For all other apps—The applicationDidBecomeActive(_:) method of our app delegate object.

Activation is also the time to put finishing touches on our UI before displaying it to the user. Don’t run any code that might block our activation method. Instead, make sure we have everything, we need in advance. For example, if our data changes frequently outside of the app, we use background tasks to fetch updates from the network before our app returns to the foreground. Otherwise, be prepared to display existing data while we fetch changes asynchronously.


Start UI-Specific Tasks when View Appears

When our activation method returns, UIKit shows any windows that we made visible. It also notifies any relevant view controllers that their views are about to appear. We use our view controller’s viewWillAppear(_:) method to perform any final updates to our interface. For example:

  • Start user interface animations, as appropriate.
  • Begin playing media files, if auto-play is enabled.
  • Begin displaying graphics for games and immersive content at their full frame rates.

Don’t show a different view controller or make major changes to our user interface. By the time our view controller appears on-screen, our interface should be ready to display.


Conclusion

In this note series, we understood how to prepare UI to run in the foreground in iOS. We also discussed how to configure our app to appear on-screen.

Thanks for reading! I hope you enjoyed and learned about UI Preparation concepts during foreground in iOS. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe to the blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other website and tutorials of iOS as below links :

If you have any comments, questions, or think I missed something, leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

iOS – How To Manage App’s Life Cycle In iOS ?

Hello Readers, CoolMonkTechie heartily welcomes you in this article (How To Manage App’s Life Cycle In iOS ?).

In this article, We will understand how to manage App’s Life Cycle in iOS. We will discuss how to respond to system notifications when our app is in the foreground or background, and handle other significant system-related events.

For Understanding the App’s Lifecycle concepts, We will discuss on the below topics:

  • Overview
  • Respond to The Scene-Based Life-Cycle Events
  • Responds to App-Based Life-Cycle Events
  • Respond to Other Significant Events

A famous quote about Learning is :

” One learns from books and example only that certain things can be done. Actual learning requires that you do those things. “

So Let’s begin.


Overview

The current state of our app determines what it can and cannot do at any time. For example, a foreground app has the user’s attention, so it has priority over system resources, including the CPU. By contrast, a background app must do as little work as possible, and preferably nothing, because it is offscreen. As our app changes from state to state, we must adjust its behavior accordingly.

When our app’s state changes, UIKit notifies us by calling methods of the appropriate delegate object:

  • In iOS 13 and later, use UISceneDelegate objects to respond to life-cycle events in a scene-based app.
  • In iOS 12 and earlier, use the UIApplicationDelegate object to respond to life-cycle events.

If we enable scene support in our app, iOS always uses our scene delegates in iOS 13 and later. In iOS 12 and earlier, the system uses our app delegate.


Respond to The Scene-Based Life-Cycle Events

If our app supports scenes, UIKit delivers separate life-cycle events for each. A scene represents one instance of our app’s UI running on a device. The user can create multiple scenes for each app, and show and hide them separately. Because each scene has its own life cycle, each can be in a different state of execution. For example, one scene might be in the foreground while others are in the background or are suspended.

Scene support is an opt-in feature. To enable basic support, add the UIApplicationSceneManifest key to our app’s Info.plist file.

The following figure shows the state transitions for scenes. When the user or system requests a new scene for our app, UIKit creates it and puts it in the unattached state. User-requested scenes move quickly to the foreground, where they appear onscreen. A system-requested scene typically moves to the background so that it can process an event. For example, the system might launch the scene in the background to process a location event. When the user dismisses our app’s UI, UIKit moves the associated scene to the background state and eventually to the suspended state. UIKit can disconnect a background or suspended scene at any time to reclaim its resources, returning that scene to the unattached state.

We use scene transitions to perform the following tasks:

  • When UIKit connects a scene to our app, configure our scene’s initial UI and load the data our scene needs.
  • When transitioning to the foreground-active state, configure our UI and prepare to interact with the user.
  • Upon leaving the foreground-active state, save data and quiet our app’s behavior.
  • Upon entering the background state, finish crucial tasks, free up as much memory as possible, and prepare for our app snapshot.
  • At scene disconnection, clean up any shared resources associated with the scene.
  • In addition to scene-related events, we must also respond to the launch of our app using our UIApplicationDelegate object.


Responds to App-Based Life-Cycle Events

In iOS 12 and earlier, and in apps that don’t support scenes, UIKit delivers all life-cycle events to the UIApplicationDelegate object. The app delegate manages all of your app’s windows, including those displayed on separate screens. As a result, app state transitions affect our app’s entire UI, including content on external displays.

App State Transitions

The following figure shows the state transitions involving the app delegate object. After launch, the system puts the app in the inactive or background state, depending on whether the UI is about to appear onscreen. When launching to the foreground, the system transitions the app to the active state automatically. After that, the state fluctuates between active and background until the app terminates.

We use app transitions to perform the following tasks:

  • At launch, initialize our app’s data structures and UI.
  • At activation, finish configuring our UI and prepare to interact with the user.
  • Upon deactivation, save data and quiet our app’s behavior.
  • Upon entering the background state, finish crucial tasks, free up as much memory as possible, and prepare for our app snapshot.
  • At termination, stop all work immediately and release any shared resources.


Respond to Other Significant Events

In addition to handling life-cycle events, apps must also be prepared to handle the events listed in the following below points. We can use our UIApplicationDelegate object to handle most of these events. In some cases, we may also be able to handle them using notifications, allowing us to respond from other parts of our app.

  • Memory warnings – Received when our app’s memory usage is too high. Reduce the amount of memory our app uses.
  • Protected data becomes available/unavailable – Received when the user locks or unlocks their device. We use applicationProtectedDataDidBecomeAvailable(_:) and applicationProtectedDataWillBecomeUnavailable(_:) methods to check protected data availability.
  • Handoff tasks – Received when an NSUserActivity object needs to be processed. We can use application(_:didUpdate:) method to handoff tasks.
  • Time changes – Received for several different time changes, such as when the phone carrier sends a time update. We can use applicationSignificantTimeChange(_:) method to see time changes.
  • Open URLs – Received when your app needs to open a resource. We can use application(_:open:options:) method to open URLs.

That’s all about in this article.


Conclusion

In this article, We understood how to manage App’s Life Cycle in iOS. We also discussed how to respond to system notifications when our app is in the foreground or background, and handle other significant system-related events in iOS.

Thanks for reading ! I hope you enjoyed and learned about App’s Lifecycle Management Concepts in iOS. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other website and tutorials of iOS as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Exit mobile version