使用 Unity 來(lái)實(shí)現(xiàn) iOS 原生彈框

原帖地址

目標(biāo)
本文的主要的目標(biāo)是幫助你使用 Unity 創(chuàng)建 iOS 原生彈框。

你會(huì)得到的最終效果如下圖




你想要遵循 iOS 的標(biāo)準(zhǔn)來(lái)顯示彈框嗎?

你想要移除額外的圖形來(lái)減小你構(gòu)建應(yīng)用的大小嗎?

你想要從 Unity 中顯示原生的彈框來(lái)提高用戶體驗(yàn)嗎?

如果你有這些疑慮,那么現(xiàn)在你來(lái)對(duì)地方了。在這篇博客中,我將使用 Unity 創(chuàng)建 iOS 原生彈框。

第一步 介紹

彈框是一種小的遮擋或者提示用戶做一些操作的警告信息。

在這兒,我們將創(chuàng)建3種類型的彈框

類型 行為
消息 彈框 單一行為
確認(rèn) 彈框 兩種行為
評(píng)價(jià)我們 彈框 三種行為

現(xiàn)在讓我們創(chuàng)建一些簡(jiǎn)單的彈窗吧!

第二步 在 Unity 中設(shè)置場(chǎng)景

創(chuàng)建新的 Unity 工程,然后保存場(chǎng)景到你的資源文件夾中。

為三個(gè)彈框創(chuàng)建三個(gè)按鈕

第三步 創(chuàng)建腳本然后分配所有按鈕的引用

創(chuàng)建一個(gè)腳本然后給它命名。我命名為 PopupView.cs ,現(xiàn)在讓我們?cè)诖a中添加一個(gè)按鈕點(diǎn)擊的監(jiān)聽(tīng)事件。

為每一個(gè)按鈕創(chuàng)建一個(gè)方法并且在按鈕點(diǎn)擊事件添加引用。從 iOS 的對(duì)話行為中返回一個(gè)枚舉存儲(chǔ)消息的狀態(tài)。

public enum MessageState
{
    OK,
    YES,
    NO,
    RATED,
    REMIND,
    DECLINED,
    CLOSED
}   

#region PUBLIC_VARIABLES

//在你想要評(píng)價(jià)的應(yīng)用創(chuàng)建一個(gè)變量來(lái)保持 appID
public string appleId = "925623445";
#endregion

#region BUTTON_EVENT_LISTENER

// 會(huì)話按鈕點(diǎn)擊事件
public void OnDialogPopUp() {
    NativeDialog dialog = new NativeDialog("TheAppGuruz", "Do you wants to know about TheAppGuruz");
    dialog.SetUrlString("http://theappguruz.com/");
    dialog.init();
}

// 評(píng)價(jià)按鈕點(diǎn)擊事件
public void OnRatePopUp()
{
    NativeRateUS ratePopUp = new NativeRateUS("Like this game?", "Please rate to support future updates!");
    ratePopUp.SetAppleId(appleId);
    ratePopUp.InitRateUS();
}

// 消息按鈕點(diǎn)擊事件
public void OnMessagePopUp()
{
    NativeMessage msg = new NativeMessage("TheAppGuruz", "Welcome To TheAppGuruz");
}
#endregion

現(xiàn)在讓我們?yōu)樵膹椏蛐袨樽?cè)委托事件監(jiān)聽(tīng)者。

#region UNITY_DEFAULT_CALLBACKS

void OnEnable()
{
    // 注冊(cè)所有的委托事件監(jiān)聽(tīng)者
    IOSRateUsPopUp.onRateUSPopupComplete += OnRateUSPopupComplete;
    IOSDialog.onDialogPopupComplete += OnDialogPopupComplete;
    IOSMessage.onMessagePopupComplete += OnMessagePopupComplete;
}

void OnDisable()
{
    // 注銷所有的委托事件監(jiān)聽(tīng)者
    IOSRateUsPopUp.onRateUSPopupComplete -= OnRateUSPopupComplete;
    IOSDialog.onDialogPopupComplete -= OnDialogPopupComplete;
    IOSMessage.onMessagePopupComplete -= OnMessagePopupComplete;
}

#endregion

#region DELEGATE_EVENT_LISTENER

// 當(dāng)點(diǎn)擊評(píng)價(jià)彈框的按鈕時(shí)會(huì)輸出不同的狀態(tài)
void OnRateUSPopupComplete(MessageState state)
{
    switch (state)
    {
        case MessageState.RATED:
        Debug.Log("Rate Button pressed");
        break;
        case MessageState.REMIND:
        Debug.Log("Remind Button pressed");
        break;
        case MessageState.DECLINED:
        Debug.Log("Declined Button pressed");
        break;
    }
}

// 當(dāng)點(diǎn)擊會(huì)話彈框的按鈕時(shí)會(huì)輸出不同的狀態(tài)
void OnDialogPopupComplete(MessageState state)
{
    switch (state)
    {
        case MessageState.YES:
        Debug.Log("Yes button pressed");
        break;
        case MessageState.NO:
        Debug.Log("No button pressed");
        break;
    }
}

// 當(dāng)點(diǎn)擊消息彈框的按鈕時(shí)會(huì)輸出不同的狀態(tài)
void OnMessagePopupComplete(MessageState state)
{
    Debug.Log("Ok button Clicked");
}
#endregion

第四步 創(chuàng)建腳本和 Objective-c 代碼的相互作用

現(xiàn)在,創(chuàng)建一個(gè)腳本命名為 IOSNative.cs 來(lái)直接和 iOS 代碼(Objective-c)進(jìn)行交互。


#define DEBUG_MODE

using UnityEngine;
using System.Collections;

#if (UNITY_IPHONE && !UNITY_EDITOR) || DEBUG_MODE
using System.Runtime.InteropServices;
#endif

public class IOSNative
{
    #if (UNITY_IPHONE && !UNITY_EDITOR) || DEBUG_MODE
    [DllImport("__Internal")]
    private static extern void _TAG_ShowRateUsPopUp(string title, string message, string rate, string remind, string declined);

    [DllImport("__Internal")]
    private static extern void _TAG_ShowDialog(string title, string message, string yes, string no);

    [DllImport("__Internal")]
    private static extern void _TAG_ShowMessage(string title, string message, string ok);

    [DllImport("__Internal")]
    private static extern void _TAG_RedirectToAppStoreRatingPage(string appId);

    [DllImport("__Internal")]
    private static extern void _TAG_RedirectToWebPage(string urlString);

    [DllImport("__Internal")]
    private static extern void _TAG_DismissCurrentAlert();

    #endif

public static void showRateUsPopUP(string title, string message, string rate, string remind, string declined)
{
    #if (UNITY_IPHONE && !UNITY_EDITOR) || DEBUG_MODE
    _TAG_ShowRateUsPopUp(title, message, rate, remind, declined);
    #endif
}

public static void showDialog(string title, string message, string yes, string no)
{
    #if (UNITY_IPHONE && !UNITY_EDITOR) || DEBUG_MODE
    _TAG_ShowDialog(title, message, yes, no);
    #endif
}

public static void showMessage(string title, string message, string ok)
{
    #if (UNITY_IPHONE && !UNITY_EDITOR) || DEBUG_MODE
    _TAG_ShowMessage(title, message, ok);
    #endif
}

public static void RedirectToAppStoreRatingPage(string appleId)
{
    #if (UNITY_IPHONE && !UNITY_EDITOR) || DEBUG_MODE
    _TAG_RedirectToAppStoreRatingPage(appleId);
    #endif
}

public static void RedirectToWebPage(string urlString)
{
    #if (UNITY_IPHONE && !UNITY_EDITOR) || DEBUG_MODE
    _TAG_RedirectToWebPage(urlString);
    #endif
}

public static void DismissCurrentAlert()
{
    #if (UNITY_IPHONE && !UNITY_EDITOR) || DEBUG_MODE
    _TAG_DismissCurrentAlert();
    #endif
}
}

第五步 為不同的彈框創(chuàng)建腳本

正如我上面所提到的,我們將創(chuàng)建三種類型的彈框,讓我們創(chuàng)建腳本來(lái)創(chuàng)建不同的彈框。

消息彈框

A) 創(chuàng)建 NativeMessage.cs 腳本為簡(jiǎn)單的消息彈框做一些基本設(shè)置。

public class NativeMessage
{
#region PUBLIC_FUNCTIONS

public NativeMessage(string title, string message)
{
init(title, message, "Ok");
}

public NativeMessage(string title, string message, string ok)
{
init(title, message, ok);
}

private void init(string title, string message, string ok)
{
#if UNITY_IPHONE
IOSMessage.Create(title, message, ok);
#endif
}
#endregion
}

B) 為簡(jiǎn)單消息彈框創(chuàng)建 IOSMessage.cs 腳本

public class IOSMessage : MonoBehaviour
{           
#region DELEGATE
public delegate void OnMessagePopupComplete(MessageState state);
public static event OnMessagePopupComplete onMessagePopupComplete;

    #endregion

    #region DELEGATE_CALLS

    private void RaiseOnMessagePopupComplete(MessageState state)
    {
        if (onMessagePopupComplete != null)
            onMessagePopupComplete(state);
    }

    #endregion

    #region PUBLIC_VARIABLES

    public string title;
    public string message;
    public string ok;

    #endregion

    #region PUBLIC_FUNCTIONS

    public static IOSMessage Create(string title, string message)
    {
        return Create(title, message, "Ok");
    }

    public static IOSMessage Create(string title, string message, string ok)
    {
        IOSMessage dialog;
        dialog = new GameObject("IOSMessagePopUp").AddComponent<IOSMessage>();
        dialog.title = title;
        dialog.message = message;
        dialog.ok = ok;
        
        dialog.init();
        return dialog;
    }

    public void init()
    {
        IOSNative.showMessage(title, message, ok);
    }

    #endregion

    #region IOS_EVENT_LISTENER

    public void OnPopUpCallBack(string buttonIndex)
    {
        RaiseOnMessagePopupComplete(MessageState.OK);
        Destroy(gameObject);
    }

    #endregion
}

確認(rèn)彈框

A) 創(chuàng)建 NativeDialog.cs 腳本為會(huì)話的消息彈框做一些基本設(shè)置。

public class NativeDialog
{
    #region PUBLIC_VARIABLES
    string title;
    string message;
    string yesButton;
    string noButton;
    public string urlString;
    #endregion

    #region PUBLIC_FUNCTIONS
    public NativeDialog(string title, string message)
    {
        this.title = title;
        this.message = message;
        this.yesButton = "Yes";
        this.noButton = "No";
    }

    public NativeDialog(string title, string message, string yesButtonText, string noButtonText)
    {
        this.title = title;
        this.message = message;
        this.yesButton = yesButtonText;
        this.noButton = noButtonText;
    }

    public void SetUrlString(string urlString)
    {
        this.urlString = urlString;
    }

    public void init()
    {
        #if UNITY_IPHONE
        IOSDialog dialog = IOSDialog.Create(title, message, yesButton, noButton);
        dialog.urlString = urlString;
        #endif
    }
    #endregion
}

B) 為會(huì)話消息彈框創(chuàng)建 IOSDialog.cs 腳本

public class IOSDialog : MonoBehaviour
{
    #region DELEGATE
    public delegate void OnDialogPopupComplete(MessageState state);
    public static event OnDialogPopupComplete onDialogPopupComplete;
    #endregion

    #region DELEGATE_CALLS
    private void RaiseOnOnDialogPopupComplete(MessageState state)
    {
        if (onDialogPopupComplete != null)
            onDialogPopupComplete(state);
    }
    #endregion

    #region PUBLIC_VARIABLES
    public string title;
    public string message;
    public string yes;
    public string no;
    public string urlString;
    #endregion

    #region PUBLIC_FUNCTIONS
    // Constructor
    public static IOSDialog Create(string title, string message)
    {
        return Create(title, message, "Yes", "No");
    }

    public static IOSDialog Create(string title, string message, string yes, string no)
    {
        IOSDialog dialog;
        dialog = new GameObject("IOSDialogPopUp").AddComponent<IOSDialog>();
        dialog.title = title;
        dialog.message = message;
        dialog.yes = yes;
        dialog.no = no;
        dialog.init();
        return dialog;
    }

    public void init()
    {
        IOSNative.showDialog(title, message, yes, no);
    }
    #endregion

    #region IOS_EVENT_LISTENER
    public void OnDialogPopUpCallBack(string buttonIndex)
    {
        int index = System.Convert.ToInt16(buttonIndex);
        switch (index)
        {
            case 0: 
                IOSNative.RedirectToWebPage(urlString);
                RaiseOnOnDialogPopupComplete(MessageState.YES);
                break;
            case 1: 
                RaiseOnOnDialogPopupComplete(MessageState.NO);
                break;
        }
        Destroy(gameObject);
    }
    #endregion
}

評(píng)價(jià)我們彈框

A) 創(chuàng)建 NativeRateUS.cs 腳本為評(píng)價(jià)我們的彈框做一些基本設(shè)置。

public class NativeRateUS
{
   #region PUBLIC_VARIABLES
    public string title;
    public string message;
    public string yes;
    public string later;
    public string no;
    public string appleId;
    #endregion

    #region PUBLIC_FUNCTIONS
    // Constructor
    public NativeRateUS(string title, string message)
    {
        this.title = title;
        this.message = message;
        this.yes = "Rate app";
        this.later = "Later";
        this.no = "No, thanks";
    }

    // Constructor
    public NativeRateUS(string title, string message, string yes, string later, string no)
    {
        this.title = title;
        this.message = message;
        this.yes = yes;
        this.later = later;
        this.no = no;
    }
    // Set AppID to rate app
    public void SetAppleId(string _appleId)
    {
        appleId = _appleId;
    }
    
    // Initialize rate popup
    public void InitRateUS()
    {
        #if UNITY_IPHONE
        IOSRateUsPopUp rate = IOSRateUsPopUp.Create(title, message, yes, later, no);
        rate.appleId = appleId;
        #endif
    }
    #endregion
}

B) 為評(píng)價(jià)我們彈框創(chuàng)建 IOSRateUsPopUp.cs 腳本

public class IOSRateUsPopUp : MonoBehaviour
{
    #region DELEGATE
    public delegate void OnRateUSPopupComplete(MessageState state);
    public static event OnRateUSPopupComplete onRateUSPopupComplete;
    #endregion

    #region DELEGATE_CALLS
    private void RaiseOnOnRateUSPopupComplete(MessageState state)
    {
        if (onRateUSPopupComplete != null)
            onRateUSPopupComplete(state);
    }

    #endregion

    #region PUBLIC_VARIABLES
    public string title;
    public string message;
    public string rate;
    public string remind;
    public string declined;
    public string appleId;
    #endregion

    #region PUBLIC_FUNCTIONS
    public static IOSRateUsPopUp Create()
    {
        return Create("Like the Game?", "Rate US");
    }

    public static IOSRateUsPopUp Create(string title, string message)
    {
        return Create(title, message, "Rate Now", "Ask me later", "No, thanks");
    }

    public static IOSRateUsPopUp Create(string title, string message, string rate, string remind, string declined)
    {
        IOSRateUsPopUp popup = new GameObject("IOSRateUsPopUp").AddComponent<IOSRateUsPopUp>();
        popup.title = title;
        popup.message = message;
        popup.rate = rate;
        popup.remind = remind;
        popup.declined = declined;
        popup.init();
        return popup;
    }

    public void init()
    {
        IOSNative.showRateUsPopUP(title, message, rate, remind, declined);
    }
    #endregion

    #region IOS_EVENT_LISTENER
    public void OnRatePopUpCallBack(string buttonIndex)
    {
        int index = System.Convert.ToInt16(buttonIndex);
        switch (index)
        {
            case 0: 
                IOSNative.RedirectToAppStoreRatingPage(appleId);
                RaiseOnOnRateUSPopupComplete(MessageState.RATED);
                break;
            case 1:
                RaiseOnOnRateUSPopupComplete(MessageState.REMIND);
                break;
            case 2:
                RaiseOnOnRateUSPopupComplete(MessageState.DECLINED);
                break;
        }
        Destroy(gameObject);
    }
    #endregion
}

注意
在這些類中(每個(gè)彈框的 B 部分),我們創(chuàng)建了游戲物體并且我們使用游戲物體的名字來(lái)獲得事件的回調(diào)。我們將在下一個(gè)部分(Objective-C 文件 UnitySendMessage())使用這些名字。

第六步 設(shè)置 iOS 文件

你完成了基本的代碼!現(xiàn)在,讓我們用 Objective-C 編碼來(lái)創(chuàng)建彈框

這樣做,創(chuàng)建新的 xcode 工程來(lái)創(chuàng)建 Objective-C 文件。如果你不了解 xcdoe 并不知道怎樣使用 xcode 來(lái)創(chuàng)建工程,那么請(qǐng)看這里 使用 xcode 創(chuàng)建基本的工程。

不要擔(dān)心現(xiàn)在的代碼,你只需要在你的文件中拷貝然后粘貼。如果你在創(chuàng)建工程和文件時(shí)面臨著任何問(wèn)題,那么你可以從博客的底部下載源代碼。只要你下載完了工程,你就可以拷貝所有的 iOS 文件到你的 unity 工程的 Plugins 文件夾中

回到 xcode,創(chuàng)建新的 Objective-C 文件命名為 DataConvertor 來(lái)轉(zhuǎn)換數(shù)據(jù)。

DataConverter.h

#import <Foundation/Foundation.h>
@interface DataConvertor : NSObject
+ (NSString*) charToNSString: (char*)value;
+ (const char *) NSIntToChar: (NSInteger) value;
+ (const char *) NSStringToChar: (NSString *) value;
@end

DataConverter.m

#import "DataConvertor.h"
@implementation 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];
}
@end

創(chuàng)建另一個(gè)文件命名為 IOSNativePopUpsManager 來(lái)從 unity 腳本中調(diào)用,并且顯示彈框。

IOSNativePopUpsManager.h

#import <Foundation/Foundation.h>
#import "DataConvertor.h"
@interface IOSNativePopUpsManager : NSObject
+ (void) unregisterAllertView;
@end

IOSNativePopUpsManager.m

#import "IOSNativePopUpsManager.h"
@implementation IOSNativePopUpsManager
static UIAlertController* _currentAllert = nil;
+ (void) unregisterAllertView {
    if(_currentAllert != nil) {
        _currentAllert = nil;
    }
}

+(void) dismissCurrentAlert {
    if(_currentAllert != nil) {
        [_currentAllert dismissViewControllerAnimated:NO completion:nil];
        _currentAllert = nil;
    }
}

+(void) showRateUsPopUp: (NSString *) title message: (NSString*) msg b1: (NSString*) b1 b2: (NSString*) b2 b3: (NSString*) b3 {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *rateAction = [UIAlertAction actionWithTitle:b1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [IOSNativePopUpsManager unregisterAllertView];
    UnitySendMessage("IOSRateUsPopUp", "OnRatePopUpCallBack", [DataConvertor NSIntToChar:0]);
    }];

    UIAlertAction *laterAction = [UIAlertAction actionWithTitle:b2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [IOSNativePopUpsManager unregisterAllertView];
    UnitySendMessage("IOSRateUsPopUp", "OnRatePopUpCallBack", [DataConvertor NSIntToChar:1]);
    }];

    UIAlertAction *declineAction = [UIAlertAction actionWithTitle:b3 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [IOSNativePopUpsManager unregisterAllertView];
    UnitySendMessage("IOSRateUsPopUp", "OnRatePopUpCallBack", [DataConvertor NSIntToChar:2]);
    }];

    [alertController addAction:rateAction];
    [alertController addAction:laterAction];
    [alertController addAction:declineAction];
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:nil];
    _currentAllert = alertController;
}

+ (void) showDialog: (NSString *) title message: (NSString*) msg yesTitle:(NSString*) b1 noTitle: (NSString*) b2{

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:b1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [IOSNativePopUpsManager unregisterAllertView];
    UnitySendMessage("IOSDialogPopUp", "OnDialogPopUpCallBack", [DataConvertor NSIntToChar:0]);
    }];

    UIAlertAction *noAction = [UIAlertAction actionWithTitle:b2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [IOSNativePopUpsManager unregisterAllertView];
    UnitySendMessage("IOSDialogPopUp", "OnDialogPopUpCallBack", [DataConvertor NSIntToChar:1]);
    }];

    [alertController addAction:yesAction];
    [alertController addAction:noAction];
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:nil];
    _currentAllert = alertController;
}

+(void)showMessage: (NSString *) title message: (NSString*) msg okTitle:(NSString*) b1 {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:b1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [IOSNativePopUpsManager unregisterAllertView];
    UnitySendMessage("IOSMessagePopUp", "OnPopUpCallBack", [DataConvertor NSIntToChar:0]);
    }];

    [alertController addAction:okAction];
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:nil];
    _currentAllert = alertController;
}

extern "C" {
    // Unity Call
    void _TAG_ShowRateUsPopUp(char* title, char* message, char* b1, char* b2, char* b3) {
        [IOSNativePopUpsManager showRateUsPopUp:[DataConvertor charToNSString:title] message:[DataConvertor charToNSString:message] b1:[DataConvertor charToNSString:b1] b2:[DataConvertor charToNSString:b2] b3:[DataConvertor charToNSString:b3]];
    }
    void _TAG_ShowDialog(char* title, char* message, char* yes, char* no) {
        [IOSNativePopUpsManager showDialog:[DataConvertor charToNSString:title] message:[DataConvertor charToNSString:message] yesTitle:[DataConvertor charToNSString:yes] noTitle:[DataConvertor charToNSString:no]];
    }
    void _TAG_ShowMessage(char* title, char* message, char* ok) {
        [IOSNativePopUpsManager showMessage:[DataConvertor charToNSString:title] message:[DataConvertor charToNSString:message] okTitle:[DataConvertor charToNSString:ok]];
    }
    void _TAG_DismissCurrentAlert() {
        [IOSNativePopUpsManager dismissCurrentAlert];
    }
}
@end

注意
在這個(gè)類中,我們使用 UnitySendMessage() 向 unity 發(fā)送一條消息,然后我們使用游戲物體的名字作為參數(shù)。必須和創(chuàng)建的游戲物體,特別是彈框類相匹配。

現(xiàn)在創(chuàng)建一個(gè)新的文件命名為 IOSNativeUtility 來(lái)重定向控制從應(yīng)用程序到評(píng)價(jià)頁(yè)面或者任何其他網(wǎng)頁(yè)。

IOSNativeUtility.h

#import <Foundation/Foundation.h>
#import "DataConvertor.h"
#if UNITY_VERSION < 450
#include "iPhone_View.h"
#endif

@interface IOSNativeUtility : NSObject
@property (strong) UIActivityIndicatorView *spinner;
+ (id) sharedInstance;
- (void) redirectToRatigPage: (NSString *) appId;
@end

IOSNativeUtility.m

#import "IOSNativeUtility.h"
@implementation IOSNativeUtility
static IOSNativeUtility *_sharedInstance;
static NSString* templateReviewURLIOS7 = @"itms-apps://itunes.apple.com/app/idAPP_ID";
NSString *templateReviewURL = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APP_ID";

+ (id)sharedInstance {
    if (_sharedInstance == nil) {
        _sharedInstance = [[self alloc] init];
    }
    return _sharedInstance;
}

-(void) redirectToRatigPage:(NSString *)appId {
    #if TARGET_IPHONE_SIMULATOR
        NSLog(@"APPIRATER NOTE: iTunes App Store is not supported on the iOS simulator. Unable to open App Store page.");
    #else
    NSString *reviewURL;
    NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    if ([[vComp objectAtIndex:0] intValue] >= 7) {
        reviewURL = [templateReviewURLIOS7 stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", appId]];
    } else {
        reviewURL = [templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", appId]];
    }
    NSLog(@"redirecting to iTunes page, IOS version: %i", [[vComp objectAtIndex:0] intValue]);
    NSLog(@"redirect URL: %@", reviewURL);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:reviewURL]];
    #endif
}

-(void) openWebPage:(NSString *)urlString{
    #if TARGET_IPHONE_SIMULATOR
        NSLog(@"APPIRATER NOTE: iTunes App Store is not supported on the iOS simulator. Unable to open App Store page.");
    #else
        NSURL *url = [ [ NSURL alloc ] initWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
    #endif
}

extern "C" {
    void _TAG_RedirectToAppStoreRatingPage(char* appId) {
        [[IOSNativeUtility sharedInstance] redirectToRatigPage: [DataConvertor charToNSString:appId ]];
    }
    void _TAG_RedirectToWebPage(char* urlString){
        [[IOSNativeUtility sharedInstance] openWebPage:[DataConvertor charToNSString:urlString]];
    }
}
@end

現(xiàn)在,從 xcode 工程目錄中把所有的 Objective-C 文件拷貝到 unity 工程的 Plugins 目錄中。

如果你在創(chuàng)建 xcode 工程或 Objective-C 文件時(shí)面臨著任何問(wèn)題,那么你可以從博客的底部下載源代碼。只要你下載完了工程,你就可以拷貝所有的 Objective-C 文件到你的 unity 工程的 Plugins/iOS 文件夾中

我希望這篇博客對(duì)你是有幫助的。如果你對(duì) iOS 原生彈框有任何問(wèn)題或疑惑,那么請(qǐng)自由地在評(píng)論區(qū)發(fā)表評(píng)論。我一定會(huì)盡快回復(fù)你。有一個(gè)游戲開(kāi)發(fā)的想法么?你還在等什么?現(xiàn)在就聯(lián)系我們吧,不久你就會(huì)看到你的想法實(shí)現(xiàn)了。

下載完整代碼
CSDN鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容