iOS分享
2019-01-28 本文已影响0人
Zi_Fi
// SystemShareInterface.h
// Created by zhaozeguang
@interface SystemShareInterface : NSObject {
UIViewController * viewController;
int scriptHandler;
}
+ (SystemShareInterface *)getInstance;
//+ (void)shareAction;
-(void) setViewController:(UIViewController *) viewController;
+(void) shareBySys:(NSDictionary *)dict;
+ (void)registerSysShareCallFun:(NSDictionary *)dict;
@end
#import <UIKit/UIKit.h>
#import <Social/social.h>
#include "SystemShareInterface.h"
#include "cocos2d.h"
#include "scripting/lua-bindings/manual/CCLuaBridge.h"
static SystemShareInterface *s_SystemShareInterface = nil;
@implementation SystemShareInterface : NSObject
+ (SystemShareInterface *)getInstance {
if (s_SystemShareInterface == nil)
{
s_SystemShareInterface = [[SystemShareInterface alloc] init];
s_SystemShareInterface->scriptHandler = 0;
}
return s_SystemShareInterface;
}
-(void) setViewController:(UIViewController *) viewController{
s_SystemShareInterface->viewController = viewController;
}
+ (void)registerSysShareCallFun:(NSDictionary *)dict{
int scriptHandler = [[dict objectForKey:@"scriptHandler"] intValue];
NSLog(@"join registerSysShareCallFun");
if ( s_SystemShareInterface->scriptHandler )
{
cocos2d::LuaBridge::releaseLuaFunctionById(s_SystemShareInterface->scriptHandler);
s_SystemShareInterface->scriptHandler = 0;
}
s_SystemShareInterface->scriptHandler = scriptHandler;
}
#pragma System Share
+(void)shareBySys:(NSDictionary *)dict
{
NSLog(@"join shareBySys");
NSMutableArray* itemArr = [[NSMutableArray alloc] init];
NSString *thumbImage = [[NSBundle mainBundle] pathForResource:@"res/icon" ofType:@"png"];
NSString *shareUrl = [dict objectForKey:@"shareUrl"];
NSString *description = [dict objectForKey:@"description"];
NSString *localImage = [dict objectForKey:@"localImage"];
NSString *iconPath = [dict objectForKey:@"iconPath"];
NSString *platform = [dict objectForKey:@"shareplatform"];
NSString *webImgUrl = [dict objectForKey:@"webImgUrl"];
NSString *localImgArry = [dict objectForKey:@"localImgArry"];
NSLog(@"获取参数");
if (localImage) {
//图片
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfFile:localImage]];
[itemArr addObject:img];
} else if (shareUrl) {
//链接
NSLog(@"shareUrl = %@",shareUrl);
if (iconPath){
NSLog(@"iconPath = %@",iconPath);
NSArray * arr = [iconPath componentsSeparatedByString:@"."];
thumbImage = [[NSBundle mainBundle] pathForResource:arr[0] ofType:arr[1]];
if(!thumbImage){
thumbImage = iconPath;
}
}
NSData * data = [NSData dataWithContentsOfFile:thumbImage];
UIImage *img = [UIImage imageWithData:data];
[itemArr addObject:description];
[itemArr addObject:[NSURL URLWithString:shareUrl]];
[itemArr addObject:img];
}else if(webImgUrl){
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:webImgUrl]]];
[itemArr addObject:img];
}else if(localImgArry){
NSArray * arr = [localImgArry componentsSeparatedByString:@","];
for (int i = 0; i < arr.count; i++) {
NSData * data = [NSData dataWithContentsOfFile:arr[i]];
UIImage *img = [UIImage imageWithData:data];
[itemArr addObject:img];
}
}
NSLog(@"判断类型");
UIActivityViewController* activity = [[UIActivityViewController alloc] initWithActivityItems:itemArr applicationActivities:nil];
if([activity respondsToSelector:@selector(popoverPresentationController)]){
activity.popoverPresentationController.sourceView = s_SystemShareInterface->viewController.view;
}
activity.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
NSLog(@"activityType :%@", activityType);
if (completed) {
NSLog(@"completed");
}
else {
NSLog(@"cancel");
}
cocos2d::LuaBridge::pushLuaFunctionById(s_SystemShareInterface->scriptHandler);
cocos2d::LuaStack *stack = cocos2d::LuaBridge::getStack();
stack->pushString([platform UTF8String]);
stack->pushString([activityType UTF8String]);
stack->pushInt(completed);
stack->pushInt((int)activityError.code);
stack->executeFunction(4);
};
NSLog(@"添加类型");
// if (@available(iOS 11_0, *)) {
// activity.excludedActivityTypes = @[UIActivityTypePostToFacebook,
// UIActivityTypePostToTwitter,
// UIActivityTypePostToWeibo,
// UIActivityTypeMessage,
// UIActivityTypeMail,
// UIActivityTypePrint,
// UIActivityTypeCopyToPasteboard,
// UIActivityTypeAssignToContact,
// UIActivityTypeSaveToCameraRoll,
// UIActivityTypeAddToReadingList,
// UIActivityTypePostToFlickr,
// UIActivityTypePostToVimeo,
// UIActivityTypePostToTencentWeibo,
// UIActivityTypeAirDrop,
// UIActivityTypeOpenInIBooks,
// UIActivityTypeMarkupAsPDF];
// } else if(@available(iOS 9_0, *)) {
// activity.excludedActivityTypes = @[UIActivityTypePostToFacebook,
// UIActivityTypePostToTwitter,
// UIActivityTypePostToWeibo,
// UIActivityTypeMail,
// UIActivityTypePrint,
// UIActivityTypeCopyToPasteboard,
// UIActivityTypeAssignToContact,
// UIActivityTypeSaveToCameraRoll,
// UIActivityTypeAddToReadingList,
// UIActivityTypePostToFlickr,
// UIActivityTypePostToVimeo,
// UIActivityTypePostToTencentWeibo,
// UIActivityTypeAirDrop,
// UIActivityTypeOpenInIBooks];
// }else if(@available(iOS 7_0, *)) {
// activity.excludedActivityTypes = @[UIActivityTypePostToFacebook,
// UIActivityTypePostToTwitter,
// UIActivityTypePostToWeibo,
// UIActivityTypeMail,
// UIActivityTypePrint,
// UIActivityTypeCopyToPasteboard,
// UIActivityTypeAssignToContact,
// UIActivityTypeSaveToCameraRoll,
// UIActivityTypeAddToReadingList,
// UIActivityTypePostToFlickr,
// UIActivityTypePostToVimeo,
// UIActivityTypePostToTencentWeibo,
// UIActivityTypeAirDrop];
// }else if(@available(iOS 6_0, *)) {
// activity.excludedActivityTypes = @[UIActivityTypePostToFacebook,
// UIActivityTypePostToTwitter,
// UIActivityTypePostToWeibo,
// UIActivityTypeMail,
// UIActivityTypePrint,
// UIActivityTypeCopyToPasteboard,
// UIActivityTypeAssignToContact,
// UIActivityTypeSaveToCameraRoll];
// }
NSLog(@"开始调用");
[s_SystemShareInterface->viewController presentViewController:activity animated:YES completion:nil];
// NSLog(@"进入shareAction");
// // 1、设置分享的内容,并将内容添加到数组中
// NSString *shareText = @"我的个人博客";
// UIImage *shareImage = [UIImage imageNamed:@"shareImage.png"];
// NSURL *shareUrl = [NSURL URLWithString:@"http://blog.csdn.net/flyingkuikui"];
// NSArray *activityItemsArray = @[shareText,shareImage,shareUrl];
//
// // 2、初始化控制器,添加分享内容至控制器
// UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItemsArray applicationActivities:nil];
// activityVC.modalInPopover = YES;
// // 3、设置回调
// if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
// // ios8.0 之后用此方法回调
// UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
// NSLog(@"activityType == %@",activityType);
// if (completed == YES) {
// NSLog(@"completed");
// }else{
// NSLog(@"cancel");
// }
// };
// activityVC.completionWithItemsHandler = itemsBlock;
// }else{
// // ios8.0 之前用此方法回调
// UIActivityViewControllerCompletionHandler handlerBlock = ^(UIActivityType __nullable activityType, BOOL completed){
// NSLog(@"activityType == %@",activityType);
// if (completed == YES) {
// NSLog(@"completed");
// }else{
// NSLog(@"cancel");
// }
// };
// activityVC.completionHandler = handlerBlock;
// }
// // 4、调用控制器
// [s_SystemShareInterface->viewController presentViewController:activityVC animated:YES completion:nil];
}
@end