一、分享類型
iOS支持的分享類型非常豐富,包括文本、圖片、鏈接、音頻、視頻、文件等。其中,常用的分享類型為文本、圖片、鏈接。
文本分享代碼如下:
UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[@"我是分享的文本"] applicationActivities:nil]; [self presentViewController:vc animated:YES completion:nil];
圖片分享代碼如下:
UIImage *image = [UIImage imageNamed:@"image.jpg"]; UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[image] applicationActivities:nil]; [self presentViewController:vc animated:YES completion:nil];
鏈接分享代碼如下:
NSURL *url = [NSURL URLWithString:@"http://www.example.com"]; UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil]; [self presentViewController:vc animated:YES completion:nil];
二、分享平台
iOS支持的分享平台也非常多,包括微信、QQ、微博、短信、郵件等
分享到微信代碼如下:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) { WXMediaMessage *message = [WXMediaMessage message]; message.title = @"分享標題"; message.description = @"分享描述"; [message setThumbImage:[UIImage imageNamed:@"image.jpg"]]; WXWebpageObject *webpage = [WXWebpageObject object]; webpage.webpageUrl = @"http://www.example.com"; message.mediaObject = webpage; SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneSession; [WXApi sendReq:req]; } else { // 未安裝微信客戶端 }
分享到QQ代碼如下:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) { QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:@"http://www.example.com"] title:@"分享標題" description:@"分享描述" previewImageData:UIImageJPEGRepresentation([UIImage imageNamed:@"image.jpg"], 0.5)]; SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj]; QQApiSendResultCode sent = [QQApiInterface sendReq:req]; if (sent != EQQAPISENDSUCESS) { // 分享失敗 } } else { // 未安裝QQ客戶端 }
三、自定義分享界面
iOS提供了UIActivityViewController類來進行分享,但是該類的界面無法自定義,如果需要自定義分享界面,則需要自己編寫分享界面,可以參考以下代碼:
- (void)showCustomShareView { UIView *bgView = [[UIView alloc] initWithFrame:self.view.bounds]; bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 200)]; contentView.backgroundColor = [UIColor whiteColor]; [bgView addSubview:contentView]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(contentView.frame), 20)]; titleLabel.text = @"分享到"; titleLabel.textColor = [UIColor blackColor]; titleLabel.textAlignment = NSTextAlignmentCenter; [contentView addSubview:titleLabel]; UIButton *shareButton1 = [[UIButton alloc] initWithFrame:CGRectMake(20, 60, 60, 60)]; [shareButton1 setImage:[UIImage imageNamed:@"icon1.png"] forState:UIControlStateNormal]; [shareButton1 addTarget:self action:@selector(shareButton1Clicked:) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview:shareButton1]; UIButton *shareButton2 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(contentView.frame) / 2 - 30, 60, 60, 60)]; [shareButton2 setImage:[UIImage imageNamed:@"icon2.png"] forState:UIControlStateNormal]; [shareButton2 addTarget:self action:@selector(shareButton2Clicked:) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview:shareButton2]; UIButton *shareButton3 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(contentView.frame) - 80, 60, 60, 60)]; [shareButton3 setImage:[UIImage imageNamed:@"icon3.png"] forState:UIControlStateNormal]; [shareButton3 addTarget:self action:@selector(shareButton3Clicked:) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview:shareButton3]; [self.view addSubview:bgView]; } - (void)shareButton1Clicked:(UIButton *)sender { // 分享到第一個平台 } - (void)shareButton2Clicked:(UIButton *)sender { // 分享到第二個平台 } - (void)shareButton3Clicked:(UIButton *)sender { // 分享到第三個平台 }
四、注意事項
在進行分享時,需要注意以下事項:
1. 在分享前,需要檢查對應的平台是否安裝,如果沒有安裝,則需要提示用戶安裝對應的客戶端。
2. 在分享圖片和文件時,需要注意文件大小,過大的文件可能會導致分享失敗。
3. 在分享到微信和QQ時,需要進行額外的註冊和處理,具體可以參考官方文檔。
原創文章,作者:BZLGX,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/334446.html