Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中获取iOS设备的广告ID?_C#_Ios_Unity3d - Fatal编程技术网

如何在C#中获取iOS设备的广告ID?

如何在C#中获取iOS设备的广告ID?,c#,ios,unity3d,C#,Ios,Unity3d,我想知道是否有办法通过Unity中的C#脚本获取iOS设备的广告ID 提前感谢。单靠C语言是无法实现的,但你可以编写一个超级简单的插件来实现 创建一个名为getVendorId.mm的文件(或任何您想命名的文件),并将以下代码放入其中: #import <AdSupport/ASIdentifierManager.h> extern "C" { char* _getDeviceVendorId() { NSUUID *adId = [[ASI

我想知道是否有办法通过Unity中的C#脚本获取iOS设备的广告ID

提前感谢。

单靠C语言是无法实现的,但你可以编写一个超级简单的插件来实现

创建一个名为getVendorId.mm的文件(或任何您想命名的文件),并将以下代码放入其中:

    #import <AdSupport/ASIdentifierManager.h>

extern "C"
{
    char* _getDeviceVendorId()
    {
        NSUUID *adId = [[ASIdentifierManager sharedManager] advertisingIdentifier];
        NSString *udid = [adId UUIDString];
        const char *converted = [udid UTF8String];
        char* res = (char*)malloc(strlen(converted) + 1);
        strcpy(res, converted);
        return res;
    }
}
然后,您可以在该脚本中的任意位置调用该方法,如下所示:

string AdvertisingId = _getDeviceVendorId();

有一种更简单的跨平台方式,不需要编写任何插件

public Task<string> GetIDFA()
    {
        var tcs = new TaskCompletionSource<string>();

        if (!Application.RequestAdvertisingIdentifierAsync((idfa, enabled, error) =>
        {
            if (!string.IsNullOrEmpty(error))
            {
                taskCompletionSource.SetException(new Exception(error));
            }
            else if (!enabled)
            {
                taskCompletionSource.SetException(new NotSupportedException("User has disabled advertising tracking"));
            }
            else
            {
                taskCompletionSource.SetResult(idfa);
            }
        }))
        {
            throw new NotSupportedException("Advertising is not supported");
        }

        return taskCompletionSource.Task;
    }
公共任务GetIDFA()
{
var tcs=new TaskCompletionSource();
如果(!Application.RequestAdvertisingIdentifierAsync((idfa,已启用,错误)=>
{
如果(!string.IsNullOrEmpty(错误))
{
SetException(新异常(错误));
}
否则,如果(!已启用)
{
taskCompletionSource.SetException(新的NotSupportedException(“用户已禁用广告跟踪”);
}
其他的
{
taskCompletionSource.SetResult(idfa);
}
}))
{
抛出新的NotSupportedException(“不支持广告”);
}
返回taskCompletionSource.Task;
}

请注意,这将在Unity 2020.1中停止对android工作
public Task<string> GetIDFA()
    {
        var tcs = new TaskCompletionSource<string>();

        if (!Application.RequestAdvertisingIdentifierAsync((idfa, enabled, error) =>
        {
            if (!string.IsNullOrEmpty(error))
            {
                taskCompletionSource.SetException(new Exception(error));
            }
            else if (!enabled)
            {
                taskCompletionSource.SetException(new NotSupportedException("User has disabled advertising tracking"));
            }
            else
            {
                taskCompletionSource.SetResult(idfa);
            }
        }))
        {
            throw new NotSupportedException("Advertising is not supported");
        }

        return taskCompletionSource.Task;
    }