ios配置微信SDK:主要讲解一些核心步骤,具体流程去看官网就行了
1.配置Xcode项目
1.1配置Associated Domains,不要前面的https://,后面到com就行不用斜杠/
1.2配置文件apple-app-site-association
1.2.1details中配置数组是为了配置多个app,从而多个app可以使用一个文件即可;
1.2.2其中paths为可以打开app的路径,可以配置多个,用逗号分隔,比如如下的配置只有链接为:https://Associated Domains/myapp/...的时候才能打开app;如果配置*表示任何路径都能打开
1.2.3 该文件放到服务器(Associated Domains)的根目录下或者根目录的.well-known目录下,配置成功在浏览器输入https://Associated Domains/apple-app-site-association可以下载该文件
(可参考微信配置:https://help.wechat.com/apple-app-site-association)
{
"applinks": {
"apps": [],
"details": [{
"appID": "team id.bundle id",
"paths": ["/myapp/*"]
}]
}
}
1.3微信开发平台应用配置
在第二步中确定了Associated Domains和打开路径之后,就可以取微信开发平台创建应用
配置应用的UniversalLink为:Associated Domains+打开路径,比如按照上面的配置为:
https://Associated Domains/myapp/;其他配置按照步骤即可
1.4配置在xcode项目中导入微信sdk
1.3.1根据官方教程导入微信sdk即可
1.3.2注册微信sdk
[WXApi registerApp:WeChatAPPKEY universalLink:WeChatUNIVERSALLINK];
1.3.3配置微信登录和分享的回调
//需要在appDelegate.m文件中实现如下:
// Universal Links打开app
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler API_AVAILABLE(ios(8.0))
{
// qqsdk配置回调
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
if ([TencentOAuth CanHandleUniversalLink:userActivity.webpageURL]) {
// qq分享
BOOL flag = [QQApiInterface handleOpenUniversallink:url delegate:self];
if (!flag) {
// qq登录
return [TencentOAuth HandleUniversalLink:url];
}
return flag;
}
}
// 微信sdk回调 如果还有其他的需要加入判断
/**
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
if ([userActivity.webpageURL.description containsString:WeChatAPPKEY]) {
return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
}
*/
return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
// ios9以后调用
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options NS_AVAILABLE_IOS(9_0){
if ([TencentOAuth CanHandleOpenURL:url]) {
// qq登录
return [TencentOAuth HandleOpenURL:url] ;
}
else if ([url.absoluteString rangeOfString:@"response_from_qq"].location != NSNotFound) {
// qq分享
return [QQApiInterface handleOpenURL:url delegate:[DJQQManager manager]];
}
// 微信登录
// else if ([url.host isEqualToString:@"oauth"]) {
return [WXApi handleOpenURL:url delegate:self];
// }
return true;
}
1.5发起微信授权登录,实现代理WXApiDelegate
1.5.1发起授权
//构造SendAuthReq结构体
SendAuthReq* req =[[SendAuthReq alloc]init];
req.scope = @"snsapi_userinfo";
req.state = @"自定义即可";
//第三方向微信终端发送一个SendAuthReq消息结构
if ([WXApi isWXAppInstalled])
{
[WXApi sendReq:req completion:^(BOOL success) {
}];
}
else
{
[WXApi sendAuthReq:req viewController:vc delegate:self completion:^(BOOL success) {
}];
}
1.5.2处理回调
-(void)onResp:(BaseResp *)resp
{
if([resp isKindOfClass:[SendMessageToWXResp class]]){
NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
NSString *strMsgcot ;//内容
NSLog(@"%@",strMsg);
if (resp.errCode == 0) {
strMsgcot = @"分享成功";
}else{
strMsgcot = @"分享失败";
}
//提示框
[self showToast:strMsgcot];
}
// 登录
else if ([resp isKindOfClass:[SendAuthResp class]]){
SendAuthResp * res = (SendAuthResp*)resp;
NSString * msg = @"登录失败";
switch (res.errCode) {
case 0://用户同意
{
msg = nil;
// 授权登录
}
break;
case -4://用户拒绝授权
msg = @"用户拒绝";
break;
case -2://用户取消
msg = @"用户取消";
break;
default:
break;
}
if (msg) {
// 失败处理
}
}
// 支付
/*
else if ([resp isKindOfClass:[PayResp class]]){
NSString * msg = @"支付失败";
BOOL flag = false;
PayResp*response=(PayResp*)resp;
switch(response.errCode){
case WXSuccess:
//服务器端查询支付通知或查询API返回的结果再提示成功
msg = @"支付成功";
flag = true;
break;
case WXErrCodeCommon:{
msg = @"支付错误,订单状态异常";
}
break;
case WXErrCodeUserCancel:{
msg = @"支付失败,订单已被取消";
}
break;
default:
break;
}
*/
}