Unity3d whatsapp 分享接入代码 android and ios

news/2024/7/20 21:50:01 标签: unity3d, whatsapp, ios

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

http://www.niftyadmin.cn/n/1339125.html

相关文章

unity3d 获取蓝牙 状态 android

C# // 返回值&#xff1a;// -5: 未知// -4: (iOS)复位// -3: 没有蓝牙权限// -2: (Android)获取蓝牙适配器错误&#xff0c;(iOS)不支持蓝牙// -1: 蓝牙未开启// 0: 蓝牙已开启&#xff0c;未连接// 1: (Android)蓝牙连接中// 2: (Android)蓝牙已连接public static int Get…

layer弹出信息框API

首先向大家推荐layer&#xff0c;在这里也非常感谢贤心的贡献&#xff0c;非常不错的信息框及弹出层解决方案&#xff0c;为一些项目的前端开发提高了很大的效率&#xff0c;希望layer 越办越好&#xff01; 下面是API&#xff0c;呵呵&#xff0c;官方抄袭过来的&#xff0c;为…

unity3d 判断是否安装指定app android and ios

调用 IsInstallApp("jp.naver.line.android","line://") IsInstallApp("com.whatsapp","whatsapp://") C# static Dictionary<string, bool> IsInstallAppTable new Dictionary<string, bool>();public static bool Is…

EXCEL图片导出

2019独角兽企业重金招聘Python工程师标准>>> public void setPicture1(){ String str "E:/a.jpg"; String str1 "E:/b.jpg"; FileOutputStream fileOut null; BufferedImage bufferImg null; …

unity3d iPhone X 适屏代码 屏幕适应方案

添加获取手机型号方法 #import <sys/utsname.h>(NSString*)getDeviceVersion {struct utsname systemInfo;uname(&systemInfo);NSString *deviceVersion [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];return deviceVersion; }在…

[译] 现代垃圾回收

关于 Go 语言最新的垃圾回收器&#xff08;garbage collector&#xff09;&#xff0c;我最近阅读了许多篇赞扬它的文章&#xff0c;但是它们都让我将信将疑&#xff0c;其中的不少来自 Go 语言的官方团队博客。他们像是暗示着在垃圾回收领域已经发生了一个巨大的突破。 以下是…

unity3d 获取使用内存大小 android and ios

获取的是应用在移动设备上面的实时允许是的进程使用内存大小和你直接在移动设备上查看进程的内存使用一模一样&#xff0c;绝对精确&#xff0c;废话不说上代码&#xff0c;你懂的 C# public static int GetUseMemory(){int memory -1; #if UNITY_ANDROIDtry{AndroidJavaCla…