android
private static readonly string ShareTextureFilePath = Application.persistentDataPath + "/MLShare.jpg"; //截屏地址
public void SendToWhatsApp(string text, bool isImage, uint shareType, uint id, int itemId = 0) //text 分享文本 isImage 是否有图片 WhatsApp 分享可以文字和图片一起发
{
#if UNITY_ANDROID && !UNITY_EDITOR
try
{
var Intent = new AndroidJavaClass("android.content.Intent");
var ACTION_SEND = Intent.GetStatic<string>("ACTION_SEND");
var EXTRA_TEXT = Intent.GetStatic<string>("EXTRA_TEXT");
var EXTRA_STREAM = Intent.GetStatic<string>("EXTRA_STREAM");
var intent = new AndroidJavaObject("android.content.Intent");
intent.Call<AndroidJavaObject>("setPackage", "com.whatsapp");
intent.Call<AndroidJavaObject>("setAction", ACTION_SEND);
if (text != null && text != "")
{
intent.Call<AndroidJavaObject>("putExtra", EXTRA_TEXT, text);
intent.Call<AndroidJavaObject>("setType", "text/plain");
}
if (isImage)
{
using (AndroidJavaClass uri = new AndroidJavaClass("android.net.Uri"))
{
if(DeviceUtil.GetAndroidVersionCode()>=24)
{
AndroidJavaClass _unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject _currentActivity = _unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
var file = new AndroidJavaObject("java.io.File", ShareTextureFilePath);
AndroidJavaClass fileProvider = new AndroidJavaClass("android.support.v4.content.FileProvider");
var imgurl = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile",_currentActivity,"com.mobile.legends.fileprovider",file);
intent.Call<AndroidJavaObject>("putExtra",EXTRA_STREAM, imgurl);
}
else
{
intent.Call<AndroidJavaObject>("putExtra", EXTRA_STREAM,uri.CallStatic<AndroidJavaObject>("parse", ShareTextureFilePath));
}
intent.Call<AndroidJavaObject>("setType", "image/*");
}
}
if (text != null && text != "" && isImage)
{
intent.Call<AndroidJavaObject>("setType", "*/*");
}
var UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("startActivity", intent);//Intent.CallStatic<object>("createChooser", intent, ""));
}
catch (Exception e)
{
}
#elif (UNITY_IOS || UNITY_IPHONE) && !UNITY_EDITOR
_IOS_SendToWhatsApp(text, ShareTextureFilePath, isImage);
#endif
}
IOS
static WhatsApp* static_WhatsApp = NULL;
void _IOS_SendToWhatsApp(const char *url,const char *imgUrl,bool isImage)
{
if (isImage && imgUrl != NULL) {
@try
{
NSString *nsUrl =[NSString stringWithUTF8String:imgUrl];
if( NULL == static_WhatsApp )
{
static_WhatsApp = [WhatsApp sharedInstance];
}
if( NULL == nsUrl )
{
return;
}
[static_WhatsApp SendToWhatsApp:nsUrl];
}
@catch (NSException *exception)
{
}
@finally
{
}
}
else if (url != NULL) {
NSString *text = [NSString
stringWithFormat:@"whatsapp://send?text=%@",
[[NSString stringWithUTF8String:url] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *newtext = [text stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
NSURL *nsurl = [NSURL URLWithString:newtext];
[[UIApplication sharedApplication] openURL:nsurl];
}
}
WhatsApp.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIViewController.h>
@interface WhatsApp : UIViewController<UIDocumentInteractionControllerDelegate>
{
}
@property(nonatomic,retain) UIDocumentInteractionController *documentationInteractionController;
+ (WhatsApp*)sharedInstance;
- (void) SendToWhatsApp:(NSString*)imgUrl;
@end
WhatsApp.m
#import "WhatsApp.h"
#import <UIKit/UIKit.h>
@implementation WhatsApp
@synthesize documentationInteractionController = __documentationInteractionController;
+ (WhatsApp*)sharedInstance
{
static WhatsApp* _instance = nil;
if (_instance == nil)
{
_instance = [[WhatsApp alloc] init];
}
return _instance;
}
- (void) SendToWhatsApp:(NSString*)imgUrl
{
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){
UIImage *Image =[[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:imgUrl,NSHomeDirectory()]];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.jpg"];
[UIImageJPEGRepresentation(Image, 1.0) writeToFile:savePath atomically:YES];
self.documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
self.documentationInteractionController.UTI = @"net.whatsapp.image";
self.documentationInteractionController.delegate = self;
[self.documentationInteractionController presentOpenInMenuFromRect:CGRectZero inView:UnityGetGLViewController().view animated: YES];
}
}
@end