سلسلة #SwiftUI_نتعلم
في درسنا الثاني سنتعلم سويا ماهي الملفات الأساسية عند إنشاء مشروع Swift UI و لماذا هي موجودة و ماهي هي أهميتها ؟
١- في الجهة اليسار من البرنامج نلاحظ وجود قائمة بها ملفات و تسمها Navigation وهي بتعطينا عدة خيارات منها :
أ- Project Navigator : وهنا نجد جميع الملفات و الأصول الخاصة بمشروعنا .
ب- Source Control Navigator : تقدر تراقب التعديلات إلي حصلت على الأكواد الخاصة بك أو عمل نسخ من الأكواد و مشاركتها مع مطورين أخرين .
ج- Symbol Navigator : أحد أجمل المميزات في Xcode وهي أن تكون لديك القدرة على مشاهدة جميع الكلاسات و الوظائف و الملفات بشكل منظم و لو عندك مشروع شغال عليه من أول هالميزة بتريحك كثير .
٢- ماهي ملفات AppDelegate – SceneDelegate – ContentView – Assets.xcassets
أ- AppDelegate :
هنا نقطة إنطلاق ( تشغيل التطبيق ) و بها تكون دورة حياة التطبيق .
didFinishLaunchingWithOptions
يتم إستدعاء الوظيفة عند إقلاع التطبيق ( تشغيل )
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true }
configurationForConnecting
يتم إستدعاء الوظيفة عند إنشاء واجهة ( عناصر ) جديدة
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) }
didDiscardSceneSessions
يتم إستدعاء الوظيفة عندما يقوم المستخدم بإغلاق النافذة عن طريق سحبها .
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. }
ب- SceneDelegate :
يتم إستدعاء الكلاس (SceneDelegate) بعد وظيفة didFinishLaunchingWithOptions .
حيث أنها مسئولة عن ما يعرض على سطح التطبيق من عناصرة واجهة المستخدم و البيانات بصفة عامة .
و الأن سنقوم بشرح الوظائف بداخلها :
willConnectTo
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). // Create the SwiftUI view that provides the window contents. let contentView = ContentView() // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: contentView) self.window = window window.makeKeyAndVisible() } }
أول وظيفة تستدعى و تقوم بإنشاء نافذة التطبيق ContentView()
sceneDidDisconnect
func sceneDidDisconnect(_ scene: UIScene) { // Called as the scene is being released by the system. // This occurs shortly after the scene enters the background, or when its session is discarded. // Release any resources associated with this scene that can be re-created the next time the scene connects. // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). }
يتم إستدعاء الوظيفة تلقائيا عند فتح تطبيق أخر على سبيل المثال بيحيث أن التطبيق يعمل في الخلفية .
sceneDidBecomeActive
func sceneDidBecomeActive(_ scene: UIScene) { // Called when the scene has moved from an inactive state to an active state. // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. }
يتم إستدعاء الوظيفة عندما يكون التطبيق في حالة عدم نشاط ( عندما تقوم بإعادة فتح التطبيق من خلفية التطبيقات ) إلى أن يكون التطبيق في حالة نشطة .
sceneWillResignActive
func sceneWillResignActive(_ scene: UIScene) { // Called when the scene will move from an active state to an inactive state. // This may occur due to temporary interruptions (ex. an incoming phone call). }
عكس الوظيفة السابقة بحيث تم إستدعاءها عندما يدخل التطبيق من حالة نشطة إلى حالة غير نشطة .
sceneWillEnterForeground
func sceneWillEnterForeground(_ scene: UIScene) { // Called as the scene transitions from the background to the foreground. // Use this method to undo the changes made on entering the background. }
تستدعى الوظيفة واجهة التطبيق عندما يكون في حالة غير نشطة الى حالة نشطة .
sceneDidEnterBackground
func sceneDidEnterBackground(_ scene: UIScene) { // Called as the scene transitions from the foreground to the background. // Use this method to save data, release shared resources, and store enough scene-specific state information // to restore the scene back to its current state. }
تقوم بعكس عمل الوظيفة السابقة .
ج- ملف ContentView
إذا تتذكر أننا شرحنا بأن SceneDelegate مسؤلة عن ما يعرض على سطح المكتب أي انها تقوم بإدارة طريقة عرض العناصر .
if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: contentView) self.window = window window.makeKeyAndVisible() }
نلاحظ في الكود أعلاه بأنه تم إنشاء Content View
وهي الوظائف و العناصر التي نقوم ببنائها و تظهر على سطح المكتب .
الأن سنقوم بتحليل كود ContentView التالي :
import SwiftUI struct ContentView: View { var body: some View { Text("Hello, World!") } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
المطورين الذين يعملون على UIKit يدركون مدى سهولة إنشاء و تطوير التطبيق عن طريق Swift UI لأنها ببساطة في الكود التالي نلاحظ أن ContentView من نوع View . و يعني أن كل شي تريد عرضه في Swift UI لابد أن يكون نوعة View .
ثانيا بداخل ContentView نجد body و بداخلة كلمة some View وهي من نوع opaque return types والتي تعني أنها من الممكن أن تعيد أي قيمة سوف تتحول إلى View
0 responses on "سلسلة تعلم Swift UI للمبتدئين - ماهي الملفات AppDelegate , SceneDelegate And ContentView"