iOS 调用打电话\发信息\email功能

一、使用系统自带功能,发送短信、拨打电话、发送邮件、打开网站

1、调用 自带mail

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@163.com"]];

2、调用 电话phone

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8008808888"]]; // 退出应用

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://8008808888"]];// 不退出应用

3、调用 SMS

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888888"]];

4、调用自带 浏览器 safari

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];


宏定义使用方法

#define MailSend(email) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto://%@",email]])

#define PhoneCall(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phone]])

#define PhoneCallAuto(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",phone]])

#define SMSSend(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",phone]])

#define SafariOpen(url) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url])



二、打电话 三种方式

// 定义点击拨号按钮时的操作

- (void)callAction

{

    // 此处读入电话号码

    NSString *number = @"";

    

    // number为号码字符串 如果使用这个方法 结束电话之后会进入联系人列表

    // NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",number];


    // 而这个方法则打电话前先弹框 是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核

    NSString *num = [[NSString alloc] initWithFormat:@"telprompt://%@",number];

    // 拨号

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

}


//  第三种方式打电话

// 下面的代码能在应用中添加一个电话按钮, UIWebView加载电话,这种是合法的,可以上App Store的。

- (void)CallPhone

{

    // 电话号码

    NSString *phoneNum = @"";

    

    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];

    

    if (!phoneCallWebView)

    {

        // 这个webView只是一个后台的容易 不需要add到页面上来  效果跟方法二一样 但是这个方法是合法的

        phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];

    }

    

    [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];

}


三、发短信功能


调用发短信功能

[[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];

}


群发短信:

- (void)sendSMS

{

    BOOL canSendSMS = [MFMessageComposeViewController canSendText];

    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];

}


事件绑定发送短信

- (void)showSMSPicker:(UIButton *)button

{

    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));

    if (messageClass != nil)

    {

        if ([messageClass canSendText])

        {

            [self displaySMSComposerSheet];

        }

        else

        {

            // 设备没有短信功能

            

        }

    }

    else

    {

        // iOS版本过低,iOS4.0以上才支持程序内发送短信

    }

}


iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话)


四、调用safar来打开一个网页,下面是一个简单的使用:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;

    [self.window makeKeyAndVisible];

    

    // 调用safar打开网页

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.cnblogs.com/foxmin"]];

    

    return YES;

}



五、查看更多iPhone应用程序的调用和第三方应用程序的调用,可以在iPhoneURL方案查看。下面列举部分:

Apple iPhone Applications

Safari

Any URL starting with http:// which does not point to maps.google.com or www.youtube.com is sent to Safari:

NSString *stringURL = @"http://wiki.akosma.com/";

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];

Apparently feed:// opens http://reader.mac.com in Safari.


Maps

URLs starting with http://maps.google.com open up the "Maps" application automatically:

NSString *title = @"title";

float latitude = 35.4634;

float longitude = 9.43425;

int zoom = 13;

NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@@%1.6f,%1.6f&z=%d", title, latitude, longitude, zoom];

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];

It seems that maps:// also opens up the Maps application.


Phone

The phone links start with "tel:" but must not contain spaces or brackets (it can contain dashes and "+" signs, though):

NSMutableString *phone = [[@"+ 12 34 567 89 01" mutableCopy] autorelease];

[phone replaceOccurrencesOfString:@" "

                       withString:@""

                          options:NSLiteralSearch

                            range:NSMakeRange(0, [phone length])];

[phone replaceOccurrencesOfString:@"("

                       withString:@""

                          options:NSLiteralSearch

                            range:NSMakeRange(0, [phone length])];

[phone replaceOccurrencesOfString:@")"

                       withString:@""

                          options:NSLiteralSearch

                            range:NSMakeRange(0, [phone length])];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phone]];

[[UIApplication sharedApplication] openURL:url];


SMS

To open the SMS application, just use the sms: protocol in your URL:

NSString *stringURL = @"sms:";

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];

You can specify a phone number, but apparently not a default text for your message:

NSString *stringURL = @"sms:+12345678901";

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];

The same restrictions apply as for phone URLs, regarding spaces, brackets and dashes.


Mail

These URLs launch Mail and open the compose message controller:

NSString *stringURL = @"mailto:test@example.com";

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];

You can also provide more information, for a customized subject and body texts:

NSString *subject = @"Message subject";

NSString *body = @"Message body";

NSString *address = @"test1@akosma.com";

NSString *cc = @"test2@akosma.com";

NSString *path = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@", address, cc, subject, body];

NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:url];

You might also omit some information:

NSString *subject = @"Message subject";

NSString *path = [NSString stringWithFormat:@"mailto:?subject=%@", subject];

NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:url];

For more complex body texts, you might want to use the CFURLCreateStringByAddingPercentEscapes() function, as explained above in this page.


YouTube

URLs starting with http://www.youtube.com open up the "YouTube" application automatically:

NSString *stringURL = @"http://www.youtube.com/watch?v=WZH30T99MaM";

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];

It also works with this URL (which normally brings the Flash video player used by YouTube):

NSString *stringURL = @"http://www.youtube.com/v/WZH30T99MaM";

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];


iTunes

To open up iTunes, use this URL:

NSString *stringURL = @"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=156093464&id=156093462&s=143441";

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];


App Store

Very similar to the iTunes URLs:

NSString *stringURL = @"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=294409923&mt=8";

NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url];


iBooks

(source: http://akos.ma/aqdr)

 NSString *stringURL = @"itms-books:";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 NSString *stringURL = @"itms-bookss:";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 

 Third Party Applications

 Most URL schemes in this list come from App Lookup.

 

 AirSharing

 Launch the application:

 NSString *stringURL = @"airsharing://";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 

 Alocola

 Launch the application:

 NSString *stringURL = @"alocola://";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 

 Appigo Notebook

 Launch the application:

 NSString *stringURL = @"appigonotebook://";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 

 Appigo Todo

 Launch the application:

 NSString *stringURL = @"appigotodo://";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 Create a new task:

 NSString *template = @"appigotodo://com.example.xyzapp/import?name=%@&note=%@&due-date=%@&priority=%@&repeat=%@";

 NSString *name = @"Buy%20some%20milk";

 NSString *note = @"Stop%20on%20the%20way%20home%20from%20work.";

 NSString *dueDate = @"2009-07-16";

 NSString *priority = @"1";

 NSString *repeat = @"101";

 NSString *stringURL = [NSString stringWithFormat:template, name, note, dueDate, priority, repeat];

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 

 Duo

 Launch the application and pre-populate the update field with your desired Tweet/Facebook update. Optional: Provide Duo with the custom URL and return path to your app to provide the user with a button to return to your app.

 NSString *appID = @"Your Apps Name";

 NSString *updateInfo = @"The Tweet/Status Update";

 NSString *returnScheme = @"Your Apps Return URL Scheme";

 NSString *returnPath = @"Your/Apps/Return/Path";

 NSString *baseURL = @"duoxx://updateFaceTwit?";

 NSString *encodedUpdateInfo = [updateInfo stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

 NSString *urlString = [NSString stringWithFormat:@"%@BLappID=%@;BLupdateInfo=%@;BLreturnScheme=%@;BLreturnPath=%@",

                        baseURL, appID, encodedUpdateInfo, returnScheme, returnPath];

 

 NSURL *url = [NSURL URLWithString:urlString];

 [[UIApplication sharedApplication] openURL:url];

 

 

 The Cartographer

 Launch app and add a placemark at given coordinate:

 NSString *title = @"Placemark title (optional)";

 NSString *summary = @"Placemark description (optional)";

 CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(123.0, 12.0);

 

 title = [(id)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)title, NULL, (CFStringRef)@";/?:@&=+$,", kCFStringEncodingUTF8) autorelease];

 summary = [(id)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)summary, NULL, (CFStringRef)@";/?:@&=+$,", kCFStringEncodingUTF8) autorelease];

 

 NSString *cartographerURL = [NSString stringWithFormat:@"cartographer://placemark?coordinate=%lf,%lf&title=%@&summary=%@",

                              coordinate.latitude, coordinate.longitude, title, summary];

 

 NSURL *url = [NSURL URLWithString:cartographerURL];

 [[UIApplication sharedApplication] openURL:url];

 Launch app and load either a KML source or a Google My Map:

 NSString *sourceURL = @"http://....";

 // e.g. http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=208320711678395936256.00046bbcfdcd1f3ebf64b&z=5

 

 // Optional:

 MKCoordinateRegion region = ...;

 sourceURL = [sourceURL stringByAppendingFormat:@"&ll=%lf,%lf&spn=%lf,%lf",

              region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta];

 

 NSURL *url = [NSURL URLWithString:[sourceURL stringByReplacingOccurrencesOfString:@"http://" withString:@"cartographer://"]];

 [[UIApplication sharedApplication] openURL:url];

 

 ChatCo

 NSString *stringURL = @"irc://irc.example.domain/roomName";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 NSString *stringURL = @"irc://irc.example.domain/Nickname@roomName";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

 NSString *stringURL = @"irc://irc.example.domain/Nickname!Realname@roomName";

 NSURL *url = [NSURL URLWithString:stringURL];

 [[UIApplication sharedApplication] openURL:url];

转载于:https://my.oschina.net/potato512/blog/647730

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值