我正在使用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头。因此,建议我实际需要做的事情。

最佳答案

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

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

没问题,该应用程序将在此之后构建并运行。

关于ios - 如何解决xpc.h未找到错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24013814/

10-10 20:21