使用CTMessageCenter发送短信(iOS 7)

4

我正在尝试使用私有API程序化地发送短信。我的手机没有越狱。

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

这段代码总是打印出“消息未发送”。有人能帮我吗?

我也遇到了这个问题,你找到解决方法了吗? - alexqinbj
@qlexqinbj,没戏兄弟。 - Thilina Chamath Hewagama
已经解决了吗? - Bagusflyer
2个回答

3

我猜想您需要按照国际标准E.123的格式书写电话号码。因此,要添加加号和国家代码。如果是美国的电话号码,请将前导0替换为+1:

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

如果要拨打斯里兰卡的电话号码,请使用适当的国家代码+94。

更新

我已经在iOS 7下测试了旧的可工作的iOS 5代码... sendSMSWithText:serviceCenter:toAddress: 返回NO。 而在使用新方法sendSMSWithText:serviceCenter:toAddress:withMoreToFollow:时,也是一样的。

Panagiotis的建议似乎是正确的 :-/

更新2

https://dev59.com/dGUo5IYBdhLWcg3wpg0f#20425853 给出了正确的答案。

在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).

在非越狱设备上。

嗨@AppsolutEinfach,我的iOS8越狱设备需要添加这两个权限吗? - ximmyxiao
@ximmyxiao 我们没有使用越狱设备的经验。 - AppsolutEinfach

1
我一直在尝试在iOS 6.1上实现这个功能,但最终发现自iOS 6以来无法使用此方法。上一次成功执行此代码的时间是在iOS 5上(网上有一个名为Peanut.app的应用程序可以工作)。
实际有效的方法可以在这里找到,并且还可以在这里讨论,以下是相应的代码块。
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可执行文件的越狱tweak上运行。只是无法直接从xCode应用程序执行它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接