> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# LynxService API

APISummary
## Introduction

Lynx Service provides Lynx Engine with host-specific capabilities such as image handling, logging, and support for the [Fetch API](/next/guide/interaction/networking.md#using-fetch).
Host apps have different requirements for the libraries they use. To accommodate these differences, Lynx Engine exposes the LynxService API so that host apps can provide their own service implementations.

## Overall Design

Lynx Engine and Lynx Service are decoupled. Lynx Service implements the LynxService API exposed by Lynx Engine. Lynx Engine does not depend on a particular service implementation. At runtime, Lynx Engine uses its service lookup mechanism to find registered services and calls them through the LynxService API.

## LynxService API

Lynx Engine provides the LynxService API, which defines the capabilities of Lynx Service. Service implementations must meet the API's requirements so that Lynx Engine can use these capabilities correctly.

## 1. Quick Access

Lynx provides a default implementation of Lynx Service that you can [integrate directly](/next/guide/start/integrate-with-existing-apps.md).

## 2. Lynx Service Customization

If your app has custom requirements, you can also implement and register your own service. Using the Log Service as an example, the following steps show how to implement and register a custom Lynx Service:

### Interface Implementation

**iOS**

- 1. Implement `LynxServiceLogProtocol`

```objective-c title="YourLogService.h"
#import <Lynx/LynxService.h>
#import <Lynx/LynxServiceLogProtocol.h>

// Implement LynxServiceLogProtocol
@interface YourLogService : NSObject <LynxServiceLogProtocol>
@end
```

- 2. Use the `LynxServiceRegister` to achieve automatic registration.
- 3. Implement `serviceBizID` and `serviceScope`. These two methods have no practical significance and will be removed later.
- 4. Implement `serviceType`. It is required to return the type implemented by this Service. See LynxService.h for details.
- 5. (Recommended) Implement `sharedInstance` to make it a singleton class. Based on the current Lynx Service design, only one instance of a certain type of Service will be used globally. It is recommended to implement it as a singleton class.
- 6. Implement all the interfaces required by `LynxServiceLogProtocol`.

```objective-c title="YourLogService.mm"
#import "YourLogService.h"

[[maybe_unused]] void logWrite(unsigned int level, const char *tag, const char *format) {
  if (format == NULL) {
    return;
  }
  NSLog(@"[%s] %s", tag == NULL ? "" : tag, format);
}

// Use the LynxServiceRegister macro to achieve automatic registration
@LynxServiceRegister(LynxLogService);

// Implement LynxLogService
@implementation LynxLogService

// Irrelevant property, will be removed later
+ (NSString*)serviceBizID {
  return DEFAULT_LYNX_SERVICE;
}

// Irrelevant property, will be removed later
+ (LynxServiceScope)serviceScope {
  return LynxServiceScopeDefault;
}

// Return the corresponding service type for runtime lookup
+ (NSUInteger)serviceType {
  return kLynxServiceLog;
}

// Recommended: Implement as a singleton
+ (instancetype)sharedInstance {
  static dispatch_once_t onceToken;
  static YourLogService *logService;
  dispatch_once(&onceToken, ^{
    logService = [[YourLogService alloc] init];
  });
  return logService;
}

// Implement all the interfaces required by LynxServiceLogProtocol
- (void *)getWriteFunction {
  return (void *)logWrite;
}

@end
```

**Android**

Implement `ILynxLogService`:

```kotlin title="YourLogService.kt"
object YourLogService : ILynxLogService {
  private var logOutputChannel: LogOutputChannelType = LogOutputChannelType.Platform

  override fun logByPlatform(
    level: Int,
    tag: String,
    msg: String,
  ) {
    // Implemention
  }

  override fun isLogOutputByPlatform(): Boolean = logOutputChannel == LogOutputChannelType.Platform

  override fun getDefaultWriteFunction(): Long = 0

  override fun switchLogToSystem(enableSystemLog: Boolean) {}

  override fun getLogToSystemStatus(): Boolean = false
}
```

**Harmony**

Implement `ILynxLogService`:

```typescript title="YourLogService.ets"
import { ILynxLogService } from '@lynx/lynx';

export class YourLogService implements ILynxLogService {
  static instance = new YourLogService();

  logByPlatform(level: number, tag: string, message: string): void {
    console.log(`level: ${level} tag: ${tag} message: ${message}`);
  }

  isLogOutputByPlatform(): boolean {
    return true;
  }

  getDefaultWriteFunction(): number {
    return 0;
  }
}
```

**Desktop C++**

Implement a concrete Desktop service class. The following example uses `LynxHttpService`; `LynxSecurityService` and `LynxEventReporterService` follow the same pattern and override their own abstract entrypoints.

```cpp title="YourHttpService.h"
#include "lynx/platform/embedder/public/lynx_http_service.h"

class YourHttpService : public lynx::pub::LynxHttpService {
 public:
  void Request(std::shared_ptr<lynx::pub::LynxHttpRequest> request,
               std::shared_ptr<lynx::pub::LynxHttpResponse> response) override {
    response->SetStatusCode(200);
    response->SetStatusText("OK");
    response->Complete();
  }
};
```

Use the corresponding abstract class for each service type:

- `LynxHttpService`: override `Request(...)`
- `LynxSecurityService`: override `VerifyTASM(...)`
- `LynxEventReporterService`: override `OnEvent(...)`


### Service Register

**iOS**

Lynx Service has an automatic registration mechanism in iOS. In the implementation stage, the `LynxServiceRegister` macro has been used to complete the quick automatic registration.

If you use your own implementation of Lynx Service, please delete the [default implementation](/next/guide/start/integrate-with-existing-apps.md?platform=ios) provided by Lynx.

```diff title="Podfile" {1-5}
pod 'LynxService', '4.2.0', :subspecs => [
      'Image',
-     'Log',
      'Http',
  ]
```

**Android**

Replace the implementation of log service in `LynxServiceCenter` in the [integration documentation](/next/guide/start/integrate-with-existing-apps.md?platform=android) with your implementation.

```diff
- LynxServiceCenter.inst().registerService(ILynxLogService::class.java, LynxLogService)
+ LynxServiceCenter.inst().registerService(ILynxLogService::class.java, YourLogService)
```

And delete related dependencies

```diff title="build.gradle"
dependencies {
-   // integrating log-service
-   implementation "org.lynxsdk.lynx:lynx-service-log:4.2.0"
}
```

**Harmony**

Replace the implementation of log service in `LynxServiceCenter` in the [integration documentation](/next/guide/start/integrate-with-existing-apps.md?platform=harmony) with your implementation.

```diff
- LynxServiceCenter.registerService(LynxServiceType.Log, LynxLogService.instance);
+ LynxServiceCenter.registerService(LynxServiceType.Log, YourLogService.instance);
```

And delete related dependencies

```diff title="oh-package.json5"
"dependencies": {
- "@lynx/lynx_log_service": "4.2.0",
}
```

**Desktop C++**

Register the implemented service through `LynxServiceCenter`:

```cpp
auto service = std::make_shared<YourHttpService>();
lynx::pub::LynxServiceCenter::GetInstance().RegisterService(service);
```

The registered service type is determined by the concrete service class:

- `LynxHttpService` -> `kServiceTypeHttp`
- `LynxSecurityService` -> `kServiceTypeSecurity`
- `LynxEventReporterService` -> `kServiceTypeEventReporter`


**Compatibility Table**

**Query:** `lynx-native-api.lynx-service.lynx-service`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-native-api/lynx-service/lynx-service.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| lynx-service | Yes (2.8) | Yes (3.4) | Yes (2.8) | Yes (3.7) | Yes (3.7) | No |

**`lynx-service`**

LynxService API

**Specifications:**

