3

我正在开发一个 iOS 应用程序,在这个应用程序中,我在内部发送 SMS,无需用户参与。谷歌搜索后,我找到了答案,我正在使用这段代码。

xpc_connection_t myconnection;

dispatch_queue_t queue = dispatch_queue_create("com.apple.chatkit.clientcomposeserver.xpc", DISPATCH_QUEUE_CONCURRENT);

myconnection = xpc_connection_create_mach_service("com.apple.chatkit.clientcomposeserver.xpc", queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);

现在我们有了 XPC 连接 myconnection 到 SMS 发送服务。然而,XPC 配置提供了暂停连接的创建——我们需要再采取一步来激活。

xpc_connection_set_event_handler(myconnection, ^(xpc_object_t event){
xpc_type_t xtype = xpc_get_type(event);
if(XPC_TYPE_ERROR == xtype)
{
NSLog(@"XPC sandbox connection error: %s\n", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
}
// Always set an event handler. More on this later.

NSLog(@"Received an message event!");

});

xpc_connection_resume(myconnection);

连接已激活。就在此时,iOS 6 将在电话日志中显示一条消息,禁止此类通信。现在我们需要生成一个类似于 xpc_dictionary 的字典,其中包含消息发送所需的数据。

NSArray *receipements = [NSArray arrayWithObjects:@"+7 (90*) 000-00-00", nil];

NSData *ser_rec = [NSPropertyListSerialization dataWithPropertyList:receipements format:200 options:0 error:NULL];

xpc_object_t mydict = xpc_dictionary_create(0, 0, 0);
xpc_dictionary_set_int64(mydict, "message-type", 0);
xpc_dictionary_set_data(mydict, "recipients", [ser_rec bytes], [ser_rec length]);
xpc_dictionary_set_string(mydict, "text", "hello from your application!");

所剩无几:将消息发送到 XPC 端口并确保它已送达。

xpc_connection_send_message(myconnection, mydict);
xpc_connection_send_barrier(myconnection, ^{
NSLog(@"Message has been successfully delievered");
});

但是为了使用这个代码,我必须添加 xpc.h 头文件,但是找不到 xpc.h 头文件。所以,建议我实际上需要做什么。

在此处输入图像描述

4

2 回答 2

2

您在 iOS 中使用 XPC,默认情况下不会出现标题。我从https://github.com/realthunder/mac-headers获得了 /usr/include ——它包括 xpc.h 和相关的头文件。我从中取出 /xpc 子目录并将其仅添加到我的项目中,因为添加完整的 /usr/include 会干扰我现有的 /usr/include 并导致编译时出现架构错误。

即使在添加 /xpc 子目录并包含 xpc.h 之后,由于嵌套包含问题,我也无法使包含路径正常工作。浪费了大量时间试图获得正确的解决方案,然后使用编辑 xpc.h 和 base.h 的 kludgy 解决方案,以便嵌套的 xpc 包含不使用任何路径。因此,#include <xpc/base.h>被编辑为#include "base.h"等。

那行得通,应用程序在此之后构建并运行。

于 2015-03-30T19:43:28.583 回答
-1

库路径错误使用这个...

#include /usr/include/xpc/xpc.h

于 2014-09-27T21:29:41.100 回答