IOS INTERVIEW QUESTIONS DAY 20
iOS Fundamentals
1️⃣ Describe the difference between shallow copy and deep copy in iOS.
In iOS, the difference between shallow copy and deep copy is crucial when handling objects and their references.
- Shallow Copy: A shallow copy creates a new reference to the original object but does not duplicate the objects themselves. This means that changes to the original object will affect the copied object since they both reference the same memory location.
- Deep Copy: A deep copy, on the other hand, creates a completely new, independent copy of the object and all objects it references. Changes made to the original object will not affect the deep-copied object.
Example:
2️⃣ What is the purpose of the UIApplication class in iOS?
The UIApplication
class is central to an iOS app's lifecycle and user interaction management. It serves as the singleton object that manages the app's main event loop, coordinates with the system to handle app transitions (like launching, entering the background, or terminating), and orchestrates various UI and non-UI-related tasks.
Key responsibilities include:
- Event Handling: Receives and dispatches user interactions like touches, gestures, and system events.
- State Transitions: Manages state changes, notifying your app when it moves between states like active, inactive, background, and foreground.
- Notification Handling: Manages push notifications, local notifications, and badge updates.
- Status Bar Management: Controls the status bar’s appearance.
Example:
3️⃣ How can you implement push notifications in an iOS app?
Implementing push notifications in an iOS app involves several steps, primarily involving the setup in both the app and backend systems.
Steps:
- App Capabilities and Certificates:
- Enable Push Notifications and Background Modes in the Xcode project.
- Generate an APNs (Apple Push Notification service) certificate from the Apple Developer portal and configure it with your backend.
2. Register for Notifications:
- Request user permission to receive notifications.
- Register the device with APNs to receive a unique device token.
3. Handle Device Token:
- Implement methods in
AppDelegate
to handle the received device token.
4. Send Push Notification:
- The backend uses the device token to send notifications via APNs. This can be tested using a tool like Postman or using Firebase Cloud Messaging (FCM).
4️⃣ Explain the concept of unit testing in iOS development.
Unit testing in iOS development involves testing individual units or components of an application, such as a function, method, or class, in isolation to ensure they work as intended.
Key Aspects:
- Isolation: Each unit test should be independent, testing only the specific piece of code without dependencies on other parts of the application.
- Automation: Unit tests are automated and can be run frequently to catch regressions early.
- Repeatability: Unit tests should consistently produce the same results each time they are run.
Example using XCTest: