先介紹一種最簡單的方法:
調用打電話功能
[[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以上才支持程序內發送短信
}
}