雜感1

1、復(fù)制字符串到剪切板

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"復(fù)制字符串到剪切板";

2、打電話

創(chuàng)建一個(gè)成員變量UIWebView來加載URL,撥完后能自動(dòng)回到原應(yīng)用

@interface ViewController ()
@property(nonatomic,strong)UIWebView *webView;
@end
if (_webView == nil) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    }
    [_webView loadRequest: [NSURLRequest requestWithURL:  [NSURL URLWithString: @"tel://電話號(hào)碼"]]];  //   // tel:13534268291
    //需要注意的是:這個(gè)webView千萬不要添加到界面上來,不然會(huì)擋住其他界面

3、發(fā)短信

如果想指定短信內(nèi)容,那就得使用MessageUI框架。包含頭文件:

#import <MessageUI/MessageUI.h>

遵循MFMessageComposeViewControllerDelegate協(xié)議:

@interface ViewController ()<MFMessageComposeViewControllerDelegate>
@end
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
    // 設(shè)置短信內(nèi)容
    vc.body = @"短信內(nèi)容";
    
    // 設(shè)置收件人列表
    vc.recipients = @[@"號(hào)碼1", @"號(hào)碼2"];
    // 設(shè)置代理
    vc.messageComposeDelegate = self;
    // 顯示控制器
    [self presentViewController:vc animated:YES completion:nil];

實(shí)現(xiàn)發(fā)短信的代理方法:

//代理方法,當(dāng)短信界面關(guān)閉的時(shí)候調(diào)用,發(fā)完后會(huì)自動(dòng)回到原應(yīng)用
- (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result {
    // 關(guān)閉短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    if(result == MessageComposeResultCancelled) {
        NSLog(@"取消發(fā)送");
    } else if(result == MessageComposeResultSent) {
        NSLog(@"已經(jīng)發(fā)出");
    } else {
        NSLog(@"發(fā)送失敗");
    }
}

4、發(fā)郵件

遵循MFMailComposeViewControllerDelegate協(xié)議:

@interface ViewController ()<MFMailComposeViewControllerDelegate>
@end
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
    //************************ 設(shè)置郵件內(nèi)容 ************************
    // 設(shè)置郵件主題
    [vc setSubject:@"主題"];
    // 設(shè)置郵件內(nèi)容
    [vc setMessageBody:@"郵件內(nèi)容" isHTML:NO];
    // 設(shè)置收件人列表
    [vc setToRecipients:@[@"收件人@qq.com"]];
    // 設(shè)置抄送人列表
    [vc setCcRecipients:@[@"抄送人@qq.com"]];
    // 設(shè)置密送人列表
    [vc setBccRecipients:@[@"密送人@qq.com"]];
    
    // 添加附件(例如:一張圖片)
    UIImage *image = [UIImage imageNamed:@"圖片.jpeg"];
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    [vc addAttachmentData:data mimeType:@"image/jpeg" fileName:@"lufy.jpeg"];
    // 設(shè)置代理
    vc.mailComposeDelegate = self;
    // 顯示控制器
    [self presentViewController:vc animated:YES completion:nil];

實(shí)現(xiàn)發(fā)郵件的代理方法:

//郵件發(fā)送后的代理方法回調(diào),發(fā)完后會(huì)自動(dòng)回到原應(yīng)用
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    // 關(guān)閉郵件界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    
    if(result == MFMailComposeResultCancelled) {
        NSLog(@"取消發(fā)送");
    } else if(result == MFMailComposeResultSent) {
        NSLog(@"已經(jīng)發(fā)出");
    } else {
        NSLog(@"發(fā)送失敗");
    }
}

5、打開其他常見文件

如果想打開一些常見文件,比如html、txt、PDF、PPT等,都可以使用UIWebView打開,只需要告訴UIWebView文件的URL即可。至于打開一個(gè)遠(yuǎn)程的共享資源,比如http協(xié)議的,也可以調(diào)用系統(tǒng)自帶的Safari瀏覽器:

//創(chuàng)建需要打開的 URL 地址
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
[[UIApplication sharedApplication] openURL:url];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容