我們在程序中或多或少都會使用到手機的一些應用程序,比如電話,短信,瀏覽器,相機,相冊等,那么我們該如何調用這些系統自帶的應用程序呢?下面我一一說來.
調用電話、瀏覽器
對于調用電話和瀏覽器比較簡單,使用UIApplication調用openURL方法.
調用系統電話
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
調用系統safari瀏覽器
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.coder-dong.com"]];
調用短信功能
調用系統的短信功能, 同樣使用UIApplication調用openURL方法. 我們只能openURL的方法中只能設定號碼,不能做內容太上的更改.那么我們就需要導入我們的MessageUI.framework庫,然后導入頭文件MFMessageComposeViewController.h,并實現代理方法.
導入MFMessageComposeViewController頭文件
#import <MessageUI/MFMessageComposeViewController.h>
實現自定義方法并設置代理
- (IBAction)SMSAction:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10086"]];
[self sendSMS:@"我想咨詢一下話費余額是不是一百萬." recipientList:@[@"10086客服"]];
}
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText]){
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self showDetailViewController:controller sender:nil];
}
}
當完成消息的發送之后,我們就需要調用我們的代理方法,給用戶反饋信息
//代理方法的實現
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
if (result == MessageComposeResultCancelled){
NSLog(@"取消發送消息");
} else if (result == MessageComposeResultSent){
NSLog(@"消息已經發送");
}else{
NSLog(@"消息發送失敗");
}
}
調用相機、相冊
調用相機、相冊我們需要使用到UIImagePickerController這個類,所以我們需要首先導入這個類
#import <UIKit/UIImagePickerController.h>
然后我們在點擊方法中實現調用相機,當然了,調用的時候,我們要先判斷設備是否存在攝像頭.
//設置調用類型為相機
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"本設備未發現攝像頭!");
return;
}
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
pickerController.sourceType = sourceType;
pickerController.delegate = self;
pickerController.allowsEditing = YES;//設置是否可以進行編輯
[self showDetailViewController:pickerController sender:nil];
調用系統相冊,調用系統相冊的時候,跟調用相機的時候十分相似.
//設置調用類型為相冊.
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
pickerController.sourceType = sourceType;
pickerController.delegate = self;
pickerController.allowsEditing = YES;
[self showDetailViewController:pickerController sender:nil];
我們調用系統相冊和相機最終的目的是為了獲得系統中的圖片資源.所以我們需要實現代理方法,獲取到我們的圖片.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//當選擇的類型是圖片
if ([type isEqualToString:@"public.image"])
{
//先把圖片轉成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//圖片保存的路徑
//這里將圖片放在沙盒的documents文件夾中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把剛剛圖片轉換的data對象拷貝至沙盒中 并保存為image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
//得到選擇后沙盒中圖片的完整路徑
NSString * filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
NSLog(@"%@",filePath);
//關閉相冊界面
[picker dismissModalViewControllerAnimated:YES];
//創建一個選擇后圖片的小圖標放在下方
//類似微薄選擇圖后的效果
UIImageView *smallimage = [[UIImageView alloc] initWithFrame:
CGRectMake(50, 120, 40, 40)] ;
smallimage.image = image;
//加在視圖中
[self.view addSubview:smallimage];
}
}