如何在Unity中获取IOS应用商店国家

问题描述 投票:0回答:1

一天中的好时光

我使用的是Unity IAP版本2.2.2

我想获取 IOS 应用商店的国家/地区。

这里我在Apple文档中看到产品包含priceLocale来获取区域设置。但在unity iap中,产品的产品信息中不包含该字段。

com.example.puzzles.annual: {"introductoryPrice":"0","introductoryPriceLocale":"USD","introductoryPriceNumberOfPeriods":"1","numberOfUnits":"1","unit":"1"}

如何获取iOS应用商店的国家/地区? 附: IP 位置或从 isoCurrencyCode 获取国家/地区对我来说不是一个选择

ios unity-game-engine in-app-purchase
1个回答
0
投票

致那些想要在 IOS 上获得 App store 国家/地区的人。

创建一个 C# 类 MyNativeBindings.cs

public class MyNativeBindings
{
    [DllImport("__Internal")]
    public static extern void LoadLocationInfo(string productId);
    [DllImport("__Internal")]
    public static extern string GetLocationCode();
}

创建ios原生objective-c类MyNativeBindings.mm并移动到Asset/Plugins/ios文件夹

#import <StoreKit/StoreKit.h>
#include <string>

@class ProductRequestResponseDelegate;
@interface ProductRequestResponseDelegate : NSObject  <SKProductsRequestDelegate>;
@property (nonatomic) std::function<void(const std::string  &type, const std::string &country_code)> callback;

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response;

@end

static ProductRequestResponseDelegate * g_delegate = nil;

extern "C" {

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    // Helper method to create C string copy
    char* MakeStringCopy (NSString* nsstring)
    {
        if (nsstring == NULL) {
            return NULL;
        }
        // convert from NSString to char with utf8 encoding
        const char* string = [nsstring cStringUsingEncoding:NSUTF8StringEncoding];
        if (string == NULL) {
            return NULL;
        }

        // create char copy with malloc and strcpy
        char* res = (char*)malloc(strlen(string) + 1);
        strcpy(res, string);
        return res;
    }

    void LoadLocationInfo(const char* productId) {
        g_delegate = [[ProductRequestResponseDelegate alloc] init];
        g_delegate.callback = [](const std::string & type, const std::string & code) {
//            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults registerDefaults:@{@"loc_type": [NSString stringWithUTF8String:type.c_str()]}];
            [userDefaults registerDefaults:@{@"loc_code": [NSString stringWithUTF8String:code.c_str()]}];
            NSLog(@"%@",[userDefaults objectForKey:@"loc_type"]);
            NSLog(@"%@",[userDefaults objectForKey:@"loc_code"]);
        };
        
        auto set = [[NSSet alloc] initWithObjects: [NSString stringWithUTF8String: productId], nil];
        
        auto request = [[SKProductsRequest alloc] initWithProductIdentifiers: set];
        request.delegate = g_delegate;
        [request start];
    }

    char* GetLocationType()
    {
        NSString *s = [userDefaults objectForKey:@"loc_type"];
        return MakeStringCopy(s);
    }

    char* GetLocationCode()
    {
        NSString *s = [userDefaults objectForKey:@"loc_code"];
        return MakeStringCopy(s);
    }
}



@implementation ProductRequestResponseDelegate

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (!self.callback)
            return;
        
        auto products = [response products];
        
        if ([products count] == 0)
        {
            self.callback("", "");
            return;
        }
        
        auto product = [products firstObject];
        if (![product respondsToSelector: @selector(priceLocale)])
        {
            self.callback("", "");
            return;
        }
        
        auto locale = [product priceLocale];
        if ([locale respondsToSelector: @selector(countryCode)])
            self.callback("code", [[locale countryCode] UTF8String]);
        else
            self.callback("identifier", [[locale localeIdentifier] UTF8String]);
    });
}



@end
  1. 您应该在以下情况下调用 MyNativeBindings.LoadLocationInfo(productID) IAP 已初始化。
  2. 然后调用 MyNativeBindings.GetLocationCode() 获取商店国家短代码。
© www.soinside.com 2019 - 2024. All rights reserved.