যখন অ্যাপ্লিকেশনটি ফোরগ্রাউন্ডে বা ব্যাকগ্রাউন্ডে থাকে তখন iOS ডেভেলপার হওয়ার কারণে আমাদের একাধিক ইভেন্ট যেমন ব্যাকগ্রাউন্ড ডাউনলোড, অ্যাপটি ফোরগ্রাউন্ডে আসলে ইভেন্টগুলি পরিচালনা করতে হবে তা জানা গুরুত্বপূর্ণ৷
এখানে আমরা দেখব কিভাবে অ্যাপ্লিকেশনটি ব্যাকগ্রাউন্ডে বা ফোরগ্রাউন্ডে আছে কিনা তা পরীক্ষা করা যায়।
আমরা এর জন্য বিজ্ঞপ্তি কেন্দ্র ব্যবহার করব,
এটি সম্পর্কে আরও পড়তে আপনি অ্যাপল নথি উল্লেখ করতে পারেন।
https://developer.apple.com/documentation/foundation/notificationcenter
একটি বিজ্ঞপ্তি প্রেরণ প্রক্রিয়া যা নিবন্ধিত পর্যবেক্ষকদের কাছে তথ্য সম্প্রচার করতে সক্ষম করে। আমরা একই সাথে পর্যবেক্ষক যোগ করব এবং কল পাব৷
৷ধাপ 1 − Xcode খুলুন → নতুন প্রকল্প → একক দৃশ্য অ্যাপ্লিকেশন → আসুন এটির নাম রাখি “ফোরগ্রাউন্ড ব্যাকগ্রাউন্ড”
ধাপ 2 − ভিউডিডলোডে নোটিফিকেশন সেন্টারের একটি অবজেক্ট তৈরি করুন
let notificationCenter = NotificationCenter.default
ধাপ 3 − ব্যাকগ্রাউন্ড এবং ফোরগ্রাউন্ডের জন্য পর্যবেক্ষক যোগ করুন
notificationCenter.addObserver(self, selector: #selector(backgroundCall), name: UIApplication.willResignActiveNotification, object: nil) notificationCenter.addObserver(self, selector: #selector(foregroundCall), name: UIApplication.didBecomeActiveNotification, object: nil)
পদক্ষেপ 4৷ − নির্বাচক পদ্ধতি প্রয়োগ করুন
@objc func foregroundCall() {
print("App moved to foreground")
}
@objc func backgroundCall() {
print("App moved to background!")
} ধাপ 5 - ব্রেকপয়েন্ট রাখুন এবং অ্যাপ্লিকেশনটি চালান।
সম্পূর্ণ কোড
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(backgroundCall), name: UIApplication.willResignActiveNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(foregroundCall), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func foregroundCall() {
print("App moved to foreground")
}
@objc func backgroundCall() {
print("App moved to background!")
}
}৷