IOS中一些常用的小功能的實現

iOS中的很多小功能都是非常簡單的,幾行代碼就搞定了,比如打電話、打開網址、發郵件、發短信等

打電話~方法1

  • 最簡單最直接的方式:直接跳到撥號界面

     NSURL *url = [NSURL URLWithString:@"tel://10010"];
     [[UIApplication sharedApplication] openURL:url];
    
  • 缺點
    電話打完之后不會自動回到原應用,直接停留在通話記錄界面

打電話~方法2

  • 撥打之前會彈框詢問用戶是否撥號,撥完后能自動回到原應用

     NSURL *url = [NSURL URLWithString:@"telprompt://10010"];
     [[UIApplication sharedApplication] openURL:url];
    
  • 缺點
    因為是私有API,所以可能不會被審核通過

打電話~方法3

創建一個UIWebView來加載URL,撥完后能自動回到原應用

if (_webView == nil) {
    _webView = [[UIWebView alloc] initWithFrame:CGRectZero];
}

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];

撥號之前會彈框詢問用戶是否撥號,撥完后能自動回到原程序
注意:這個webView千萬不要設置尺寸,不然會擋住其他界面,他只是用來打電話,不需要顯示


發短信~方法1

  • 直接跳到發短信界面,但是不能指定短信內容,而且不能回到原應用

     NSURL *url = [NSURL URLWithString:@"sms://10010"];
     [[UIApplication sharedApplication] openURL:url];
    

發短信~方法2

如果想指定短信內容,那就得使用MessageUI框架

包含主頭文件
#import <MessageUI/MessageUI.h>
//顯示發短信的控制器
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
//設置短信內容
vc.body = @"How are you?";
//設置收件人列表
vc.recipients = @[@"10010", @"10086"];
設置代理
vc.messageComposeDelegate = self;
顯示控制器
[self presentViewController:vc animated:YES completion:nil];
代理方法,當短信界面關閉的時候調用,發完后會自動回到原應用

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    //關閉短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    if (result == MessageComposeResultCancelled) {
        NSLog(@"取消發送");
    } else if (result == MessageComposeResultSent) {
        NSLog(@"已經發出");
    } else {
        NSLog(@"發送失敗");
    }
}

發郵件~方法1

  • 用自帶的郵件客戶端,發完郵件后不會自動回到原應用

     NSURL *url = [NSURL URLWithString:@"mailto://1648465054@qq.com"];
     [[UIApplication sharedApplication] openURL:url];
    
  • 參數實現

      //創建可變的地址字符串對象
      NSMutableString *mailUrl = [[NSMutableString alloc] init];
      //添加收件人,如有多個收件人,可以使用componentsJoinedByString方法連接,連接符為","
      NSString *recipients = @"1648465054@qq.com";
      [mailUrl appendFormat:@"mailto:%@?", recipients];
      //添加抄送人
      NSString *ccRecipients = @"10086@qq.com";
      [mailUrl appendFormat:@"&cc=%@", ccRecipients];
      //添加密送人
      NSString *bccRecipients = @"123456@163.com";
      [mailUrl appendFormat:@"&bcc=%@", bccRecipients];
      //添加郵件主題
      [mailUrl appendFormat:@"&subject=%@",@"設置郵件主題"];
      //添加郵件內容
      [mailUrl appendString:@"&body=<b>Hello</b> World!"];
      //跳轉到系統郵件App發送郵件
      NSString *emailPath = [mailUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
      [[UIApplication sharedApplication]openURL:[NSURL URLWithString:emailPath] options:@{} completionHandler:nil];
    

發郵件~方法2

  • 跟發短信的第二種方法差不多,只不過控制器名叫做MFMailComposeViewController——使用模態跳轉出郵件發送界面。具體實現如下:
    1) 項目需要導入MessageUI.framework框架
    2) 在對應類里導入頭文件:#import <MessageUI/MessageUI.h>
    3) 對應的類遵從代理:MFMailComposeViewControllerDelegate

    //判斷用戶是否已設置郵件賬戶
    if ([MFMailComposeViewController canSendMail]) { 
       [self sendEmailAction]; // 調用發送郵件的代碼
    }else{
       //給出提示,設備未開啟郵件服務
    }
    
  • 實現

    -(void)sendEmailAction{
           // 創建郵件發送界面
          MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
          // 設置郵件代理
          [mailCompose setMailComposeDelegate:self];
          // 設置收件人
          [mailCompose setToRecipients:@[@"sparkle_ds@163.com"]];
          // 設置抄送人
          [mailCompose setCcRecipients:@[@"1622849369@qq.com"]];
          // 設置密送人
          [mailCompose setBccRecipients:@[@"15690725786@163.com"]];
          // 設置郵件主題
          [mailCompose setSubject:@"設置郵件主題"];
          //設置郵件的正文內容
          NSString *emailContent = @"我是郵件內容";
          // 是否為HTML格式
          [mailCompose setMessageBody:emailContent isHTML:NO];
          // 如使用HTML格式,則為以下代碼
          // [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World!</p></body></html>" isHTML:YES];
          //添加附件
          UIImage *image = [UIImage imageNamed:@"qq"];
          NSData *imageData = UIImagePNGRepresentation(image);
          [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"qq.png"];
          NSString *file = [[NSBundle mainBundle] pathForResource:@"EmptyPDF" ofType:@"pdf"];
          NSData *pdf = [NSData dataWithContentsOfFile:file];
          [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"EmptyPDF.pdf"];
          // 彈出郵件發送視圖
          [_curVC presentViewController:mailCompose animated:YES completion:nil];
    }
    
  • 代理方法

    #pragma mark - MFMailComposeViewControllerDelegate的代理方法:
    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
      switch (result) {
      case MFMailComposeResultCancelled:
          NSLog(@"Mail send canceled: 用戶取消編輯");
          break;
      case MFMailComposeResultSaved:
          NSLog(@"Mail saved: 用戶保存郵件");
          break;
      case MFMailComposeResultSent:
          NSLog(@"Mail sent: 用戶點擊發送");
          break;
      case MFMailComposeResultFailed:
          NSLog(@"Mail send errored: %@ : 用戶嘗試保存或發送郵件失敗", [error localizedDescription]);
          break;
      }
      // 關閉郵件發送視圖
      [_curVC dismissViewControllerAnimated:YES completion:nil];
    }
    

發郵件~方法3

SKPSMTPMessage(第三方庫)——可以在不告知用戶的情況下進行郵件發送,但建議在發送之前告知用戶,讓用戶決定是否發送。具體實現如下:
1)添加該第三方庫
2)項目還需要導入CFNetwork.framework框架
3)在對應類中導入頭文件:#import "SKPSMTPMessage.h",#import "NSData+Base64Additions.h"
4)對應的類遵從代理:SKPSMTPMessageDelegate
感興趣的可以轉至github上面查看一下具體的使用方法


打開其他常見文件

如果想打開一些常見文件,比如html、txt、PDF、PPT等,都可以使用UIWebView打開
只需要告訴UIWebView文件的URL即可
至于打開一個遠程的共享資源,比如http協議的,也可以調用系統自帶的Safari瀏覽器:

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

應用間的跳轉

  • 有時候需要在本應用中打開其他應用,比如從A應用跳轉到B應用

  • 首先B應用應該有自己的url地址(URL Schemes):如jimoo://ios.open

  • 接著在A應用中使用UIApplication完成跳轉

     NSURL *url = [NSURL URLWithString:@"jimoo://ios.open"];
     [[UIApplication sharedApplication] openURL:url];
    

應用評分

為了提高應用的用戶體驗,經常需要邀請用戶對應用進行評分,應用評分無非就是跳轉到AppStore展示自己的應用,然后由用戶自己撰寫評論,如何跳轉到AppStore,并且展示自己的應用。

方法:

NSString *appid = @"725296055”;
NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容