development

API에서 iPhone IDFA를 검색하는 방법은 무엇입니까?

big-blog 2020. 11. 19. 21:45
반응형

API에서 iPhone IDFA를 검색하는 방법은 무엇입니까?


장치를 얻고 싶습니다 IDFA. iOS 공식 API에서이 정보를 얻는 방법은 무엇입니까?


가장 먼저:

#import <AdSupport/ASIdentifierManager.h> 

NSString으로 가져 오려면 다음을 사용하십시오.

[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]

따라서 코드는 다음과 같습니다.

NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

먼저 사용자 사용자가 광고 추적에서 옵트 아웃하기로 결정했는지 확인 해야합니다 . 그가 허용 한 경우에만 IDFA.

isAdvertisingTrackingEnabled메소드를 호출하여 확인할 수 있습니다 ASIdentifierManager.

isAdvertisingTrackingEnabled

광고 추적을 수행하기 전에이 속성의 값을 확인하십시오. 값이 NO인 경우 게재 빈도 설정, 전환 이벤트, 순 사용자 수 추정, 보안 및 사기 감지, 디버깅 용도로만 광고 식별자를 사용하세요.

다음 코드 조각은 문자열 값을 얻는 방법을 보여줍니다 IDFA.

ObjC

@import AdSupport;

- (NSString *)identifierForAdvertising {
    // Check whether advertising tracking is enabled
    if([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
        NSUUID *identifier = [[ASIdentifierManager sharedManager] advertisingIdentifier];
        return [identifier UUIDString];
    }

    // Get and return IDFA
    return nil;
}

빠른

import AdSupport

func identifierForAdvertising() -> String? {
    // Check whether advertising tracking is enabled
    guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
        return nil
    }

    // Get and return IDFA
    return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}

ASIdentifierManager 는 iOS 6 이상을 실행하는 기기에서 광고 식별 번호를 획득하는 공식적인 방법입니다. -[[ASIdentifierManager sharedManager] advertisingIdentifier];그것을 얻기 위해 사용할 수 있습니다 .


Swift에서 IDFA 받기 :

    import AdSupport

    ...

    let myIDFA: String?
    // Check if Advertising Tracking is Enabled
    if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
        // Set the IDFA
        myIDFA = ASIdentifierManager.sharedManager().advertisingIdentifier.UUIDString
    } else {
        myIDFA = nil
    }

iOS 10부터 사용자가 "광고 추적 제한"을 활성화하면 OS는 "00000000-0000-0000-0000-000000000000"이라는 새 값과 함께 광고 식별자를 함께 전송합니다.

이 기사에 따르면 : https://fpf.org/2016/08/02/ios-10-feature-stronger-limit-ad-tracking/


다음 은 사용자가 광고 추적을 끈 경우 식별자에 대한 개체를 제공하는 Swift 의 주석 처리 된 도우미 클래스입니다nil .

import AdSupport

class IDFA {
    // MARK: - Stored Type Properties
    static let shared = IDFA()

    // MARK: - Computed Instance Properties
    /// Returns `true` if the user has turned off advertisement tracking, else `false`.
    var limited: Bool {
        return !ASIdentifierManager.shared().isAdvertisingTrackingEnabled
    }

    /// Returns the identifier if the user has turned advertisement tracking on, else `nil`.
    var identifier: String? {
        guard !limited else { return nil }
        return ASIdentifierManager.shared().advertisingIdentifier.uuidString
    }
}

Just add it to your project (for example in a file named IDFA.swift) and link the AdSupport.framework in your target via the "Linked Frameworks and Libraries" section in the General settings tab.

Then you can use it like this:

if let identifier = IDFA.shared.identifier {
    // use the identifier
} else {
    // put any fallback logic in here
}

Swift 3 & 4

var IDFA = String()
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
            IDFA = ASIdentifierManager.shared().advertisingIdentifier
}

Just to extend Amro's Swift answer, here's similar code wrapped in a method:

import AdSupport

...

func provideIdentifierForAdvertisingIfAvailable() -> String? {
    if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
      return ASIdentifierManager.sharedManager().advertisingIdentifier?.UUIDString ?? nil
    } else {
      return nil
    }
  }

A nicer approach to get the IDFA or nil if tracking is disabled via iOS Setting is using a (private) extension:

import AdSupport

class YourClass {

    func printIDFA() {
        print(ASIdentifierManager.shared().advertisingIdentifierIfPresent)
    }
}

private extension ASIdentifierManager {

    /// IDFA or nil if ad tracking is disabled via iOS system settings
    var advertisingIdentifierIfPresent: String? {
        if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
            return ASIdentifierManager.shared().advertisingIdentifier.uuidString
        }

        return nil        
}

참고URL : https://stackoverflow.com/questions/12944504/how-to-retrieve-iphone-idfa-from-api

반응형