先介紹一種最簡單的方法:
調用打電話功能
[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
調用發簡訊功能
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10000"]];
上面的發簡訊的功能是調用系統的介面,下面是實現一種點擊按鍵就直接傳送簡訊,相當於後台發送,能不能上軟體商店,還不能確定。相對建議來說,盡量使用第一種。
首先匯入MFMessageComposeViewControllerDelegate這個代理,實現裡面的方法
-(void)messageComposeViewController:(MFMessageComposeViewController *)controllerdidFinishWithResult:(MessageComposeResult)result {
//Notifies users about errors associated with the interface
switch (result) {
case MessageComposeResultCancelled:
if (DEBUG)NSLog(@"Result: canceled");
break;
case MessageComposeResultSent:
if (DEBUG)NSLog(@"Result: Sent");
break;
case MessageComposeResultFailed:
if (DEBUG)NSLog(@"Result: Failed");
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
群發簡訊:
- (IBAction)sendSMS {
BOOL canSendSMS = [MFMessageComposeViewControllercanSendText];
NSLog(@"can send SMS [%d]",canSendSMS);
if (canSendSMS) {
MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc] init];
picker.messageComposeDelegate =self;
picker.navigationBar.tintColor = [UIColorblackColor];
picker.body = @"test";
picker.recipients = [NSArrayarrayWithObject:@"10086"];
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
給一個人發簡訊:
從網頁上獲得內容
-(void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewControlleralloc] init];
picker.messageComposeDelegate =self;
UIWebView *web = nil;
NSMutableString* absUrl = [[NSMutableStringalloc] initWithString:web.request.URL.absoluteString];
[absUrl replaceOccurrencesOfString:@"http://i.aizheke.com"withString:@"http://m.aizheke.com"options:NSCaseInsensitiveSearchrange:NSMakeRange(0, [absUrllength])];
picker.body=[NSStringstringWithFormat:@"我在愛折客上看到:%@可能對你有用,推薦給你!link:%@",[webstringByEvaluatingJavaScriptFromString:@"document.title"],absUrl];
[absUrl release];
[self presentModalViewController:picker animated:YES];
[picker release];
}
事件綁定傳送簡訊
-(IBAction)showSMSPicker:(id)sender {
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
else {
//裝置沒有簡訊功能
}
}
else {
// iOS版本過低,iOS4.0以上才支援程式內傳送簡訊
}
}