相关疑难解决方法(0)

使用XPC与另一个应用程序通信

我有一个窗口应用程序,并添加一些功能,我需要另一个应用程序,它在登录时启动并将数据同步到服务器(如果可用).

我尝试过使用NSDistributionNotification但它在沙盒应用程序中几乎没用.我查了一下XPC,希望它可以工作,但我不知道如何让它与助手一起工作.到目前为止,我已经使用XPC做到了这一点.

主要应用

    NSXPCInterface *remoteInterface = [NSXPCInterface interfaceWithProtocol:@protocol(AddProtocol)];
    NSXPCConnection *xpcConnection = [[NSXPCConnection alloc] initWithServiceName:@"com.example.SampleService"];

    xpcConnection.remoteObjectInterface = remoteInterface;

    xpcConnection.interruptionHandler = ^{
        NSLog(@"Connection Terminated");
    };

    xpcConnection.invalidationHandler = ^{
        NSLog(@"Connection Invalidated");
    };

    [xpcConnection resume];

    NSInteger num1 = [_number1Input.stringValue integerValue];
    NSInteger num2 = [_number2Input.stringValue integerValue];

    [xpcConnection.remoteObjectProxy add:num1 to:num2 reply:^(NSInteger result) {
        NSLog(@"Result of %d + %d = %d", (int) num1, (int) num2, (int) result);
    }];
Run Code Online (Sandbox Code Playgroud)

XPC服务

In main () ...
SampleListener *delegate = [[SampleListener alloc] init];
NSXPCListener *listener = [NSXPCListener serviceListener];
listener.delegate = delegate; …
Run Code Online (Sandbox Code Playgroud)

macos cocoa xpc helper

15
推荐指数
3
解决办法
7987
查看次数

标签 统计

cocoa ×1

helper ×1

macos ×1

xpc ×1