您的位置:首页 > 移动开发 > IOS开发

调用系统功能-打电话/发短息/发邮箱/LED灯/地图/截屏

2018-01-16 15:48 429 查看

调用系统打电话/发短息/发邮箱/LED灯功能/地图/截屏

1. 拨打电话

- (void)CallIphone:(NSString *)number {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",number]];
// 方法一(返回应用)
UIWebView *webView = [[UIWebView alloc] init];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:webView];

// 方法二(留在打电话界面)
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
}


2. 发短息(发完后留在短信界面)

- (void)sendMessage:(NSString *)number {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",number]];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}


3. 发邮箱

- (void)sendEmail:(NSString *)emailNum {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@",emailNum]];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}


4.调用Safari浏览器功能(在开发中遇到应用内打开网页的需求,建议使用UIWebView打开)

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://www.baidu.com/"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com/"]];
}


5. 控制LED灯(导入框架< AVFoundation/AVFoundation.h >)

-(void)turnTorchOn:(BOOL)on
{
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass !=nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (on) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
}
[device unlockForConfiguration];
}else{
NSLog(@"初始化失败");
}
}else{
NSLog(@"没有闪光设备");
}
}


6. 跳转WIFI设置

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=WIFI"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];
}


7. 跳转到蓝牙设置

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=Bluetooth"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Bluetooth"]];
}


8. 跳转到通用设置

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=General"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];
}


9.跳转到关于本机

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=General&path=About"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General&path=About"]];
}


10.跳转到定位服务

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
}


11.跳转到通知

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=NOTIFICATIONS_ID"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=NOTIFICATIONS_ID"]];
}


12.调用系统地图

// 获取当前位置
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
// 地理编码器
CLGeocoder *geocader = [[CLGeocoder alloc] init];
// 假定终点坐标
[geocader geocodeAddressString:@"" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *endPlacemark = placemarks.lastObject;

// 创建一个地图的地标对象
MKPlacemark *endMKPlacemark = [[MKPlacemark alloc] initWithPlacemark:endPlacemark];
// 在地图上标注一个点
MKMapItem *endMapItem = [[MKMapItem alloc] initWithPlacemark:endMKPlacemark];
//MKLaunchOptionsDirectionsModeKey 指定导航模式
//NSString * const MKLaunchOptionsDirectionsModeDriving 驾车
//NSString * const MKLaunchOptionsDirectionsModeWalking 步行
//NSString * const MKLaunchOptionsDirectionsModeTransit 公交
// 打开系统地图,并标出当前位置
[MKMapItem openMapsWithItems:@[currentLocation,endMapItem] launchOptions:@{MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}];


13. 截屏

截全屏

-(void)viewDidAppear:(BOOL)animated {
[superviewDidAppear:animated];

self.view.backgroundColor= [UIColorgreenColor];

UIWindow*screenWindow = [[UIApplicationsharedApplication]keyWindow];

UIGraphicsBeginImageContext(screenWindow.frame.size);

[screenWindow.layerrenderInContext:UIGraphicsGetCurrentContext()];

UIImage* viewImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage,nil,nil,nil);

}


截固定区域

#pragmamark ~~~~~~~~~~ 自定义截屏位置大小的逻辑代码 ~~~~~~~~~~

staticintScreenshotIndex=0; //

-(void)ScreenShot{

//这里因为我需要全屏接图所以直接改了,宏定义iPadWithd为1024,iPadHeight为768,

//UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0);//设置截屏大小

UIGraphicsBeginImageContextWithOptions(CGSizeMake(iPadWidth, iPadHeight), YES,0);//设置截屏大小

[[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRef imageRef =viewImage.CGImage;

//CGRect rect = CGRectMake(166, 211, 426, 320);//这里可以设置想要截图的区域

CGRect rect = CGRectMake(0,0, iPadWidth, iPadHeight);//这里可以设置想要截图的区域

CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

UIImage *sendImage =[[UIImage alloc] initWithCGImage:imageRefRect];

UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库

NSData *imageViewData =UIImagePNGRepresentation(sendImage);

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *pictureName= [NSString stringWithFormat:@"screenShow_%d.png",ScreenshotIndex];

NSString *savedImagePath =[documentsDirectory stringByAppendingPathComponent:pictureName];

NSLog(@"截屏路径打印: %@", savedImagePath);

//这里我将路径设置为一个全局String,这里做的不好,我自己是为了用而已,希望大家别这么写

[self SetPickPath:savedImagePath];

[imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录

CGImageRelease(imageRefRect);

ScreenshotIndex++;

}

//设置路径

- (void)SetPickPath:(NSString *)PickImage {

_ScreenshotsPickPath =PickImage;

}

//获取路径<这里我就直接用于邮件推送的代码中去了,能达到效果,但肯定有更好的写法>

- (NSString *)GetPickPath {

return_ScreenshotsPickPath;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息