
今天微信小程序新增了支持跳转 App 的功能,算是一次比较大的突破,我也第一时间体验了一下该功能,App 和小程序之间的跳转还是比较灵活的。


因为需要用户主动触发才能打开 APP,所以该功能不由 API 来调用,需要用open-type的值设置为launchApp的<button>组件的点击来触发。
当小程序从 APP 分享消息卡片的场景打开时(场景值 1036,APP 分享小程序文档 iOS参见,Android参见),小程序会获得打开 APP 的能力,此时用户点击按钮可以打开分享该卡片的 APP。即小程序不能打开任意 APP,只能跳回分享该小程序卡片的 APP。
在一个小程序的生命周期内,只有在特定条件下,才具有打开 APP 的能力。打开 APP 的能力可以理解为由小程序框架在内部管理的一个状态,为 true 则可以打开 APP,为 false 则不可以打开 APP。
在小程序的生命周期内,这个状态的初始值为 false,之后会随着小程序的每次打开(无论是启动还是切到前台)而改变:
下载最新的微信 SDK,或使用 CocoaPods 更新。
分享微信小程序:

- (void)downloadCoverImage{ SDWebImageManager *manager = [SDWebImageManager sharedManager]; __weak typeof(self) weakSelf = self; [manager loadImageWithURL:self.item.itemCover.url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSizem, NSURL *targetUrl) { //NSLog(@"receiveSize:%ld,expectedSize:%ld",(long)receivedSize,(long)expectedSize); } completed:^(UIImage *image,NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (image) { NSData *imageData = UIImageJPEGRepresentation(image, 0.1); weakSelf.hdImageData = UIImageJPEGRepresentation(image, 0.1); weakSelf.thumbImage = [[[UIImage alloc]initWithData:data] scaledToSize:CGSizeMake(500, 400)]; weakSelf.shareImage = [[[UIImage alloc]initWithData:imageData] scaledToSize:CGSizeMake(100, 100)]; } }]; self.sharestr = [NSString stringWithFormat:@"只需%@元,广州女装源头货,买手精选,一件起批,赶紧来上新进货吧!",self.item.salePrice]; } 分享效果: 
需要将 <button> 组件 open-type 的值设置为 launchApp。如果需要在打开 APP 时向 APP 传递参数,可以设置 app-parameter 为要传递的参数。通过 binderror 可以监听打开 APP 的错误事件。
<button open-type="launchApp" app-parameter="itemId=12345&userId=1234" binderror="launchAppError">打开 APP</button> 若分享成功后,微信唤起 App,并传递 app-parameter 参数到 App: 
在 App 中添加 WXApiDelegate 中的 onReq 方法,处理参数。
如果唤起 App 失败,在 binderror 对应的方法中处理唤起失败后的逻辑:
Page({ launchAppError: function(e) { console.log(e.detail.errMsg) } }) 如果是没有安装 App,可以使用 wx.previewImage 的方法弹出引导关注公众号的图片文案。 
都知道从公众号文章或者底部菜单是可以调转到小程序的,从小程序是不可以调转回公众号的,从上面的动画中,你有没有发现亮点?这里有个小技巧,在微信小程序中长按 previewImage 打开的图片,是可以识别二维码的,接着直接跳转到公众号!
同学们不要开心的太早,这个功能不是每次都能实现的,有兴趣的可以自己试一下。

1 xmlyqing00 2018-07-04 22:48:16 +08:00 微信群还可以加吗?我最近做好了一个小程序,交流交流。 |