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;
    }
}
相关推荐
FreeBuf_1 分钟前
朝鲜APT组织使用Nim语言恶意软件对macOS发起隐秘Web3与加密货币攻击
macos·web3·策略模式
YungFan1 小时前
iOS26适配指南之通知
ios·swift
木叶丸2 小时前
跨平台方案该如何选择?
android·前端·ios
我唔知啊2 小时前
OC底层原理二:OC对象的分类(实例对象、类对象、元类对象)
ios·objective-c
泓博4 小时前
KMP(Kotlin Multiplatform)改造(Android/iOS)老项目
android·ios·kotlin
Digitally4 小时前
如何将信息从 iPhone 同步到Mac(完整步骤和示意图)
macos·ios·iphone
大猫会长4 小时前
使用Mac自带的图像捕捉导出 iPhone 相册
ios·iphone
二流小码农10 天前
鸿蒙开发:基于node脚本实现组件化运行
android·ios·harmonyos
依旧风轻10 天前
Domain 层完全指南(面向 iOS 开发者)
ios·domain·entity·sqi
续天续地10 天前
开箱即用的Kotlin Multiplatform 跨平台开发模板:覆盖网络/存储/UI/DI/CI工具链
ios·kotlin