Unity去掉删除libiPhone-lib.a中的UIWebView问题

问题

2020年5月以来,上传到iTunes Connect的ipa文件如包含UIWebView相关引用,会导致ipa包无法通过处理,构建版本不可用。

审核团队在邮件中给出的说明如下:

ITMS-90809: Deprecated API Usage – Apple will stop accepting submissions of apps that use UIWebView APIs . See https://developer.apple.com/documentation/uikit/uiwebview
for more information.

使用find查找工程中相关API:

find . | grep -v .svn  | grep "\.a" | grep -v "\.app" | xargs grep UIWebView

结果指向libiPhone-lib.a文件,该文件是Unity导出工程时生成的系统库文件,说明其中包含UIWebView引用。

解决方案

方案一

升级Unity版本

Unity在Unity2017.4.33跟(2018.4.10)版本中修复了此问题,条件允许可升级Unity版本来解决。

iOS:Fixed Deprecated API Usage warning for using UIWebView when submitting Builds to the App Store Connect.(1180664, 1182274)

方案二

去除libiPhone-lib.a中UIWebView的引用

  1. 首先,生成一个URLUtility.mm文件
include <iostream>
import <UIKit/UIKit.h>
using namespace std;
namespace core {
    template <class type>
    class StringStorageDefault {};
    template <class type,class type2>
    class basic_string {
    public:
        char * str;
        basic_string( char* arg){
            str = arg;
        }
    };
}
void OpenURLInGame(core::basic_string< char,core::StringStorageDefault<char> > const&arg){}

void OpenURL(core::basic_string<char,core::StringStorageDefault<char> >const &arg){
    const void *arg2 = arg.str;
    UIApplication *app = [UIApplication sharedApplication];
    NSString *urlStr = [NSString stringWithUTF8String:(char *)arg2];
    NSURL *url = [NSURL URLWithString:urlStr];
    if (@available(iOS 10.0, *)) {
        [app openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:nil];
    } else {
        [app openURL:url];
    }
}


void OpenURL(std::string const&arg){
    UIApplication *app = [UIApplication sharedApplication];
    NSString *urlStr = [NSString stringWithUTF8String:arg.c_str()];
    NSURL *url = [NSURL URLWithString:urlStr];
    if (@available(iOS 10.0, *)) {
        [app openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:nil];
    } else {
        [app openURL:url];
    }
}
  1. 使用lipo查看libiPhone-lib.a文件中包含的架构
lipo -info libiPhone-lib.a 
Architectures in the fat file: libiPhone-lib.a are: armv7 arm64 armv7s
  1. 使用URLUtility.mm生成对应架构下URLUtility.o文件
clang -c URLUtility.mm -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
  1. 拆分libiPhone-lib.a中各架构文件
lipo libiPhone-lib.a -thin arm64 -output ./URLUtility64/libiPhone-lib64.a
  1. 移除对应架构中的原有URLUtility.o文件
ar -d ./URLUtility64/libiPhone-lib64.a URLUtility.o
  1. 将新生成的URLUtility.o文件添加到对应架构的.a中
ar -q ./URLUtility64/libiPhone-lib64.a ./URLUtility64/URLUtility.o
  1. 将各架构的.a文件合并生成新的libiPhone-lib.a
lipo -create ./URLUtility64/libiPhone-lib64.a ./URLUtilityv7/libiPhone-libv7.a ./URLUtilityv7s/libiPhone-libv7s.a -output ./new/libiPhone-lib.a

以上,即可替换libiPhone-lib.a中的URLUtility.o文件,从而达到移除UIWebView相关引用的目的。

方案三

直接使用python脚本进行处理

#!/usr/bin/evn python
# coding=UTF-8

import os
import shutil

def restructure():
    iPhone_OS_SDK_path = "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk"
    arm64_path = "./URLUtility64/"
    armv7_path = "./URLUtilityv7/"
    armv7s_path = "./URLUtilityv7s/"
    new_lib_path = "./new"

    if os.path.exists(arm64_path):
        print("------移除原URLUtility64文件------")
        shutil.rmtree(arm64_path)
    os.makedirs(arm64_path)

    if os.path.exists(armv7_path):
        shutil.rmtree(armv7_path)
        print("------移除原URLUtilityv7文件------")
    os.makedirs(armv7_path)
    
    if os.path.exists(armv7s_path):
        shutil.rmtree(armv7s_path)
        print("------移除原URLUtilityv7s文件------")
    os.makedirs(armv7s_path)

    if os.path.exists(new_lib_path):
        shutil.rmtree(new_lib_path)
        print("------移除原new文件------")
    os.makedirs(new_lib_path)

    cmd_generate_o_arm64 = "clang -c URLUtility.mm -arch arm64 -isysroot " + iPhone_OS_SDK_path
    cmd_generate_o_armv7 = "clang -c URLUtility.mm -arch armv7 -isysroot " + iPhone_OS_SDK_path
    cmd_generate_o_armv7s = "clang -c URLUtility.mm -arch armv7s -isysroot " + iPhone_OS_SDK_path

    cmd_thin_arm64 = "lipo libiPhone-lib.a -thin arm64 -output ./URLUtility64/libiPhone-lib64.a"
    cmd_thin_armv7 = "lipo libiPhone-lib.a -thin armv7 -output ./URLUtilityv7/libiPhone-libv7.a"
    cmd_thin_armv7s = "lipo libiPhone-lib.a -thin armv7s -output ./URLUtilityv7s/libiPhone-libv7s.a"

    cmd_mv_arm64 = "mv URLUtility.o ./URLUtility64/URLUtility.o"
    cmd_mv_armv7 = "mv URLUtility.o ./URLUtilityv7/URLUtility.o"
    cmd_mv_armv7s = "mv URLUtility.o ./URLUtilityv7s/URLUtility.o"

    cmd_delete_arm64 = "ar -d ./URLUtility64/libiPhone-lib64.a URLUtility.o"
    cmd_delete_armv7 = "ar -d ./URLUtilityv7/libiPhone-libv7.a URLUtility.o"
    cmd_delete_armv7s = "ar -d ./URLUtilityv7s/libiPhone-libv7s.a URLUtility.o"

    cmd_add_arm64 = "ar -q ./URLUtility64/libiPhone-lib64.a ./URLUtility64/URLUtility.o"
    cmd_add_armv7 = "ar -q ./URLUtilityv7/libiPhone-libv7.a ./URLUtilityv7/URLUtility.o"
    cmd_add_armv7s = "ar -q ./URLUtilityv7s/libiPhone-libv7s.a ./URLUtilityv7s/URLUtility.o"
    
    cmd_create_lib = "lipo -create ./URLUtility64/libiPhone-lib64.a ./URLUtilityv7/libiPhone-libv7.a ./URLUtilityv7s/libiPhone-libv7s.a -output " + new_lib_path + "/libiPhone-lib.a"


    print("------处理arm64架构------")
    os.system(cmd_generate_o_arm64)
    os.system(cmd_thin_arm64)
    os.system(cmd_mv_arm64)
    os.system(cmd_delete_arm64)
    os.system(cmd_add_arm64)

    print("------处理armv7架构------")
    os.system(cmd_generate_o_armv7)
    os.system(cmd_thin_armv7)
    os.system(cmd_mv_armv7)
    os.system(cmd_delete_armv7)
    os.system(cmd_add_armv7)

    print("------处理armv7s架构------")
    os.system(cmd_generate_o_armv7s)
    os.system(cmd_thin_armv7s)
    os.system(cmd_mv_armv7s)
    os.system(cmd_delete_armv7s)
    os.system(cmd_add_armv7s)

    os.system(cmd_create_lib)
    print("生成新的libiPhone-lib.a,已移除UIWebView引用")


if __name__ == "__main__":
    restructure()

初始文件目录为:

.
├── URLUtility.mm
└── libiPhone-lib.a

脚本运行完成后文件目录为:

.
├── URLUtility.mm
├── URLUtility64
│   ├── URLUtility.o
│   └── libiPhone-lib64.a
├── URLUtilityv7
│   ├── URLUtility.o
│   └── libiPhone-libv7.a
├── URLUtilityv7s
│   ├── URLUtility.o
│   └── libiPhone-libv7s.a
├── libiPhone-lib.a
├── new
│   └── libiPhone-lib.a
└── restructure.

其中new文件目录下的libiPhone-lib.a已实现UIWebView引用的移除,使用find命令进行验证,发现新的libiPhone-lib.a中已不包含UIWebView相关任何代码。将该文件与工程中的对应文件进行替换,即可解决ipa包上传问题。

以上转载自:https://www.jianshu.com/p/327c7fbefc4a
下面是我在项目中使用上面的方案二实现的方式

	@if [ -f "publish/ios/Libraries/Plugins/iOS/URLUtility.mm" ]; then \
		(cd publish/ios/Libraries && \
			lipo libiPhone-lib.a -thin arm64 -output libiPhone-lib64.a && \
			clang -c Plugins/iOS/URLUtility.mm -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk && \
			ar -d libiPhone-lib64.a URLUtility.o && \
			ar -q libiPhone-lib64.a URLUtility.o && \
			lipo libiPhone-lib.a -thin armv7 -output libiPhone-libv7.a && \
			clang -c Plugins/iOS/URLUtility.mm -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk && \
			ar -d libiPhone-libv7.a URLUtility.o && \
			ar -q libiPhone-libv7.a URLUtility.o && \
			lipo libiPhone-lib.a -thin armv7s -output libiPhone-libv7s.a && \
			lipo -create libiPhone-lib64.a libiPhone-libv7.a libiPhone-libv7s.a -output libiPhone-lib.a && \
			rm -f libiPhone-lib64.a libiPhone-libv7.a libiPhone-libv7s.a URLUtility.o ); \
	fi

你可能感兴趣的:(Unity游戏开发)