【解决】Xcode11 适配iOS14 beta IDFA,并发布上线

一、问题:解决在Xcode11适配 iOS14 的问题

解决

  1. 我们使用Xcode11没有iOS14的库,
  2. Xcode12 beta无法上架
  3. 将Xcode12 beta中的系统库导入到 Xcode11 中,提交会被拒

二、思路:

其实很简单,就是将Xcode12的系统库Copy到 xcode11的同级系统库的位置,然后在代码中调用
⚠️ 注意:我们不能直接调用,需要借助runtime的反射间接调用

三、解决方案:

第一步:

我们需要将Xcode12 Beta版本中的 AppTrackingTransparency.framework 复制(Copy)到Xcode11 的同级目录下
Xcode12 Beta 版本中的 AppTrackingTransparency.framework 路径:

真机:/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/AppTrackingTransparency.framework
模拟器:/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/AppTrackingTransparency.framework

⚠️要把 静态库 Copy 到Xcode11的相同路径下,Xcode11路径跟上面的路径一致

第二步:

在项目 - target - Build Phases - Link Binary With Libraries中导入 AppTrackingTransparency.framework 系统库

第三步:

由于我们n在低版本的Xcode中使用 高版本的系统库,所以我们需要通过runtime的反射去使用系统库
我们需要创建一个对应的Runtime反射类 bATTrackingManager.h
.h 文件 bATTrackingManager.h 完全 Copy ATTrackingManager.h中的内容,并将类名由 ATTrackingManager 改为 bATTrackingManager,代码如下

#import 
#import 
typedef NS_ENUM(NSUInteger, ATTrackingManagerAuthorizationStatus) {
    ATTrackingManagerAuthorizationStatusNotDetermined = 0,
    ATTrackingManagerAuthorizationStatusRestricted,
    ATTrackingManagerAuthorizationStatusDenied,
    ATTrackingManagerAuthorizationStatusAuthorized
} NS_SWIFT_NAME(ATTrackingManager.AuthorizationStatus) API_AVAILABLE(ios(14), macosx(10.16), tvos(14));
API_AVAILABLE(ios(14), macosx(10.16), tvos(14))
@interface bATTrackingManager : NSObject
/*!
 * @property trackingAuthorizationStatus
 *
 * @abstract
 * Returns information about your application’s tracking authorization status.
 * Users are able to grant or deny developers tracking privileges on a per-app basis.
 * Application developers must call `requestTrackingAuthorizationWithCompletionHandler:` for the ability to track users.
 *
 * @result
 * The current authorization status. If the user has not yet been prompted to approve access, the return value will either be
 * ATTrackingManagerAuthorizationStatusNotDetermined, or ATTrackingManagerAuthorizationStatusRestricted if this value is managed.
 * Once the user has been prompted, the return value will be either ATTrackingManagerAuthorizationStatusDenied or ATTrackingManagerAuthorizationStatusAuthorized.
 */
@property (class, nonatomic, readonly, assign) ATTrackingManagerAuthorizationStatus trackingAuthorizationStatus;
/*!
 * @method requestTrackingAuthorizationWithCompletionHandler:completion:
 *
 * @abstract
 * Request user tracking authorization with a completion handler returning the user's authorization status.
 * Users are able to grant or deny developers tracking privileges on a per-app basis.
 * This method allows developers to determine if access has been granted. On first use, this method will prompt the user to grant or deny access.
 *
 * The completion handler will be called with the result of the user's decision for granting or denying permission to use application tracking.
 * The completion handler will be called immediately if access to request authorization is restricted.
 */
+ (void)requestTrackingAuthorizationWithCompletionHandler:(void(^)(ATTrackingManagerAuthorizationStatus status))completion;
// This class, at this time, should not be instantiated.
+ (instancetype)new NS_UNAVAILABLE;
// This class, at this time, should not be instantiated.
- (instancetype)init NS_UNAVAILABLE;
@end

bATTrackingManager.m文件实现需要做两个处理*

#import "bATTrackingManager.h"
#import 
#import 
@implementation bATTrackingManager
+ (ATTrackingManagerAuthorizationStatus)trackingAuthorizationStatus {
   Class ATTrackingManager = NSClassFromString(@"ATTrackingManager");
   if (ATTrackingManager == nil) {
       NSBundle *bundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/AppTrackingTransparency.framework"];
       [bundle load];
       ATTrackingManager = NSClassFromString(@"ATTrackingManager");
   }
   ATTrackingManagerAuthorizationStatus returnValue = ((int (*)(id, SEL)) objc_msgSend)(ATTrackingManager, _cmd);
   return returnValue;
}
+ (void)requestTrackingAuthorizationWithCompletionHandler:(void(^)(ATTrackingManagerAuthorizationStatus status))completion {
   Class ATTrackingManager = NSClassFromString(@"ATTrackingManager");
   if (ATTrackingManager == nil) {
       NSBundle *bundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/AppTrackingTransparency.framework"];
       [bundle load];
       ATTrackingManager = NSClassFromString(@"ATTrackingManager");
   }
   ((void (*)(id, SEL, void(^)(ATTrackingManagerAuthorizationStatus status))) objc_msgSend)(ATTrackingManager, _cmd, completion);
}
@end

下面针对上面的代码解释:

// 动态的链接  AppTrackingTransparency.framework 库
NSBundle *bundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/AppTrackingTransparency.framework"];
[bundle load];
// Runtime反射调用AppTrackingTransparency.framework库
Class ATTrackingManager = NSClassFromString(@"ATTrackingManager");
ATTrackingManagerAuthorizationStatus returnValue = ((int (*)(id, SEL)) objc_msgSend)(ATTrackingManager, _cmd);
Class ATTrackingManager = NSClassFromString(@"ATTrackingManager");
((void (*)(id, SEL, void(^)(ATTrackingManagerAuthorizationStatus status))) objc_msgSend)(ATTrackingManager, _cmd, completion);

第四步:将代码里使用到的 ATTrackingManager 全部替换为 bATTrackingManager

or在需要的地方调用,代码如下:

#import "bATTrackingManager.h"
#import 
#import 
if (@available(iOS 14, *)) {
        // iOS14及以上版本需要先请求权限
        [bATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            // 获取到权限后,依然使用老方法获取idfa
            if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                NSString *idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
                NSLog(@"%@",idfa);
            } else {
                     NSLog(@"请在设置-隐私-Tracking中允许App请求跟踪");
            }
        }];
    } else {
        // iOS14以下版本依然使用老方法
        // 判断在设置-隐私里用户是否打开了广告跟踪
        if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
            NSString *idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
            NSLog(@"%@",idfa);
        } else {
            NSLog(@"请在设置-隐私-广告中打开广告跟踪功能");
        }
    }

到此,完成了Xcode11 对iOS13 AppTrackingTransparency.framework库的支持,可以打包上架了。

AppTrackingTransparency下载地址: https://pan.baidu.com/s/1nEF6vLairv_UkcC_qvOLLw 密码: 8l6w

参考:Xcode11提前适配iOS14 IDFA,并打包上线

你可能感兴趣的:(【解决】Xcode11 适配iOS14 beta IDFA,并发布上线)