//
// 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]];
}