2013-10-04 30 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