2013-10-04 37 views
4

我试图用私人API以编程方式发送短信。我的手机没有越狱。使用CTMessageCenter发送短信(iOS 7)

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

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

+0

我也遇到过这个问题,你有没有找到方法? – alexqinbj

+0

@qlexqinbj,没有好运的伴侣。 –

+0

已经解决了吗? –

回答

1

我一直试图在iOS 6.1上这样做,因为我终于明白这个方法最终不能在iOS 6之后使用。最后一次我可以让这段代码成功执行在iOS 5上(有一个Peanut.app在网上工作)。

实际工作的是什么可以找到here和讨论here以及以下代码块。

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应用程序执行它。

2

我想你必须用E.123国际表示法编写电话号码。 因此添加加号和国家代码。对于电话号码是美国用+1替换前导0:

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

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

UPDATE

我的iOS 7下测试的老工作的iOS 5代码... sendSMSWithText:serviceCenter:toAddress:回报NO。 同时采用了新的方法sendSMSWithText:serviceCenter:toAddress:withMoreToFollow:

帕纳约蒂斯的建议,同样的似乎是正确的: -/

更新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). 

在非越狱的设备。

+0

嗨@AppsolutEinfach我的iOS8越狱设备需要添加这两个权利? – ximmyxiao

+0

@ximmyxiao我们对越狱设备没有任何经验。 – AppsolutEinfach