遇到项目需要接入微信和支付宝支付,安卓平台弄完了,结果在ios平台卡了很久,这里记录下主要遇到的问题。
unity版本:2019.4.27
xcode版本:12.4
对于xcode中的配置,我使用代码的方式自动配置了,以下代码就是设置xcode的。这个代码借鉴了这里的。
using UnityEditor;
using System.IO;
using UnityEngine;
#if UNITY_IOS && UNITY_EDITOR
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
#endif
public static class XCodePostProcessBuild
{
#if UNITY_IOS && UNITY_EDITOR
private static readonly string[] csAddFrameworks = new string[]{
"Security.framework","WebKit.framework", "CoreGraphics.framework", "CoreTelephony.framework", "libz.tbd" ,"libsqlite3.0.tbd", "libc++.tbd"
, "SystemConfiguration.framework", "QuartzCore.framework", "CoreText.framework", "UIKit.framework", "Foundation.framework", "CFNetwork.framework"
, "CoreMotion.framework", "AlipaySDK.framework"
};
[PostProcessBuild(1)]
public static void OnPostprocessBuild(BuildTarget buildTarget, string pathToBuiltProject)
{
if (BuildTarget.iOS != buildTarget)
{
return;
}
string projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
SetFrameworksAndBuildSettings(projectPath);
SetInfoList(pathToBuiltProject, "填自己微信工程的bundle id", "自己微信工程的appid");
SetAssociatedDomains(projectPath, "pay.91mayou.com/myddz/");
}
private static void SetFrameworksAndBuildSettings(string path)
{
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(path));
string target = proj.GetUnityMainTargetGuid();
Debug.Log("Target Name is " + target);
// 设置 BuildSettings
proj.AddBuildProperty(target, "Other Linker Flags", "-Objc -all_load");
proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
//根据微信SDK文档的要求,加入相关的Frameworks
for (int i = 0; i < csAddFrameworks.Length; ++i)
{
if (!proj.ContainsFramework(target, csAddFrameworks[i]))
proj.AddFrameworkToProject(target, csAddFrameworks[i], false);
}
File.WriteAllText(path, proj.WriteToString());
}
public static void SetInfoList(string buildPath, string wxUrlName, string wxScheme)
{
string listPath = buildPath + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(listPath));
// 在“info”标签栏的“URL type“添加“URL scheme”,值为你在微信后台注册的应用程序的 AppID
PlistElementArray urlArray = plist.root.CreateArray("CFBundleURLTypes");
PlistElementDict dict = urlArray.AddDict();
dict.SetString("CFBundleTypeRole", "Editor");
dict.SetString("CFBundleURLName", wxUrlName);
PlistElementArray urlSchemes = dict.CreateArray("CFBundleURLSchemes");
urlSchemes.AddString(wxScheme);
// 在“info”标签栏的“URL type“添加“URL scheme”,原本只有微信,支付宝这里新增的
PlistElementArray urlArray2 = plist.root.values["CFBundleURLTypes"].AsArray();
PlistElementDict dict2 = urlArray2.AddDict();
dict2.SetString("CFBundleTypeRole", "Editor");
dict2.SetString("CFBundleURLName", "支付宝平台的appid");
PlistElementArray urlSchemes2 = dict2.CreateArray("CFBundleURLSchemes");
urlSchemes2.AddString("填自己的项目标识哪个字符串 官方例子是:alisdkdemo");
// 在 “info”标签栏的“LSApplicationQueriesSchemes“添加weixin wechat和weixinULAPI
PlistElementArray wxArray = plist.root.CreateArray("LSApplicationQueriesSchemes");
wxArray.AddString("weixin");
wxArray.AddString("wechat");
wxArray.AddString("weixinULAPI");
wxArray.AddString("alipay");
File.WriteAllText(listPath, plist.WriteToString());
}
// 设置Associated Domains
public static void SetAssociatedDomains(string pbxProjectPath, string domainUrl)
{
//默认 Target Name, 你自己的可能不一样
string targetName = "Unity-iPhone";
//Set the entitlements file name to what you want but make sure it has this extension
string entitlementsFileName = "my_app.entitlements";
var entitlements = new ProjectCapabilityManager(pbxProjectPath, entitlementsFileName, targetName);
entitlements.AddAssociatedDomains(new string[] { "applinks:" + domainUrl });
entitlements.WriteToFile();
}
#endif
}
上面脚本设置xcode是没有设置开发团队的,需要自己手动选择.
关于支付宝的接入,主要借鉴了这里。但是我没有添加那么多文件到plugins文件夹下,支付宝我只添加了这两个。
AlipaySDK.bundle
AlipaySDK.framework
至于那个pay_oc.m脚本如下:
#import
//#import "APOrderInfo.h"
//#import "APRSASigner.h"
#import
@implementation APViewController
@end
#if defined (__cplusplus)
extern "C"
{
#endif
#pragma mark ==============点击支付行为==============
void iospay(const char *orderString )
{
NSLog(@"调用到支付宝的支付了");
//应用注册scheme,在Info.plist定义URL types
NSString *appScheme = @"ali_myddz";
NSString *orderStr = [NSString stringWithUTF8String:orderString];
if (orderString != nil) {
[[AlipaySDK defaultService] payOrder:orderStr fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
if([resultDic[@"resultStatus"] isEqual: @"9000"])
{
UnitySendMessage("WeChatComponent", "AliPayCallback", "true");
}
else
{
UnitySendMessage("WeChatComponent", "AliPayCallback", "false");
}
}];
}
}
#if defined (__cplusplus)
}
#endif
支付宝按照网上的文档接入,一直报错提示
按网上的说法就是库没填加进去,"libz.tbd" ,"libsqlite3.0.tbd", "libc++.tbd",其实三个在下图这两个地方都要添加的,我开始就在1那个位置添加了,但始终报这个错,查了两天原因,最后发现在2位置没有这3个,也要添加这3个库。
而自动设置xcode配置的脚本并没有设置成功AlipaySDK.framework这个库,这个库需要手动重新设置下。
到这里就可以xcode编译通过了,但是安装到手机测试的时候,xcode提示 unable to install "xxx"不能装。原因是下面图中的需要修改成Do Not Embed(不嵌入),在上一步手动添加AlipaySDK.framework后,xcode默认是Embed & Sign(嵌入并签名)。这样改了之后就可以真机安装测试了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)