调用系统打电话/发短息/发邮箱/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];
[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{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(iPadWidth, iPadHeight), YES,0);
[[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef =viewImage.CGImage;
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);
[self SetPickPath:savedImagePath];
[imageViewData writeToFile:savedImagePath atomically:YES];
CGImageRelease(imageRefRect);
ScreenshotIndex++;
}
- (void)SetPickPath:(NSString *)PickImage {
_ScreenshotsPickPath =PickImage;
}
- (NSString *)GetPickPath {
return_ScreenshotsPickPath;
}