开发者

How to Create SMS application in iPhone

开发者 https://www.devze.com 2022-12-20 03:49 出处:网络
I am new to iphone development, i want to create SMS application in my application.I have created mail application using \"messageUI.framework\".Is there any 开发者_开发技巧framework for creating SMS

I am new to iphone development, i want to create SMS application in my application.I have created mail application using "messageUI.framework".Is there any 开发者_开发技巧framework for creating SMS application.I have no idea of it, so tell me the way of approaching this task. Please guide me to achieve my task.Please help me out.Thanks.


Unfortunately, there is no built-in view controller for sending SMS like MFMailComposeViewController for email.

As of iOS 4.0, you can use the MFMessageComposeViewController, a counterpart to the email-only MFMailComposeViewController. This lets you lay out and send an SMS message.

Additionally, you can use the SMS URL scheme, like is decribed in this question. However, it appears that you cannot prepopulate the body of an SMS message this way.


You can use the MFMessageComposeViewController class, as documented by Apple.

For that, first add MessageUI.framework to your project.

// in .h file make the following change

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>

@interface YourViewController: UIViewController <MFMessageComposeViewControllerDelegate>

// then in .m file do the following.

- (void)viewDidLoad {
    [super viewDidLoad];

SMSLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 340.0, 260.0, 30.0)];
    SMSLabel .frame = CGRectMake(30.0, 340.0, 260.0, 30.0);
    SMSLabel .adjustsFontSizeToFitWidth = YES;
    SMSLabel .hidden = YES;
    SMSLabel .text = @"";
    SMSLabel .userInteractionEnabled = NO;
    SMSLabel.alpha=0.0;
    [self.view addSubview:SMSLabel ];  


}

-(void)ComposerSheet 
{
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate = self;

    picker.recipients = [NSArray arrayWithObject:@"1234567"];  
    picker.body = @"iPhone OS4";

    [self presentModalViewController:picker animated:YES];
    [picker release];

}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    SMSLabel.alpha=1.0;

    switch (result)
    {
        case MessageComposeResultCancelled:
            SMSLabel .text = @"Result: canceled";
            NSLog(@"Result: canceled");
            break;
        case MessageComposeResultSent:
            SMSLabel .text = @"Result: sent";
            NSLog(@"Result: sent");
            break;
        case MessageComposeResultFailed:
            SMSLabel .text = @"Result: failed";
            NSLog(@"Result: failed");
            break;
        default:
            SMSLabel .text = @"Result: not sent";
            NSLog(@"Result: not sent");
            break;
    }

    [self dismissModalViewControllerAnimated:YES];

}


Sending messages is somewhat easy — you can usually send an email to a specially formatted number like 555.555.5555@verizon.net (example only, not sure of real format) and it will send to the device. There is not going to be an easy way to receive sms messages in your app natively.

You can, however, try using one of many free sms apis such as ZeepMobile


MFMessageComposeViewController will send the message through iMessage app. But If you want to send sms through network career of your iPhone then following code will help you

    NSString *phoneToCall = @"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
0

精彩评论

暂无评论...
验证码 换一张
取 消