app内自动拨打号码 ios
2015-05-21 00:56:28 sammyieveo 阅读数 660

直接代码:

// ACETelPrompt.h



#import <Foundation/Foundation.h>


@interface ACETelPrompt : NSObject


typedef void (^ACETelCallBlock)(NSTimeInterval duration);

typedef void (^ACETelCancelBlock)(void);


+ (BOOL)callPhoneNumber:(NSString *)phoneNumber

                   call:(ACETelCallBlock)callBlock

                 cancel:(ACETelCancelBlock)cancelBlock;


@end


// ACETelPrompt.m


#import "ACETelPrompt.h"

#import "AppDelegate.h"


// the time required to launch the phone app and come back (will be substracted to the duration)

#define kCallSetupTime      3.0


@interface ACETelPrompt ()

@property (nonatomic, strong) NSDate *callStartTime;


@property (nonatomic, copy) ACETelCallBlock callBlock;

@property (nonatomic, copy) ACETelCancelBlock cancelBlock;

@end


@implementation ACETelPrompt


+ (instancetype)sharedInstance

{

    static ACETelPrompt *_instance = nil;

    static dispatch_once_t oncePredicate;

    dispatch_once(&oncePredicate, ^{

        _instance = [[self alloc] init];

    });

    return _instance;

}


+ (BOOL)callPhoneNumber:(NSString *)phoneNumber

                   call:(ACETelCallBlock)callBlock

                 cancel:(ACETelCancelBlock)cancelBlock

{

    if ([self validPhone:phoneNumber]) {

        

        ACETelPrompt *telPrompt = [ACETelPrompt sharedInstance];

        

        // observe the app notifications

        [telPrompt setNotifications];

        

        // set the blocks

        telPrompt.callBlock = callBlock;

        telPrompt.cancelBlock = cancelBlock;

        

        // clean the phone number

        NSString *simplePhoneNumber =

        [[phoneNumber componentsSeparatedByCharactersInSet:

          [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

        

        // call the phone number using the telprompt scheme

        NSString *stringURL = [@"telprompt://" stringByAppendingString:simplePhoneNumber];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:stringURL]];

        

        return YES;

    }

    return NO;

}


+ (BOOL)validPhone:(NSString*) phoneString

{

    NSTextCheckingType type = [[NSTextCheckingResult phoneNumberCheckingResultWithRange:NSMakeRange(0, phoneString.length)

                                                                            phoneNumber:phoneString] resultType];

    return type == NSTextCheckingTypePhoneNumber;

}



#pragma mark - Notifications


- (void)setNotifications

{

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(applicationDidEnterBackground:)

                                                 name:UIApplicationDidEnterBackgroundNotification

                                               object:nil];

    

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(applicationDidBecomeActive:)

                                                 name:UIApplicationDidBecomeActiveNotification

                                               object:nil];

    

}



#pragma mark - Events


- (void)applicationDidEnterBackground:(NSNotification *)notification

{

    // save the time of the call

    self.callStartTime = [NSDate date];

}


- (void)applicationDidBecomeActive:(NSNotification *)notification

{

    // now it's time to remove the observers

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    

    if (self.callStartTime != nil) {

        

        // I'm coming back after a call

        if (self.callBlock != nil) {

            self.callBlock(-([self.callStartTime timeIntervalSinceNow]) - kCallSetupTime);

        }

        

        // reset the start timer

        self.callStartTime = nil;

    

    } else if (self.cancelBlock != nil) {

        

        // user didn't start the call

        self.cancelBlock();

    }

}


调用

- (IBAction)connectUs:(id)sender {

    [ACETelPrompt callPhoneNumber:@"020-38880808080"

                             call:^(NSTimeInterval duration) {

                                 NSLog(@"User made a call of %.1f seconds", duration);

                                 

                             } cancel:^{

                                 NSLog(@"User cancelled the call");

                             }];

}




@end



2012-06-20 17:20:56 zhibudefeng 阅读数 729
NSString *string=[NSString stringWithFormat:@"%@",stringPhone];
		NSString *telString = [@"tel://" stringByAppendingString:[string stringByReplacingOccurrencesOfRegex:@"[^0-9+]" withString:@""]];
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telString]];

2013-06-25 18:23:30 Li_Huifeng 阅读数 617


NSString *number =[NSString stringWithFormat:@"tel://%@",telephone];// 此处读入电话号码

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
2014-04-09 17:02:44 zhangkongzhongyun 阅读数 727

iOS4.0新加入了MFMessageComposeViewControllerMFMessageComposeViewControllerDelegate,提供了发送短信的接口,可以像发送邮件那样不用跳出程序来发送短信. 介绍可参阅Message UI Framework Reference

一些笔记:

MFMessageComposeViewController

  • 提供了操作界面
  • 使用前必须检查canSendText方法,若返回NO则不应将这个controller展现出来,而应该提示用户不支持发送短信功能.
  • 界面不能自行定制
  • 要发送的短信的内容(body)和收件人(recipients)在展现这个controller前需初始化好,展现了之后短信内容不能通过程序来进行修改.不过用户仍然可以手工修改短信内容和选择收件人
  • 用户点了发送或者取消,或者发送失败时,MFMessageComposeViewControllerDelegate 的- messageComposeViewController:didFinishWithResult:方法都能得到通知,在这里进行相应的处理

若在iOS3.0上运行的话,会提示dyld: Symbol not found: _OBJC_CLASS_$_MFMessageComposeViewController 解决方案:

  1. MessageUI.framework的引入类型应选择weak(在target -> Get Info -> General -> Linked Libraries -> MessageUI.framework -> Type 里修改)
  2. 不要在.h文件里直接import MessageUI/MFMessageComposeViewController.h,改为import <MessageUI/MessageUI.h>

具体的发送代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    
    #pragma mark -
    #pragma mark SMS

    -(IBAction)showSMSPicker:(id)sender {
      // The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later.
      // So, we must verify the existence of the above class and log an error message for devices
      //     running earlier versions of the iPhone OS. Set feedbackMsg if device doesn't support
      //     MFMessageComposeViewController API.
      Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));

      if (messageClass != nil) {
          // Check whether the current device is configured for sending SMS messages
          if ([messageClass canSendText]) {
              [self displaySMSComposerSheet];
          }
          else {
              [UIAlertView quickAlertWithTitle:@"设备没有短信功能" messageTitle:nil dismissTitle:@"关闭"];
          }
      }
      else {
          [UIAlertView quickAlertWithTitle:@"iOS版本过低,iOS4.0以上才支持程序内发送短信" messageTitle:nil dismissTitle:@"关闭"];
      }
    }

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

      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:%@"
                                          ,[web stringByEvaluatingJavaScriptFromString:@"document.title"]
                                          ,absUrl];
      [absUrl release];
      [self presentModalViewController:picker animated:YES];
      [picker release];
    }

    - (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                   didFinishWithResult:(MessageComposeResult)result {

      switch (result)
      {
          case MessageComposeResultCancelled:
              LOG_EXPR(@"Result: SMS sending canceled");
              break;
          case MessageComposeResultSent:
              LOG_EXPR(@"Result: SMS sent");
              break;
          case MessageComposeResultFailed:
              [UIAlertView quickAlertWithTitle:@"短信发送失败" messageTitle:nil dismissTitle:@"关闭"];
              break;
          default:
              LOG_EXPR(@"Result: SMS not sent");
              break;
      }
      [self dismissModalViewControllerAnimated:YES];
    }

iOS App内评分

阅读数 227

iOS App内评价

阅读数 32

iOS App内评分

阅读数 24

ios app内发送短信

阅读数 3745

ios拨打打电话

阅读数 3

没有更多推荐了,返回首页