ios原生分享

什么是 ios 系统的原生分享呢,如下图所示

具体使用系统UIActivityViewController,完整代码如下:

objectivec 复制代码
-(void)shareAny:(NSString *)text url:(NSString *)_url imagePath:(NSString *)_imagePath
{
    NSLog(@"shareAny, text:%@, url:%@, imagePath:%@", text, _url, _imagePath);
    NSString *textToShare = text;//需要分享的文本内容
    NSArray *activityItems = @[textToShare];

    if (_url != nil) {
        NSURL *urlToShare = [NSURL URLWithString:_url];//需要分享的URL
        activityItems = @[textToShare, urlToShare];
    }

    if (_imagePath != nil) {
       UIImage *imageToShare = [UIImage imageWithContentsOfFile:_imagePath];//需要分享的图片
        activityItems = @[textToShare, imageToShare];
    }

    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    // 禁用分享渠道
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
    UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
    // 分享之后的回调
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (completed) {
            NSLog(@"成功分享,分享平台%@",activityType);
        }else{
            NSLog(@"取消分享");
        };
    };

    [self.viewController presentViewController:activityVC animated:true completion:nil];
}

分享到WhatsApp

objectivec 复制代码
- (void) whatsappShareText:(NSString*)text {
    NSLog(@"whatsapp share text %@", text);
    NSString *url = [NSString stringWithFormat:@"whatsapp://send?text=%@", [text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
    NSURL *whatsappURL = [NSURL URLWithString: url];

    if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
        [[UIApplication sharedApplication] openURL: whatsappURL];
    } else {
        // Cannot open whatsapp
        NSLog(@"whatsapp cant't open");
    }
}

分享到Facebook

objectivec 复制代码
- (void) facebookShareUrl:(NSString *)url {
    NSLog(@"facebook share url %@", url);
    if(![self checkAPPIsExist:@"fb"]) {
		NSLog(@"facebook is not exits");
        return;;
    }

    // 首先判断某个平台是否可用(如果未绑定账号则不可用)
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        NSLog(@"facebook is not available");
        return;
    }

    // 创建控制器,并设置ServiceType
    SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    // 添加要分享的url
    [composeVC addURL:[NSURL URLWithString:url]];
    // 弹出分享控制器
    [self.viewController presentViewController:composeVC animated:YES completion:nil];
    // 监听用户点击事件
    composeVC.completionHandler = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultDone) {
            NSLog(@"facebook share send");
        }
        else if (result == SLComposeViewControllerResultCancelled)
        {
            NSLog(@"facebook share cancel");
        }
    };
}

- (void) facebookShareImage:(NSString *)imagePath {
    NSLog(@"facebook share image %@", imagePath);
    if(![self checkAPPIsExist:@"fb"]) {
        NSLog(@"facebook is not exits");
        return;
    }

    // 首先判断某个平台是否可用(如果未绑定账号则不可用)
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        NSLog(@"facebook is not available");
        return;
    }

    // 创建控制器,并设置ServiceType
    SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    // 添加要分享的图片
    [composeVC addImage:[UIImage imageWithContentsOfFile:imagePath]];
    // 弹出分享控制器
    [self.viewController presentViewController:composeVC animated:YES completion:nil];
    // 监听用户点击事件
    composeVC.completionHandler = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultDone) {
            NSLog(@"facebook share send");
        }
        else if (result == SLComposeViewControllerResultCancelled)
        {
            NSLog(@"facebook share cancel");
        }
    };
}

//判断是否安装APP
-(BOOL)checkAPPIsExist:(NSString*)URLScheme{
    NSURL* url;
    if ([URLScheme containsString:@"://"]) {
        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",URLScheme]];
    } else {
        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@://",URLScheme]];
    }
    if ([[UIApplication sharedApplication] canOpenURL:url]){
        return YES;
    } else {
        return NO;
    }
}
相关推荐
调皮的芋头1 小时前
iOS各个证书生成细节
人工智能·ios·app·aigc
coooliang2 小时前
【iOS】SwiftUI状态管理
ios·swiftui·swift
天荒地老笑话么4 小时前
Mac安装配置Tomcat 8
java·macos·tomcat
自娱自乐228 小时前
mac下使用webstorm监听less文件自动生成wxss文件
macos·less·webstorm
胖虎18 小时前
深入解析 iOS 视频录制(三):完整录制流程的实现与整合
ios·音视频·cocoa·ios视频录制·自定视频录制
coooliang8 小时前
【iOS】包大小和性能稳定性优化
ios
又知道了小老虎9 小时前
iOS开发基础-通过C++快速掌握Objective-C语言(基础)
ios
关键帧Keyframe10 小时前
音视频面试题集锦第 19 期 | 读取纹理数据
ios·图像识别·音视频开发
一只往上爬的蜗牛13 小时前
【DeepSeek】Mac m1电脑部署DeepSeek
macos·deepseek
小羊在奋斗13 小时前
【Linux】认识协议、Mac/IP地址和端口号、网络字节序、socket套接字
linux·网络·macos