ios原生分享

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

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

-(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

- (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

- (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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值