`
lizhuang
  • 浏览: 884487 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

iOS调用系统打电话,发短信功能

 
阅读更多
先介绍一种最简单的方法:

调用打电话功能

[[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 = [MFMessageComposeViewController canSendText];

      NSLog(@"can send SMS [%d]",canSendSMS);

      if (canSendSMS) {

    

         MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

         picker.messageComposeDelegate = self;

         picker.navigationBar.tintColor = [UIColor blackColor];

         picker.body = @"test";

         picker.recipients = [NSArray arrayWithObject:@"10086"];

         [self presentModalViewController:picker animated:YES];

         [picker release];  

      }

}

给一个人发短信:
从网页上获得内容

-(void)displaySMSComposerSheet

{

    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

    picker.messageComposeDelegate = self;

    UIWebView *web = nil;

    NSMutableString* absUrl = [[NSMutableString alloc] initWithString:web.request.URL.absoluteString];

    [absUrl replaceOccurrencesOfString:@"http://i.aizheke.com" withString:@"http://m.aizheke.com" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [absUrl length])];

    picker.body=[NSString stringWithFormat:@"我在爱折客上看到:%@ 可能对你有用,推荐给你!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以上才支持程序内发送短信

    }

}

以上内容有一部分是来各个网站,本人自己加上自己的理解,整理,至于来源于互联网的那一部分作者是谁,来自于哪里,我现在也不知道了,写出来供大家学习。如果有你是那一部分的作者,请联系我!

Restrictions

If you could send an SMS within a program on the iPhone, you'll be able to write games that spam people in the background. I'm sure you really want to have spams from your friends, "Try out this new game! It roxxers my boxxers, and yours will be too! roxxersboxxers.com!!!! If you sign up now you'll get 3,200 RB points!!"

Apple has restrictions for automated (or even partially automated) SMS and dialing operations. (Imagine if the game instead dialed 911 at a particular time of day)

Your best bet is to setup an intermediate server on the internet that uses an online SMS sending service, and send the SMSs via that route if you need complete automation. (ie, your program on the iPhone sends a UDP packet to your server, which sends the real SMS)

iOS 4 Update

iOS 4, however, now provides a viewcontroller you can import into your application. You prepopulate the SMS fields, then the user can initiate the SMS send within the controller. Unlike using the "sms:..." url format, this allows your application to stay open, and allows you to populate both the to and the body fields. You can even specify multiple recipients.

This prevents applications from sending automated SMS without the user explicitly aware of it. You still cannot send fully automated SMS from the iPhone itself, it requires some user interaction. But this at least allows you to populate everything, and avoids closing the application.

The MFMessageComposeViewController class is well documented, and tutorials show how easy it is to implement.

iOS 5 Update

iOS 5 includes messaging for iPod touch and iPad devices, so while I've not yet tested this myself, it may be that all iOS devices will be able to send SMS via MFMessageComposeViewController. If this is the case, then Apple is running an SMS server that sends messages on behalf of devices that don't have a cellular modem.

iOS 6 Update

No changes to this class.

iOS 7 Update

You can now check to see if the message medium you are using will accept a subject or attachments, and what kind of attachments it will accept. You can edit the subject and add attachments to the message, where the medium allows it.

Limitations to this class

Keep in mind that this won't work on phones without iOS 4, and it won't work on the iPod touch or the iPad, except, perhaps, under iOS 5. You must either detect the device and iOS limitations prior to using this controller, or risk restricting your app to recently upgraded 3G, 3GS, and 4 iPhones.

However, an intermediate server that sends SMS will allow any and all of these iOS devices to send SMS as long as they have internet access, so it may still be a better solution for many applications. Alternately, use both, and only fall back to an online SMS service when the device doesn't support it.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics