uni-app iOS系统抖音小程序用户信息授权返回后switchTab和navigateTo失效问题

uni-app iOS系统抖音小程序用户信息授权返回后switchTab和navigateTo失效问题

开发环境 版本号 项目创建方式
Windows win10 HBuilderX

产品分类:

uniapp/小程序/抖音

PC开发环境操作系统:

Windows

HBuilderX类型:

正式

HBuilderX版本号:

3.2.9

第三方开发者工具版本号:

2.25.0.5

基础库版本号:

2.25.0.5

项目创建方式:

HBuilderX

示例代码:

<template>
<view class="container">
<view class='header'>
<open-data type="userAvatarUrl" v-if="platform==1"></open-data>
<image src="../../static/logo.png" v-if="platform==6"></image>
</view>
<view class='content'>
<view>为了提供更好的服务,请点击下方授权登录</view>
<text>获得你的公开信息(昵称,头像等)</text>
</view>
<button class='btns bottoms' lang="zh_CN"  @click="redindex" v-if="is_login==0" >授权登录</button>
<button class="btns bottoms" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" v-if="is_login==1" >绑定手机</button>
<button class='btns bottoms no-login' type="default" @click="goBack">返回</button>
</view>
</template>  
<script>
export default {
data() {
return {
is_login:0 ,//0:先登录后绑定手机,1:直接绑定手机,2:只需要登录
login_type:0,
logo:'../../static/logo.jpg',
platform:6,
}
},
onShow() {
this.platform = this.gldb.platform;
this.login_type = uni.getStorageSync('showBtnType');
console.log('.login_type: '+this.login_type);
if(this.login_type==1){
this.is_login = 1;
}
},
methods: {
goBack(){
uni.switchTab({
url:'/pages/index/index'
})
},
redindex(res){
var that = this;
if(that.gldb.platform==1){
uni.getUserProfile({
desc: '登录账号', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
that.gldb.userInfo = res.userInfo;
let tmp = uni.getStorageSync('device_id');
uni.clearStorageSync();
uni.setStorageSync('device_id', tmp);
uni.setStorageSync('refreshLogin', 1);
uni.setStorageSync('auto_login',1);
if(that.login_type==2){
that.is_login=2;
uni.removeStorageSync('showBtnType');
uni.switchTab({
url:'/pages/index/index'
})
}else{
that.is_login=1;
}
}
})
}else{
uni.getSetting({
success: (res) => {
that.login( that.storageData );
if (res.authSetting['scope.userInfo'] == true) {
//同意授权
var isIn = true;
}else if(res.authSetting['scope.userInfo'] == false){
//拒绝
console.log('拒绝');
var isIn = false;
}else{
//还有没有授权的操作
var isIn = true;
console.log('还有没有授权的操作');
}
if( isIn == true ){
uni.authorize({
scope: 'scope.userInfo',
success( res ) {
that.login( that.storageData );
},
fail(res){
that.showToast({
title: '请开启用户信息!',
duration: 3000
});
uni.openSetting({
success(res) {
if (res.authSetting['scope.userInfo'] == true) {
that.login( that.storageData );
}
},
fail(res){
console.log('fail-openSetting');
}
});
}

})
}else{
uni.openSetting({
success(res) {
if (res.authSetting['scope.userInfo'] == true) {
that.login( that.storageData );
}
}
});
}
}
})
}
},
storageData(){
var that = this;
let tmp = uni.getStorageSync('device_id');
uni.clearStorageSync();
uni.setStorageSync('device_id', tmp);
uni.setStorageSync('refreshLogin', 1);
uni.setStorageSync('auto_login','1');
if(that.login_type==2){
that.is_login=2;
uni.removeStorageSync('showBtnType');
uni.navigateBack({
delta: 1
});
}else{
that.is_login=1;
}
},
onGetPhoneNumber(e){
console.log(e);
var that = this;
if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
//用户决绝授权
that.showToast({
title: '绑定手机后才可以获得下单等更多权限哟!',
duration: 3000
});
}else{
//允许授权
uni.setStorageSync('refreshLogin', 1);
uni.setStorageSync('ivData', {encryptedData:e.detail.encryptedData,iv:e.detail.iv,type:1});
uni.removeStorageSync('showBtnType');
console.log( uni.getStorageSync('ivData') );
uni.navigateBack({
delta: 1
});
}
}
}
}
</script>
.header {
border-bottom: 1px solid #ccc;
text-align: center;
width: 200upx;
height: 200upx;
margin: 100upx auto;
border-radius: 50%;
overflow: hidden;
}

.header image {
width: 200upx;
height: 200upx;
}

.content {
margin-left: 50upx;
margin-bottom: 90upx;
font-size: 24upx;
}

.content text {
display: block;
color: #9d9d9d;
margin-top: 40upx;
}

.bottoms {
text-align: center;
border-radius: 80upx;
width: 90%;
height: 80rpx;
line-height: 80rpx;
color: #FFFFFF;
margin: auto;
font-size: 35upx;
background: linear-gradient(-90deg, #FF4C11, #FF8225);
}

.no-login{
margin-top: 40upx;
}

open-data{
width: 50upx !important;
height: 50upx;
border-radius: 50%;
}

更多关于uni-app iOS系统抖音小程序用户信息授权返回后switchTab和navigateTo失效问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app iOS系统抖音小程序用户信息授权返回后switchTab和navigateTo失效问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个典型的iOS系统下抖音小程序授权后页面跳转失效问题。主要原因是iOS系统对授权弹窗后的页面跳转有特殊限制。

问题分析:

  1. iOS系统限制:在用户信息授权弹窗(如uni.authorizeuni.getUserProfile)关闭后,iOS系统会短暂锁定页面跳转功能,导致立即执行的switchTabnavigateTo失效。

  2. 异步执行时机:授权成功后直接调用跳转API,此时iOS可能还未完全释放页面控制权。

解决方案:

修改授权成功后的跳转逻辑,使用setTimeout延迟执行:

// 修改授权成功后的代码
success: (res) => {
  that.gldb.userInfo = res.userInfo;
  let tmp = uni.getStorageSync('device_id');
  uni.clearStorageSync();
  uni.setStorageSync('device_id', tmp);
  uni.setStorageSync('refreshLogin', 1);
  uni.setStorageSync('auto_login', 1);
  
  if(that.login_type==2){
    that.is_login=2;
    uni.removeStorageSync('showBtnType');
    // 使用setTimeout延迟跳转
    setTimeout(() => {
      uni.switchTab({
        url:'/pages/index/index'
      })
    }, 300); // 建议300-500ms延迟
  }else{
    that.is_login=1;
  }
}
回到顶部