• iOS 系统原生分享图片 文字 音乐 纯视频 网页


      为了方便使用,我封装了一个分享的工具类LFSystemShareUtil。工程要引Social.framework。

    LFSystemShareUtil.h

    #import <Foundation/Foundation.h>
    #import <Social/Social.h>
    
    typedef NS_ENUM(NSInteger, LFSystemShareType) {
        LFSystemShareWeChat,//微信
        LFSystemShareQQ,//腾讯QQ
        LFSystemShareSina,//新浪微博
    };
    
    typedef NS_ENUM(NSInteger, LFSystemShareState) {
        LFSystemShareStateCancel,//取消
        LFSystemShareStateDone,//完成
        LFSystemShareStateNone,//未安装
    };
    
    
    @interface LFSystemShareUtil : NSObject
    
    /**
     直接分享到某平台
     
     @param type 平台
     @param controller 弹出分享界面的控制器
     @param items 可以仅分享图@[UIImage],可以放多张;或者仅分享纯视频、音乐@[NSURL];或者一个带文字和缩略图的网页@[NSURL,NSString,UIImage],等等,总之把要分享的东西放到数组即可
     @param shareResult LFSystemShareState
     */
    + (void)shareWithType:(LFSystemShareType)type controller:(UIViewController *)controller andItems:(NSArray *)items completionHandler:(void(^)(LFSystemShareState state))shareResult;
    
    
    
    /**
     通过选择平台的控制面板分享
    
     @param controller 弹出分享界面的控制器
     @param items 可以仅分享图@[UIImage],可以放多张;或者仅分享纯视频、音乐@[NSURL];或者一个带文字和缩略图的网页@[NSURL,NSString,UIImage],等等,总之把要分享的东西放到数组即可
     @param shareResult 结果回调
     */
    + (void)shareWithController:(UIViewController *)controller andItems:(NSArray *)items completionHandler:(void(^)(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError))shareResult;
    
    @end

    LFSystemShareUtil.m

    #import "LFSystemShareUtil.h"
    
    @implementation LFSystemShareUtil
    
    + (void)shareWithType:(LFSystemShareType)type controller:(UIViewController *)controller andItems:(NSArray *)items completionHandler:(void(^)(LFSystemShareState state))shareResult {
        NSString *serviceType = @"";
        switch (type){
            case LFSystemShareWeChat:
                serviceType = @"com.tencent.xin.sharetimeline";
                break;
            case LFSystemShareQQ:
                serviceType = @"com.tencent.mqq.ShareExtension";
                break;
            case LFSystemShareSina:
                serviceType = @"com.apple.share.SinaWeibo.post";
                break;
            default:
                break;
        }
        
        /*
         <NSExtension: 0x1741735c0> {id = com.apple.share.Flickr.post}",
         "<NSExtension: 0x174173740> {id = com.taobao.taobao4iphone.ShareExtension}",
         "<NSExtension: 0x174173a40> {id = com.apple.reminders.RemindersEditorExtension}",
         "<NSExtension: 0x174173bc0> {id = com.apple.share.Vimeo.post}",
         "<NSExtension: 0x174173ec0> {id = com.apple.share.Twitter.post}",
         "<NSExtension: 0x174174040> {id = com.apple.mobileslideshow.StreamShareService}",
         "<NSExtension: 0x1741741c0> {id = com.apple.Health.HealthShareExtension}",
         "<NSExtension: 0x1741744c0> {id = com.apple.mobilenotes.SharingExtension}",
         "<NSExtension: 0x174174640> {id = com.alipay.iphoneclient.ExtensionSchemeShare}",
         "<NSExtension: 0x174174880> {id = com.apple.share.Facebook.post}",
         "<NSExtension: 0x174174a00> {id = com.apple.share.TencentWeibo.post}
         */
        
        /*
         "<NSExtension: 0x174174340> {id = com.tencent.xin.sharetimeline}", //微信
         "<NSExtension: 0x174173d40> {id = com.tencent.mqq.ShareExtension}", //QQ
         "<NSExtension: 0x1741738c0> {id = com.apple.share.SinaWeibo.post}", //微博
         */
        
        if ([SLComposeViewController isAvailableForServiceType:serviceType]) {
            SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:serviceType];
            for ( id obj in items){
                if ([obj isKindOfClass:[UIImage class]]){
                    [composeVC addImage:(UIImage *)obj];
                }else if ([obj isKindOfClass:[NSURL class]]){
                    [composeVC addURL:(NSURL *)obj];
                } else if ([obj isKindOfClass:[NSString class]]) {
                    [composeVC setInitialText:(NSString *)obj];
                }
            }
            
            // 弹出分享控制器
            composeVC.completionHandler = ^(SLComposeViewControllerResult result) {
                if (shareResult) {
                    shareResult((LFSystemShareState)result);
                }
            };
            [controller presentViewController:composeVC animated:YES completion:nil];
        } else {
            if (shareResult) {
                shareResult(LFSystemShareStateNone);
            }
        }
    }
    
    + (void)shareWithController:(UIViewController *)controller andItems:(NSArray *)items completionHandler:(void(^)(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError))shareResult {
        UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
        //不出现在活动项目
        activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList];
        activityVC.completionWithItemsHandler = shareResult;
        [controller presentViewController:activityVC animated:YES completion:nil];
    }
    
    @end
  • 相关阅读:
    time fly
    小论文初稿终于完成
    leetcode之Length of Last Word
    static关键字
    参数传递
    this关键字
    面向对象有三大特征
    空指针异常
    变量按数据类型分为
    构造方法
  • 原文地址:https://www.cnblogs.com/zhanglinfeng/p/7607216.html
Copyright © 2020-2023  润新知