Unity iOS integrated WeChat login (cocoapod integration method)

In a nutshell: Integrate WeChat login for unity's iOS mobile application (using cocoapod integration)


foreword

简单说说垃圾话

The company requires the development of an application, which currently includes password login and verification code login, and suddenly said that it wants to add a WeChat login, which is directly confusing; quickly read the relevant information and familiarized yourself with the WeChat login SDK integration.
In fact, before the integration, the author also thought about whether there is already a way to integrate the WeChat SDK into a unitypackage. But after a while of Google and Baidu, there is no more reliable package except for something called ShareSDK. The most difficult thing is that the information on ShareSDK is very incomplete, and the customer service hardly understands development. . Of course, it is not ruled out that the author cannot find a channel to learn more.
In short, after searching for available information to no avail, I had no choice but to start the integration myself. Fortunately, the documentation for integration is relatively complete.


开始正文

Terms and Conditions

Using the cocoapod integration method can ensure that each dependent package in the project is up to date (there should be other benefits, but unfortunately I can’t understand it, so I won’t write it now), but every time you package, you need to re-execute pod install. If the project only For a WeChat SDK, it may be more convenient to consider manual integration. For manual integration, you can refer to the article of Mr. Lin.

environment

hardware environment

Development machine: Windows computer
Publisher: Mac mini

Software Environment

  • Visual Studio 2019
  • Unity 2021.3.10f1c2
  • Xcode 14.2
  • Windows10
  • macOS Ventura 13.2.1

Steps for usage

1. Confirm that the Universal Links of WeChat are normal

According to the WeChat document, just drop it on your phone and open it

2. Confirm that the Universal Links configuration of the App is successful

  1. There is a cloud server, create a new IIS site, store a json file named "apple-app-site-association" in the root directory of this site, and then modify the content of the file according to the following template and your own project
 {
    
    
    "applinks": {
    
    
        "apps": [],
        "details": [
        	{
    
    
            	"appID": "teamID.bundleID",
            	"paths": ["/albums/*"]
            }
         
        ]
    }
}

Among them, teamID can be seen by viewing the account on your own Apple developer platform; bundleID is the unique ID of the application you set in unity, and the format is

com.公司名.应用名称
  1. After the json file is ready, create a new script in the Editor folder of unity, and then include the following method
    Among them, the generation rule of universalLinks written in the code is: For example, your file address is https://www.baidu.com/ apple-app-site-association Then, in the following address, you need to fill in "applinks:www.baidu.com" (must start with applinks:)
//按照自己工程,修改links
  static string[] universalLinks = {
    
     "applinks:www.baidu.com" };

    /// <summary>
    /// 在编辑器添加iOS能力,这个代码在编辑器打包iOS项目后会自动执行
    /// https://gist.github.com/kyeonw00/02f9d14fbfc51bc0457217a05460c310
    /// </summary>
    /// <param name="buildTarget"></param>
    /// <param name="pathToBuiltProject"></param>
    [PostProcessBuild]
    public static void XcodeAddCapability(BuildTarget buildTarget, string pathToBuiltProject)
    {
    
    
        if (buildTarget != BuildTarget.iOS)
            return;

        string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
        ProjectCapabilityManager projCapability = new ProjectCapabilityManager(projPath, "Unity-iPhone/(entitlement file)", "Unity-iPhone");

        projCapability.AddAssociatedDomains(universalLinks);

        projCapability.WriteToFile();
    }

  1. Unity packages iOS, throws all the files generated after packaging on the mac, opens it with Xcode, plugs in the Apple phone, and clicks Run
  2. Mobile safari opens the link
//根据你上面的站点域名、“apple-app-site-association”的json文件中的path来生成
//域名+path
https://www.baidu.com/albums/123 

You can see the picture below, indicating that the configuration is successful!
safari interface

3. Register your app id and Universal Links with WeChat

Log in to the open platform, then select the mobile application - basic information - modify - next page - check the iOS application, and you can add Bundleid and universal links at this time;
if there is no application, you need to create a new application and wait for review
Note: here The universal links refer to domain names!

Bundleid---------com.公司名.应用名称
universal links---------https://www.baidu.com/albums/

4. Integrate WeChat SDK

Because the author uses CocoaPods automatic integration here, the following will focus on describing this. For manual integration, you can see the article of Mr. Lin in the reference material

  1. Install cocoapods on the mac
    terminal - "enter sudo gem install cocoapods - "verify the installation, enter pod --version, and the version number will appear
    Check the version number of cocapod

  2. Import External Dependency Manager for Unity in unity (visible in Resources)

  3. According to the official tutorial, create a new xml and put it in the Editor folder, the name is "xxxxxxxxxDependencies.xml", xx means any

  4. xml content

<dependencies>
 <iosPods>
    <iosPod name="WechatOpenSDK-XCFramework"  bitcodeEnabled="true" >  </iosPod>
  </iosPods>
</dependencies>
  1. Pack it up and drop it on the MAC
  2. Open the terminal, enter cd <the path of the package you just pulled up> (quick copy path: click on the package, press command+c on the keyboard, that is, win+c); then enter pod install; then use the suffix as . xcworkspace file open project
  3. See the picture below to indicate a successful download
    See WeChat SDK
  4. Add "URL scheme"
    in the "URL type" of the "info" tab, add "URL scheme" as the application id you registered - similar to wx22c22ec2222b2222
    Here, the author also uses scripts to automatically add:
 static void GetBundleID()
    {
    
    
        bundleID = PlayerSettings.applicationIdentifier;
    }
   /// <summary>
    /// 设置Xcode工程的Info.plist文件
    /// </summary>
    /// <param name="buildTarget"></param>
    /// <param name="path"></param>
    [PostProcessBuild(100)]
    public static void XcodeSetInfo(BuildTarget buildTarget, string path)
    {
    
    
        if (buildTarget == BuildTarget.iOS)
        {
    
    
            GetBundleID();

            //自动添加URL Types的配置
            SetPInfo(path, (plist) => {
    
    

                var array = plist.root.CreateArray("CFBundleURLTypes");
                var urlTypeDict = array.AddDict();
                urlTypeDict.SetString("CFBundleURLName", bundleID);

                var urlSchemes = urlTypeDict.CreateArray("CFBundleURLSchemes");
                //在“info”标签栏的“URL type“添加“URL scheme”为你所注册的应用程序 id
                urlSchemes.AddString(appID);

            });
 

        }
    }
 /// <summary>
    /// 写入Xcode中的pinfo
    /// </summary>
    /// <param name="projPath"></param>
    static void SetPInfo(string projPath,Action<PlistDocument> action)
    {
    
    
        // Get the Info.plist file path.
        string plistPath = Path.Combine(projPath, "Info.plist");

        // Open and parse the Info.plist file.
        PlistDocument plist = new PlistDocument();
        plist.ReadFromFile(plistPath);
         

        action?.Invoke(plist);

        // Write the modified Info.plist file.
        plist.WriteToFile(plistPath);
    }

  1. Add weixin, weixinULAPI, weixinURLParamsAPI in "LSApplicationQueriesSchemes" in the "info" tab.
    Add these codes in the method XcodeSetInfo:
 //在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在 “info”标签栏的“LSApplicationQueriesSchemes“添加weixin、weixinULAPI、weixinURLParamsAPI
            SetPInfo(path,(plist)=> {
    
    

                // Add the schemes to the LSApplicationQueriesSchemes key.
                PlistElementArray schemes = plist.root.CreateArray("LSApplicationQueriesSchemes");
                schemes.AddString("weixin");
                schemes.AddString("weixinULAPI");
                schemes.AddString("weixinURLParamsAPI");

            });

After packaging, you can search for it here, although the name is not the same. . . It may be related to the author's Xcode version
You can see it after packing

5. WeChat login code, unity callback

For this part, you can refer to Mr. Lin’s article, so I won’t go into details here. However, it should be noted that objective-c needs to run on xcode, otherwise an error will be reported, so Windows development needs to add conditional compilation to the imported oc plug-in: non-editor.

Replenish

Follow up to add how to publish the finished apple package to testflight, first bury a hole haha.

Summary and Reflection

The above is the general process, but unfortunately there is a problem with the cocoapod integration method: every time you package, you need to re-execute pod install; and I don’t know why, I need to check the bitcode=yes of pods-unityframework every time; this part is currently The manual way is really troublesome, and I am still looking for an automated way to add it. Friends are welcome to comment!
check bitcode

References

Guess you like

Origin blog.csdn.net/xiaoLongww/article/details/130157394