iOS分享到Facebook/微信/Line C++接口


//
//  DoShare.h
//  DoShare
//
//  Created by zhai chunlin on 15/11/11.
//  Copyright (c) 2015年 YA. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum : NSUInteger {
    DoWXShare,
    DoFacebookShare,
    DoLineShare,
} DoShareType;

#if defined(__cplusplus)
extern "C"{
#endif
    //分享结果回调函数类型声明
    typedef void (*DoShareCallBack)(DoShareType shareType, int resultStatus);
    
    //分享
    extern void iosDoShare(DoShareType shareType, const char *appID, const char *title, const char *description, const char *imagePath);
    
    //注册回调函数
    extern void iosSetShareListener(DoShareCallBack callback);
#if defined(__cplusplus)
}
#endif


这里调用OC方法时使用NSClassFromString动态加载某个类,这样做就不用在该文件中import三方SDK的头文件了。当只需要接入某几个SDK的时候,也不需要改动该文件,只要不加载那些不需要的SDK就可以了。注意:需要在Other Lind Flag中加入 “-all_load”,或者-force_load指定库文件
,保证需要的类已被加载。

类似方法还有NSGetSizeAndAlignment、NSSelectorFromString、NSStringFromClass、NSStringFromSelector.

//
//  DoShare.m
//  DoShare
//
//  Created by zhai chunlin on 15/11/11.
//  Copyright (c) 2015年 YA. All rights reserved.
//

#import "DoShare.h"
#import "DoshareNotification.h"

@interface DoShare : NSObject

@end

#if defined(__cplusplus)
"C"{
#endif
    
    DoShareCallBack _callBack;
    
    DoShare *_share;
    
    static id _objc = nil;
    
    //字符串转化的工具函数
    NSString *_CreateNSString(const char *string)
    {
        if (string) {
            
            return [NSString stringWithUTF8String:string];
        } else {
            
            return @"";
        }
    }
    
    void iosDoShare(DoShareType shareType, const char *appID, const char *title, const char *description, const char *imagePath)
    {
        if (!_share) {
            
            _share = [[DoShare alloc] init];
        }
        
        NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
        
        [mDict setValue:_CreateNSString(appID) forKey:@"appID"];
        
        [mDict setValue:_CreateNSString(title) forKey:@"title"];
        
        [mDict setValue:_CreateNSString(description) forKey:@"description"];
        
        [mDict setValue:_CreateNSString(imagePath) forKey:@"imagePath"];
        
        if (shareType == DoWXShare) {
            
            Class myClass = NSClassFromString(@"DoWXShare");
            
            if (myClass) {
                
                if (!_objc) {
                    
                    _objc = [[myClass alloc] init];
                }
                
                [[NSNotificationCenter defaultCenter] postNotificationName:kDoShareWXShareNotification object:nil userInfo:mDict];
            }
        } else if (shareType == DoFacebookShare) {
            
            Class myClass = NSClassFromString(@"DoFacebookShare");
            
            if (myClass) {
                
                if (!_objc) {
                    
                    _objc = [[myClass alloc] init];
                }
                
                [[NSNotificationCenter defaultCenter] postNotificationName:kDoShareFacebookShareNotification object:nil userInfo:mDict];
            }
        } else if (shareType == DoLineShare) {
            
            Class myClass = NSClassFromString(@"DoLineShare");
            
            if (myClass) {
                
                if (!_objc) {
                    
                    _objc = [[myClass alloc] init];
                }
                
                [[NSNotificationCenter defaultCenter] postNotificationName:kDoShareLineShareNotification object:nil userInfo:mDict];
            }
        }
    }
    
    void iosSetShareListener(DoShareCallBack callback)
    {
        _callBack = callback;
    }
    
#if defined(__cplusplus)
}
#endif

@implementation DoShare

- (instancetype)init
{
    if (self = [super init]) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DoShareResultNotification:) name:kDoShareResultNotification object:nil];
    }
    
    return self;
}

- (void)DoShareResultNotification:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
    
    int type = [[dict objectForKey:@"shareType"] intValue];
    
    DoShareType shareType;
    
    switch (type) {
        case 0:
            shareType = DoWXShare;
            break;
        case 1:
            shareType = DoFacebookShare;
            break;
        case 2:
            shareType = DoLineShare;
            break;
        default:
            break;
    }
    
    BOOL result = [[dict objectForKey:@"errorCode"] boolValue];
    
    NSLog(@"errorCode = %d",[[dict objectForKey:@"errorCode"] boolValue]);
    
    _callBack(shareType,result);
}

@end

在做台湾Line分享图片的时候遇到了UIPasteboard以前没有用过,这个东西是创建剪切板,如果是系统级别的可以在应用间共享剪切板内容,具体可以自己了解一下。

- (void)DoLineShare:(NSNotification *)notification
{
    NSDictionary *dict = [notification userInfo];
    
    NSString *imagePath = [dict objectForKey:@"imagePath"];
    
    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
    
    pasteBoard.image = [UIImage imageWithContentsOfFile:imagePath];
    
    NSString *linePath = @"line://msg/image";
    
    NSString *finalPath = [NSString stringWithFormat:@"%@/%@",linePath,pasteBoard.name];
    
    _path = finalPath;
    
    NSLog(@"finalPath = %@",finalPath);
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:finalPath]];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值