swan.openSetting

解释: 调起客户端智能小程序设置界面,返回用户设置的操作结果。swan.openSetting 可打开的控制面板权限详见需授权接口列表swan.openSetting - 图1Web 态说明 Web 态暂不支持用户主动切换授权开关,接口调用会进入失败回调(fail)。

方法参数

Object object

object参数说明

属性名类型必填默认值说明
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明

参数类型说明
authSettingObject用户授权结果,其中 key 为 scope 值,value 为 Bool 值,表示用户是否允许授权。

错误码

Android

错误码(errCode)说明
201解析失败,请检查调起协议是否合法
1001执行失败

iOS

错误码(errCode)说明
202解析失败,请检查参数是否正确

Web 态

错误码(errCode)说明
1001不支持
错误信息(errMsg)类型说明
API swan.openSetting is not supported in webstring不支持

示例

扫码体验

swan.openSetting - 图2请使用百度APP扫码

图片示例

swan.openSetting - 图3

swan.openSetting - 图4

swan.openSetting - 图5

代码示例1 - API打开授权设置页 :

在开发者工具中预览效果

  • 在 swan 文件中
  1. <view class="container">
  2. <view class="page-body">
  3. <button bind:tap="openSetting" type="primary" hover-stop-propagation="true">打开设置</button>
  4. </view>
  5. <view class="page-title">
  6. <view class="page-title-line"></view>
  7. <view class="page-title-text">{{title}}</view>
  8. </view>
  9. </view>
  • 在 js 文件中
  1. Page({
  2. data: {
  3. title: 'openSetting'
  4. },
  5. openSetting() {
  6. swan.openSetting({
  7. success: res => {
  8. // 正常的业务逻辑
  9. },
  10. fail: res => {
  11. // 异常处理逻辑
  12. }
  13. });
  14. }
  15. });

代码示例2 - button组件打开授权设置页 :

在开发者工具中预览效果

  • 在 swan 文件中
  1. <view class="container">
  2. <view class="page-body">
  3. <button bindopensetting="openSetting" type="primary" open-type="openSetting">打开设置</button>
  4. </view>
  5. </view>
  • 在 js 文件中
  1. Page({
  2. openSetting(){
  3. swan.showToast({
  4. title: '打开回调',
  5. icon: 'none'
  6. });
  7. }
  8. });