There is no response or failure to share WeChat applets on iOS; multiple sharing methods on iOS (take WeChat as an example: link + picture + applet)

background

First explain the background: the most recent requirement is to control the sharing form according to the data returned by the interface, that is, the incoming sharing category (such as articles, audio, inviting friends to download applications, etc.), sharing platform (such as WeChat friends, circle of friends, etc.), sharing category The corresponding id (such as article id, audio id, etc.), the interface returns the corresponding sharing form (picture, applet, web page) and the corresponding information required for sharing. In this way, the form of sharing can be flexibly controlled according to various conditions. (Okay~ I admit that the actual use is not very great...)

code first

1. Picture format

		//1.创建多媒体消息结构体
        WXMediaMessage *mediaMsg = [WXMediaMessage message];
        //2.创建多媒体消息中包含的图片数据对象
        WXImageObject *imgObj = [WXImageObject object];
        //图片真实数据
        imgObj.imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"img_url"]]];
        //dic为接口返回的分享所需信息,也可以自己处理链接或者添加本地图片
        //            UIImage *image = IMAGE(@"invite.jpg");
        //
        //        imgObj.imageData = UIImagePNGRepresentation(image);
        //多媒体数据对象
        mediaMsg.mediaObject = imgObj;
        
        //3.创建发送消息至微信终端程序的消息结构体
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        //多媒体消息的内容
        req.message = mediaMsg;
        //指定为发送多媒体消息(不能同时发送文本和多媒体消息,两者只能选其一)
        req.bText = NO;
        //指定发送到会话(聊天界面)
        req.scene = WXSceneSession;  //此处可根据实际情况来加判断  WXSceneTimeline-朋友圈
        //发送请求到微信,等待微信返回onResp
        [WXApi sendReq:req];

2. Small program form

	//前面最好加个判断,如果以小程序分享到朋友圈,暂时是不支持的
	
        Protocal *data = [[Protocal alloc] init];
        data.title = dic[@"title"];
        data.desc = dic[@"content"];
        data.url = dic[@"url"];
        
        data.img = dic[@"img_url"];
        
        data.programPage = dic[@"app_path"];//小程序路径
        data.userName = dic[@"app_userName"];//小程序原始id
        //dic为接口返回分享所需信息,Protocal为自己创建的分享模型,
        --后面用的友盟的分享,也可用微信原生的,毕竟图省事
            //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
      UMShareMiniProgramObject *shareObject = [UMShareMiniProgramObject shareObjectWithTitle:data_.title descr:data_.desc thumImage:data_.image];
        shareObject.webpageUrl =data_.url?:@"";//若url为空此处会无法调起微信,所以此处最好不要写空,见后面遇到的坑
        shareObject.userName = data_.userName;
        shareObject.path = data_.programPage;
        messageObject.shareObject = shareObject;
        shareObject.hdImageData = UIImageJPEGRepresentation(data_.image,1);

        shareObject.miniProgramType = UShareWXMiniProgramTypeRelease; // 可选体验版和开发板
            //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self.mObject completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];

3. Web page form (the most original form of sharing)

	 Protocal *data = [[Protocal alloc] init];
        data.title = dic[@"title"];
        data.desc = dic[@"content"];
        data.url = dic[@"url"];
        
        data.img = dic[@"img_url"];
        
        --上面基本和小程序类似,下面粘友盟的代码
            //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
     //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:data_.title?:@"" descr:data_.desc?:@"" thumImage:data_.image];
        //设置网页地址
        shareObject.webpageUrl = data_.url?:@"";
        
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
         //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self.mObject completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];        

Because two Manager singleton packages are used in the actual project to process judgment and classification, some of the code pasted above may not be able to run directly. If you have any questions, please leave a message below...

Encountered pit

No response when sharing a WeChat Mini Program

1. The webpageUrl in UMShareMiniProgramObject cannot be empty.
Because the interface returned the mini program as the sharing form at that time, the url was not given, so the webpageUrl was given empty when assigning the value, which made it impossible to adjust WeChat. And at that time, it seemed that it was adapted to the low-version WeChat web link, so I didn't pay much attention to it, so that I found this problem after searching for a long time...

2. The sharing applet is also based on the UMSocialPlatformType_WechatSession WeChat chat type, which cannot be customized, otherwise Umeng cannot activate WeChat.
Because the product requires sharing to WeChat chat, there are not only small program types, but also other types, so at that time, I was clever and added a new type in UMSocialPlatformConfig.h of Youmeng, insert image description here
so that it could not be adjusted. And at that time, because the webpageUrl in reason 1 was empty, it could not be called up, so here it was also unable to be called up before the custom type, and there was no response, so I thought it was not the problem. Later, I realized that the WeChat chat applet is now also based on WeChat chat. It is just a style in the chat and not a form juxtaposed with the chat and circle of friends... But at that time, because of the 1 and 2 problems, I couldn’t find it for a long time. no problem found...

I have encountered these two pitfalls for the time being, and I will add them later... The content written above and the sticky code are too hasty, please correct me if there are any mistakes...

Guess you like

Origin blog.csdn.net/lichuanliangios/article/details/90308727