一开始我以为接入ShareSDK去实现分享功能,没想到要实现IOS的原生分享去实现这个功能。本人对OC一点都不熟悉,一脸蒙蔽。然后我开始在网上去找资料,发现一篇Unity实现IOS的截屏分享功能,然后我改一下他的源代码去实现了这个功能。代码如下(功能是实现了,但是不熟悉OC,源代码不敢修改的太多,有空再来搞)。
一 在Unity中声明OC中的函数并且调用OC函数
using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_IPHONE && !UNITY_EDITOR using System.Runtime.InteropServices; #endif public class GJCNativeShare : MonoBehaviour { //声明OS函数 #if UNITY_IPHONE && !UNITY_EDITOR [DllImport ("__Internal")] private static extern void _GJC_NativeShare(string text, string encodedMedia); #endif //回调的委托 public delegate void OnShareSuccess(string platform); public delegate void OnShareCancel(string platform); public OnShareSuccess onShareSuccess = null; public OnShareCancel onShareCancel = null; //单利 private static GJCNativeShare _instance = null; public static GJCNativeShare Instance { get { if (_instance == null) { _instance = GameObject.FindObjectOfType(typeof(GJCNativeShare)) as GJCNativeShare; if (_instance == null) { _instance = new GameObject ().AddComponent<GJCNativeShare> (); _instance.gameObject.name = _instance.GetType ().FullName; } } return _instance; } } /// <summary> /// 调用OC函数 /// </summary> public void NativeShare() { Debug.Log("NativeShare"); #if UNITY_IPHONE && !UNITY_EDITOR _GJC_NativeShare("", ""); #endif } /// <summary> /// 分享成功的回调 /// </summary> /// <param name="result">Result.</param> private void OnNativeShareSuccess(string result){ // Debug.Log("success: " + result); if (onShareSuccess!=null){ onShareSuccess(result); } } /// <summary> /// 分享失败的回到 /// </summary> /// <param name="result">Result.</param> private void OnNativeShareCancel(string result){ // Debug.Log("cancel: " + result); if (onShareCancel != null){ onShareCancel(result); } } }
2 在Xcode中编写GJCSocialShare.h文件
#import <UIKit/UIKit.h> @interface GJCSocialShare : NSObject + (id) sharedInstance; - (void) nativeShare:(NSString*)text media: (NSString*) media; @end @interface GJC_DataConvertor : NSObject + (NSString*) charToNSString: (char*)value; + (const char*) NSIntToChar: (NSInteger) value; + (const char*) NSStringToChar: (NSString *) value; + (NSArray*) charToNSArray: (char*)value; + (const char*) serializeErrorWithData:(NSString *)description code: (int) code; + (const char*) serializeError:(NSError *)error; + (NSMutableString *) serializeErrorWithDataToNSString:(NSString *)description code: (int) code; + (NSMutableString *) serializeErrorToNSString:(NSError *)error; + (const char*) NSStringsArrayToChar:(NSArray *) array; + (NSString *) serializeNSStringsArray:(NSArray *) array; @end
3 在Xcode中编写GJCSocialShare.mm文件
#import "GJCSocialShare.h" #import <Social/Social.h> NSString * const STR_SPLITTER = @"|"; NSString * const STR_EOF = @"endofline"; NSString * const STR_ARRAY_SPLITTER = @"%%%"; @implementation GJCSocialShare static GJCSocialShare * gjc_sharedInstance; + (id)sharedInstance { if (gjc_sharedInstance == nil) { gjc_sharedInstance = [[self alloc] init]; } return gjc_sharedInstance; } -(void) nativeShare:(NSString *)text media:(NSString *)media { NSLog(@"==============>nativeShare"); //UIImage *shareImg = [UIImage imageNamed:@"9.jpg"]; // UIImage *shareImg1 = [UIImage imageNamed:@"14.jpg"]; NSString *shareTitle = @"分享 触不到的你 下载链接"; NSURL *shareURL = [NSURL URLWithString:@"http://www.baidu.com"]; NSArray *activityItems = @[shareURL,shareTitle]; // UIActivityViewController *activeViewController = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil]; UIActivityViewController *controller; controller =[[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil]; UIViewController *vc = UnityGetGLViewController(); NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."]; if ([[vComp objectAtIndex:0] intValue] >= 8) { UIPopoverPresentationController *presentationController = [controller popoverPresentationController]; presentationController.sourceView = vc.view; presentationController.sourceRect = CGRectMake(vc.view.bounds.origin.x+vc.view.bounds.size.width/2, vc.view.bounds.origin.y+vc.view.bounds.size.height, 0, 0); presentationController.permittedArrowDirections = 0; } [vc presentViewController:controller animated:YES completion:nil]; controller.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) { // NSLog(@"activityType :%@", activityType); if (completed){ //回调Unity函数 UnitySendMessage("GJCNativeShare", "OnNativeShareSuccess", [GJC_DataConvertor NSStringToChar:activityType]); // NSLog(@"completed!"); }else{ if (activityType != nil){ //回调Unity函数 UnitySendMessage("GJCNativeShare", "OnNativeShareCancel", [GJC_DataConvertor NSStringToChar:activityType]); }else{ //回调Unity函数 UnitySendMessage("GJCNativeShare", "OnNativeShareCancel", [GJC_DataConvertor NSStringToChar:@""]); } // NSLog(@"cancel!"); } }; } @end @implementation GJC_DataConvertor +(NSString *) charToNSString:(char *)value { if (value != NULL) { return [NSString stringWithUTF8String: value]; } else { return [NSString stringWithUTF8String: ""]; } } +(const char*)NSIntToChar:(NSInteger)value { NSString *tmp = [NSString stringWithFormat:@"%ld", (long)value]; return [tmp UTF8String]; } + (const char*) NSStringToChar:(NSString *)value { return [value UTF8String]; } + (NSArray *)charToNSArray:(char*)value { NSString* strValue = [GJC_DataConvertor charToNSString:value]; NSArray *array; if([strValue length] == 0) { array = [[NSArray alloc] init]; } else { array = [strValue componentsSeparatedByString:STR_ARRAY_SPLITTER]; } return array; } + (const char*) NSStringsArrayToChar:(NSArray *) array { return [GJC_DataConvertor NSStringToChar:[GJC_DataConvertor serializeNSStringsArray:array]]; } + (NSString *) serializeNSStringsArray:(NSArray *) array { NSMutableString * data = [[NSMutableString alloc] init]; for(NSString* str in array) { [data appendString:str]; [data appendString: STR_ARRAY_SPLITTER]; } [data appendString: STR_EOF]; NSString *str = [data copy]; #if UNITY_VERSION < 500 [str autorelease]; #endif return str; } + (NSMutableString *)serializeErrorToNSString:(NSError *)error { NSString* description = @""; if(error.description != nil) { description = error.description; } return [self serializeErrorWithDataToNSString:description code: (int) error.code]; } + (NSMutableString *)serializeErrorWithDataToNSString:(NSString *)description code:(int)code { NSMutableString * data = [[NSMutableString alloc] init]; [data appendFormat:@"%i", code]; [data appendString: STR_SPLITTER]; [data appendString: description]; return data; } + (const char*) serializeErrorWithData:(NSString *)description code: (int) code { NSString *str = [GJC_DataConvertor serializeErrorWithDataToNSString:description code:code]; return [GJC_DataConvertor NSStringToChar:str]; } + (const char*) serializeError:(NSError *)error { NSString *str = [GJC_DataConvertor serializeErrorToNSString:error]; return [GJC_DataConvertor NSStringToChar:str]; } @end extern "C" { void _GJC_NativeShare(char* text, char* encodedMedia) { NSString *status = [GJC_DataConvertor charToNSString:text]; NSString *media = [GJC_DataConvertor charToNSString:encodedMedia]; [[GJCSocialShare sharedInstance] nativeShare:status media:media]; } }
4 然后将写好的.h和.mm文件导入到Unity中Plugins文件中,并且勾上Social Framework dependences 这个库。