The type or namespace name ‘iOSBackgroundService’ could not be found

Error Message : The type or namespace name ‘iOSBackgroundService’ could not be found

I have created new class named iOSBackgroundService in Platform -> iOS. But I cannot inject it in the maui program.

#elif IOS
            builder.Services.AddSingleton<IBackgroundService, iOSBackgroundService>();
#endif

And I cannot create object like this that leads to same message.

var iOSBck = new iOSBackgroundService();

But When I create object (or inject) of the default AppDelegate class that placed in the same directory (Platform->iOS). That doesn’t show any error.

Actually I haven’t any idea of it. Any help is appreciated.

This is full code of the mauiprogram.cs for your reference.

**using Microsoft.Extensions.Logging;
using Plugin.Fingerprint.Abstractions;
using Plugin.Fingerprint;
using Plugin.Maui.Biometric;
using HR_Attendance_MAUI.Services;

#if ANDROID
using HR_Attendance_MAUI.Platforms.Android.Services;
#elif IOS
using HR_Attendance_MAUI.Platforms.iOS; 
#endif


namespace HR_Attendance_MAUI
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });
            builder.Services.AddSingleton<MainPage>();
            builder.Services.AddSingleton<FinPrintReg_Page>();
            builder.Services.AddSingleton(typeof(IFingerprint), CrossFingerprint.Current);

#if DEBUG
            builder.Logging.AddDebug();
#endif
#if ANDROID
        builder.Services.AddSingleton<IBackgroundService, AndroidBackgroundService>();
#elif IOS
            builder.Services.AddSingleton<IBackgroundService, iOSBackgroundService>();
#endif

            return builder.Build();
        }
    }


}**

And this is the code of iOSBackgroundService

using Foundation;
using UIKit;
using HR_Attendance_MAUI.Services;
using HR_Attendance_MAUI.Data;
using HR_Attendance_MAUI.Model;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.IO;
using Microsoft.Maui.Controls;
using HR_Attendance_MAUI.Platforms.iOS.Services;

[assembly: Dependency(typeof(iOSBackgroundService))]

namespace HR_Attendance_MAUI.Platforms.iOS.Services
{
    [Register("iOSBackgroundService")]
    public class iOSBackgroundService : NSObject, IBackgroundService
    {
        private nint _taskId;
        private Timer _timer;
        private AttendanceDatabaseService2 _attendanceDatabaseService;
        private readonly TimeSpan _delay = TimeSpan.FromMinutes(5);

        public static bool IsForegroundServiceRunning;

        public void Start()
        {
            IsForegroundServiceRunning = true;
            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("BackgroundService", OnExpiration);

            Task.Run(async () =>
            {
                while (IsForegroundServiceRunning)
                {
                    var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AttendanceDB.db3");
                    _attendanceDatabaseService = new AttendanceDatabaseService2(dbPath);
                    List<AttendanceData> attendanceDataList = _attendanceDatabaseService.GetAllAttendances();

                    if (attendanceDataList.Count == 0)
                    {
                        Stop();
                    }

                    var networkAccess = Connectivity.NetworkAccess;
                    if (networkAccess == NetworkAccess.Internet)
                    {
                        bool result = await SyncAttendanceData();
                    }

                    Console.WriteLine("Background Service is Running");
                    await Task.Delay(_delay);
                }

                UIApplication.SharedApplication.EndBackgroundTask(_taskId);
                _taskId = UIApplication.BackgroundTaskInvalid;
            });
        }

        public void Stop()
        {
            IsForegroundServiceRunning = false;
            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }

        public bool IsForeGroundServiceRunning()
        {
            return IsForegroundServiceRunning;
        }
        private void OnExpiration()
        {
            Stop();
        }

        private async Task<bool> SyncAttendanceData()
        {
            var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AttendanceDB.db3");
            _attendanceDatabaseService = new AttendanceDatabaseService2(dbPath);
            List<AttendanceData> attendanceDataList = _attendanceDatabaseService.GetAllAttendances();

            if (attendanceDataList.Count == 0)
            {
                return false;
            }

            foreach (AttendanceData attendanceData in attendanceDataList)
            {
                string inTimeDate = attendanceData.inTimeDate;
                string inTimeDate1 = attendanceData.outTime;
                string inTimeDate2 = attendanceData.lonOut;
                string inTimeDate3 = attendanceData.latOut;
            }

            DateTimeOffset currentTime1 = DateTimeOffset.Now;
            DateTime currentTime = currentTime1.DateTime;
            currentTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 0, 0, 0);
            string currentDate = currentTime.ToString("yyyy-MM-dd HH:mm:ss.fff");

            HttpClient client;
            try
            {
                client = HttpClientFactory.CreateHttpClient();
                var response = await client.PostAsJsonAsync("api/Attendance/StoreList", attendanceDataList);

                if (response.IsSuccessStatusCode)
                {
                    _attendanceDatabaseService.StoreTodaysRecordsAndDeleteAll(currentDate);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                await App.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
                return false;
            }
        }
    }
}

Here is the IBackgroundService interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HR_Attendance_MAUI.Services
{
    public interface IBackgroundService
    {
        void Start();
        void Stop();

        bool IsForeGroundServiceRunning();
    }
}

6

Error Message : The type or namespace name ‘iOSBackgroundService’
could not be found

According to the error log, you can fix it by changing the class name from HR_Attendance_MAUI.Platforms.iOS.Services to HR_Attendance_MAUI.Platforms.iOS under Platform -> iOS.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật