iOS分享到Facebook/微信/Line C++接口
2015-11-13 00:37
453 查看
//
// 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
{
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]];
}
// 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]];
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 关于指针的一些事情
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- fastjson处理接口时的一个问题
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- c++ primer 第五版 笔记前言
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- share_ptr的几个注意点
- 局域网与广域网接口标准
- RJ-45接口信号定义
- DVI 视频接口图文解析
- 如何设置域名接口
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- 通过C#实现自动售货机接口