使用CTMessageCenter发送短信(iOS 7)

我正在尝试使用私有API以编程方式发送短信。 我的手机没有越狱。

BOOL success = [[CTMessageCenter sharedMessageCenter] sendSMSWithText:@"test 1234..." serviceCenter:nil toAddress:@"0777888888"]; if(success){ NSLog(@"Message SENT"); }else{ NSLog(@"Message not SENT"); } 

此代码始终打印“消息不发送”。 谁能帮我 ?

我想,你必须用E.123国际表示法来写电话号码。 所以加上加号和国家代码。 对于电话号码是美国取代领先的0与+1:

 [[CTMessageCenter sharedMessageCenter] sendSMSWithText:@"test 1234..." serviceCenter:nil toAddress:@"+1777888888"]; 

对于电话号码,斯里兰卡使用适当的国家代码+94。

UPDATE

我已经在iOS 7下testing了旧版iOS 5的代码… sendSMSWithText:serviceCenter:toAddress: returns NO 。 同时使用新方法sendSMSWithText:serviceCenter:toAddress:withMoreToFollow:

Panagiotis的build议似乎是正确的: – /

更新2

https://stackoverflow.com/a/20425853/2270880给出正确的答案。

在iOS 7下,应用程序需要两个授权com.apple.CommCenter.Messages-sendcom.apple.coretelephony.Identity.get 。 通过文件appname.entitlements (并在目标的“生成设置”>“所有”>“代码签名”>“代码签名授权”中设置)添加其他权利会导致错误

 The entitlements specified in your application's Code Signing Entitlements file do not match those specified in your provisioning profile. (0xE8008016). 

在非越狱设备上。

我一直试图在iOS 6.1上这样做,因为我最终发现这种方法不能在iOS 6以后使用。最后一次,我可以使这个代码成功执行在iOS 5上(有一个Peanut.appnetworking工作)。

实际工作是什么可以在这里find和讨论以及以下代码块。

 dispatch_queue_t queue = dispatch_queue_create("com.apple.chatkit.clientcomposeserver.xpc_connection_queue", DISPATCH_QUEUE_SERIAL); xpc_connection_t connection = xpc_connection_create_mach_service("com.apple.chatkit.clientcomposeserver.xpc", queue, 0); xpc_connection_set_event_handler(connection, ^(xpc_object_t){}); xpc_connection_resume(connection); dispatch_release(queue); xpc_object_t dictionary = xpc_dictionary_create(0, 0, 0); xpc_dictionary_set_int64(dictionary, "message-type", 0); NSData* recipients = [NSPropertyListSerialization dataWithPropertyList:[NSArray arrayWithObject:@"12212"] format:NSPropertyListBinaryFormat_v1_0 options:0 error:NULL]; xpc_dictionary_set_data(dictionary, "recipients", recipients.bytes, recipients.length); xpc_dictionary_set_string(dictionary, "markup", "SMS text"); xpc_connection_send_message(connection, dictionary); xpc_release(dictionary); 

虽然没有尝试过在非越狱iOS上实现。 我希望你能做到!

**编辑

让我纠正自己! 你的代码可以使用imagent可执行文件进行越狱调整。 只是不能直接从xCode应用程序执行它。