لتحويل سلسلة نصية بتنسيق HH:MM:SS إلى NSDate في Objective-C، يجب تعيين تنسيق السلسلة النصية لمطابقة التنسيق الذي تريده في NSDateFormatter. في الشفرة التي قدمتها، تم استخدام تنسيق الساعة بتنسيق صغير “hh” بينما يجب أن يكون “HH” للساعات بتنسيق 24 ساعة. هذا هو السبب في الحصول على قيمة Null.
بالنسبة لتحويل سلسلة النصية “21:00:00” إلى NSDate، يمكنك استخدام الشفرة التالية:
objectiveNSString *datestr = @"21:00:00"; NSDateFormatter *dateformat = [[NSDateFormatter alloc] init]; [dateformat setDateFormat:@"HH:mm:ss"]; NSDate *date = [dateformat dateFromString:datestr]; NSLog(@"time is %@", date);
بعد ذلك، بمجرد تحويل السلسلة النصية إلى NSDate، يمكنك استخدام هذا التاريخ لإعداد التنبيه المحلي بشكل يومي. يمكنك استخدام NSDateComponents لتحديد الوقت بدقة. على سبيل المثال، لتعيين تنبيه محلي يتم تشغيله كل يوم عند الساعة 21:00، يمكنك استخدام الشفرة التالية:
objective// Create a date component with the desired time NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date]; NSInteger hour = [components hour]; NSInteger minute = [components minute]; NSDateComponents *notificationComponents = [[NSDateComponents alloc] init]; notificationComponents.hour = hour; notificationComponents.minute = minute; // Create a notification trigger with the daily repeating time UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:notificationComponents repeats:YES]; // Create a notification content UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = @"Your Notification Title"; content.body = @"Your Notification Body"; // Create a notification request UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"YourNotificationIdentifier" content:content trigger:trigger]; // Add the notification request to the notification center UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"Error scheduling local notification: %@", error); } else { NSLog(@"Local notification scheduled successfully."); } }];
تأكد من استيراد مكتبة UserNotifications لاستخدام UNUserNotificationCenter. هذا الشيء يجعلك قادرًا على جدولة التنبيهات المحلية في iOS.
وبهذا، يمكنك تحويل سلسلة النصية إلى NSDate بالتنسيق الصحيح وتعيين تنبيه محلي يتكرر يوميًا في الوقت المحدد.
المزيد من المعلومات
بالطبع، إليك إكمال المقال:
بعد أن قمت بتحويل سلسلة النصية إلى NSDate بالتنسيق الصحيح وتحديد الوقت الذي ترغب في إعداد التنبيه المحلي عنده، يمكنك الآن إنشاء التنبيه المحلي باستخدام إطار UserNotifications المتاح في iOS.
أولاً، يجب عليك تحديد المحتوى الذي ترغب في عرضه في التنبيه المحلي. يمكنك تحديد عنوان ونص للتنبيه كما هو موضح في الشفرة التالية:
objective// Create a notification content UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = @"Your Notification Title"; content.body = @"Your Notification Body";
ثم، يجب عليك إنشاء مكونات التاريخ المطلوبة لإعداد تكرار التنبيه المحلي بشكل يومي، وذلك باستخدام NSDateComponents كما هو موضح في الشفرة التالية:
objective// Create a date component with the desired time NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date]; NSInteger hour = [components hour]; NSInteger minute = [components minute]; NSDateComponents *notificationComponents = [[NSDateComponents alloc] init]; notificationComponents.hour = hour; notificationComponents.minute = minute;
بعد ذلك، يمكنك إنشاء مُشغّل للتنبيه المحلي بشكل يومي باستخدام UNCalendarNotificationTrigger كما هو موضح في الشفرة التالية:
objective// Create a notification trigger with the daily repeating time UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:notificationComponents repeats:YES];
ثم، يجب عليك إنشاء طلب للتنبيه المحلي بشكل يومي باستخدام UNNotificationRequest كما هو موضح في الشفرة التالية:
objective// Create a notification request UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"YourNotificationIdentifier" content:content trigger:trigger];
أخيرًا، يجب عليك إضافة طلب التنبيه المحلي إلى مركز الإشعارات باستخدام UNUserNotificationCenter كما هو موضح في الشفرة التالية:
objective// Add the notification request to the notification center UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"Error scheduling local notification: %@", error); } else { NSLog(@"Local notification scheduled successfully."); } }];
باستخدام هذه الشفرة، يمكنك تحويل سلسلة النصية إلى NSDate بالتنسيق الصحيح وإعداد تنبيه محلي يتكرر يوميًا في الوقت المحدد. يمكنك تخصيص عناصر التنبيه وفقًا لاحتياجاتك، مثل تغيير العنوان والنص أو إضافة أصوات تنبيه إضافية.