Unity calls IOS's StoreKit to achieve star rating and comment on the game inside the game

An OC code on the Xcode side

Create a new empty project in Xcode (you can't do Baidu), then create a .h and .m file, remember to change the suffix of .m to .mm (the difference between .mm file and .m file is that : The .mm file can contain C++ code in addition to Objective-C and C code ), this class should inherit from NSObject

The .h code is as follows:

//

// UnityStoreKit.h

//  test

//

//  Created by HH on 2018/4/13.

//  Copyright © 2018年 HH. All rights reserved.

//


#import <Foundation/Foundation.h>

#import <StoreKit/StoreKit.h>


@interface UnityStoreKit : NSObject


@end

 The .mm code is as follows:

//

//  UnityStoreKit.mm

//  test

//

//  Created by HH on 2018/4/13.

//  Copyright © 2018年 HH. All rights reserved.

//


#import "UnityStoreKit.h"



@implementation UnityStoreKit

#if defined(__cplusplus)

extern"C"{

#endif

    void _goComment()

    {

        if([SKStoreReviewController respondsToSelector:@selector(requestReview)]) {// iOS 10.3 以上支持

            [SKStoreReviewController requestReview];

        } else { // iOS 10.3 之前的使用这个

            NSString *appId = @"1234567890"; //项目在苹果后台的appid

            NSString  * nsStringToOpen = [NSString  stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",appId];//替换为对应的APPID

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];

        }

    }

#if defined(__cplusplus)

}

#endif


@end

在软件内部进行星级评价是在IOS10.3之后的新特性。我们将这俩个文件导出到Unity里面的plugins文件夹下。把这个俩个文件所依赖的StoreKit在Unity里面给勾选上(勾上之后Unity打包成XCode文件的时候会自动把这个库给引用上)并且平台选择成IOS平台(这样打包成IOS的时候才会打包这俩个文件)。如下图所示:


二 Untiy里面的调用代码

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class UnityStoreKitMgr : MonoBehaviour {

    private static UnityStoreKitMgr _instance;
    public static UnityStoreKitMgr Instance{
        get{
            if(_instance==null)
            {
                GameObject go=     new GameObject ("UnityStoreKitMgr");
                _instance=go.AddComponent<UnityStoreKitMgr> ();
                DontDestroyOnLoad (go);
            }
            return _instance;
        }
    }


    [DllImport("__Internal")]
    private static extern void _goComment();
    public  void GoToCommnet()
    {    
        #if UNITY_IPHONE
        _goComment();
        #endif
    }

}

[DllImport("__Internal")]  

 这个我也不是很清楚。。反正就是扩展那一类的貌似是调用dl的一些函数(必写)

_goComment()必须和.mm文件里面的函数要一样。

三 调用的运行的截图



提交按钮是灰色的,是因为现在处于调试状态不用在意。

四  IOS回调Untiy

只有一个方式:

  UnitySendMessage("UnityStoreKitMgr","onCancel","params");

第一个参数:调用的Unity函数所在脚本绑定的游戏物体
第二个参数:调用的Unity函数名称
第三个参数:调用的Unity函数参数(只能是字符串类型,和android一样)




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324599006&siteId=291194637