Solve the problem that the WeChat applet image save to the album API sometimes fails to save and cannot be authorized

Use of wx.saveImageToPhotosAlbum

For details on how to use it, you can check the official document: official document

Note: This article is the test result under the 2.13.2 version of the WeChat debugging base library

According to the document, we have called it correctly, and it is no problem after testing, but it is found that sometimes some mobile phones have problems during the subsequent testing, and the authorization page cannot be popped up, and there is no prompt, that is, the save is unsuccessful. The interaction is very unfriendly, so I started to solve this problem.


Analyze the cause of the problem

After a lot of real machine testing, it was discovered that in the WeChat applet, if the user refused the authorization, the authorization window will not pop up again when the next request is made. Therefore, it is necessary to first determine the authorization status and manually pop up the authorization reminder.

At this time, wx.openSetting is often unable to trigger the authorization pop-up window, and it should be considered to prevent harassment.

Generally prompt:

"saveImageToPhotosAlbum:fail authorize no response"

"saveImageToPhotosAlbum:fail auth deny"

……

Unauthorized prompt

//这种写法是无效的,测试过
wx.saveImageToPhotosAlbum({
   filePath: res.tempFilePath,
   success: function(res) {},
   fail: function(res) {
      wx.openSetting({})//再次获取授权
     }
})

So how to solve it?

Callingwx.saveImageToPhotosAlbum()这个API之前先做个判断,判断是否有这个权限了:

wx.getSetting() this can get this information

res.authSetting.hasOwnProperty('scope.writePhotosAlbum') If there is this field, the status is false, indicating that it has been rejected. In this case, the authorization window will not pop up again automatically

Two treatments can be done at this time:

  1. Call the pop-up window to re-authorize. This is based on the plan of other developers. After testing, I found that some mobile phones cannot obtain authorization, and the same permissions cannot be obtained. You can try the effect.
    wx.getSetting({
      success: (res) => {
      if(res.authSetting.hasOwnProperty(‘scope.writePhotosAlbum’)=== false ){
       wx.showModal({
         title: '提示',
         content: '需要获取相册权限',
         success: function (res) {
         if (res.confirm) {
          //  打开授权页面,让用户手动打开权限
          wx.openSetting({
          success: function (data) {}
         })
         }
      }
     }
    }

     

  2. Since the user refused to be able to pop up the window to prompt the authorization again, at this time, he can only make a prompt, prompting the user to open the authorization of the album in the setting function of the applet. This method is useful for any model!

     How to open this setting can see this: WeChat applet setting management

     The prompts here are not for authorized users to operate, and the warm prompts are enough to achieve humanized interaction.

Guess you like

Origin blog.csdn.net/xiao_bin_shen/article/details/110876392