是否有人知道有可能以及如何經過官方的SDK / Cocoa Touch經過iPhone
以編程方式發送SMS ? 編程
用這個: app
- (void)showSMSPicker { Class messageClass = (NSClassFromString(@"MFMessageComposeViewController")); if (messageClass != nil) { // Check whether the current device is configured for sending SMS messages if ([messageClass canSendText]) { [self displaySMSComposerSheet]; } } } - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { //feedbackMsg.hidden = NO; // Notifies users about errors associated with the interface switch (result) { case MessageComposeResultCancelled: { UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending canceled!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert1 show]; [alert1 release]; } // feedbackMsg.text = @"Result: SMS sending canceled"; break; case MessageComposeResultSent: { UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert2 show]; [alert2 release]; } // feedbackMsg.text = @"Result: SMS sent"; break; case MessageComposeResultFailed: { UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending failed!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert3 show]; [alert3 release]; } // feedbackMsg.text = @"Result: SMS sending failed"; break; default: { UIAlertView *alert4 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS not sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert4 show]; [alert4 release]; } // feedbackMsg.text = @"Result: SMS not sent"; break; } [self dismissModalViewControllerAnimated: YES]; }
//Add the Framework in .h file #import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h> //Set the delegate methods UIViewController<UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate> //add the below code in .m file - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease]; if([MFMessageComposeViewController canSendText]) { NSString *str= @"Hello"; controller.body = str; controller.recipients = [NSArray arrayWithObjects: @"", nil]; controller.delegate = self; [self presentModalViewController:controller animated:YES]; } } - (void)messageComposeViewController: (MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { switch (result) { case MessageComposeResultCancelled: NSLog(@"Cancelled"); break; case MessageComposeResultFailed: NSLog(@"Failed"); break; case MessageComposeResultSent: break; default: break; } [self dismissModalViewControllerAnimated:YES]; }
XPC是MacOS中進程間通訊的系統之一。 此係統層已開發出來,用於基於使用libSystem的plist結構的傳輸進行進程間通訊,並已啓動。 實際上,它是一個界面,容許經過交換字典等結構來管理流程。 因爲遺傳緣由,iOS 5也具備此機制。 框架
您可能已經瞭解了此介紹的意思。 是的,iOS中有一些系統服務,其中包括用於XPC通訊的工具。 我想用一個用於發送SMS的守護程序來舉例說明該工做。 可是,應該指出的是,此功能在iOS 6中已修復,但與iOS 5.0-5.1.1有關。 無需使用越獄,專用框架和其餘非法工具便可對其進行利用。 僅須要目錄/ usr / include / xpc / *中的頭文件集。 工具
在iOS中發送SMS的元素之一是系統服務com.apple.chatkit,其任務包括生成,管理和發送短文本消息。 爲了便於控制,它具備公共可用的通訊端口com.apple.chatkit.clientcomposeserver.xpc。 使用XPC子系統,您能夠在未經用戶批准的狀況下生成和發送消息。 ui
好吧,讓咱們嘗試建立一個鏈接。 this
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配置能夠建立掛起的鏈接-咱們須要採起進一步的步驟來進行激活。 spa
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 a message event!"); }); xpc_connection_resume(myConnection);
鏈接已激活。 此時此刻,iOS 6將在電話日誌中顯示一條消息,指示禁止這種通訊。 如今,咱們須要生成一個相似於xpc_dictionary的字典,其中包含消息發送所需的數據。 日誌
NSArray *recipient = [NSArray arrayWithObjects:@"+7 (90*) 000-00-00", nil]; NSData *ser_rec = [NSPropertyListSerialization dataWithPropertyList:recipient 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端口並確保已傳遞。 code
xpc_connection_send_message(myConnection, mydict); xpc_connection_send_barrier(myConnection, ^{ NSLog(@"The message has been successfully delivered"); });
就這樣。 短信已發送。 orm
添加MessageUI.Framework並使用如下代碼
#import <MessageUI/MessageUI.h>
而後:
if ([MFMessageComposeViewController canSendText]) { MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc] init]; NSString *message = @"Your Message here"; [messageComposer setBody:message]; messageComposer.messageComposeDelegate = self; [self presentViewController:messageComposer animated:YES completion:nil]; }
和委託方法-
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { [self dismissViewControllerAnimated:YES completion:nil]; }
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; UIImage *ui =resultimg.image; pasteboard.image = ui; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]]; }