Unity3d whatsapp 分享接入代码 android and 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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值