華山論劍之淺談iOS調用大亂斗(電話,短信,瀏覽器,相機,相冊)

我們在程序中或多或少都會使用到手機的一些應用程序,比如電話,短信,瀏覽器,相機,相冊等,那么我們該如何調用這些系統自帶的應用程序呢?下面我一一說來.


調用電話、瀏覽器


對于調用電話和瀏覽器比較簡單,使用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];
        
    }

}



整體上看 ,調用系統的一些應用還是很簡單的,希望這篇調用大亂斗能對大家有所幫助.??

--->參考博客鏈接.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,536評論 25 708
  • 1.OC里用到集合類是什么? 基本類型為:NSArray,NSSet以及NSDictionary 可變類型為:NS...
    輕皺眉頭淺憂思閱讀 1,396評論 0 3
  • 1. 主題明確 標題不能為空,要簡潔 2. 開頭結尾 開頭,和收件人很熟的情況下,可以用Dear,XX,否則用HI...
    硬件工程師技術號閱讀 1,104評論 0 0
  • 水石軒主人閱讀 203評論 0 0