普元微博分享示例

2020-01-13  本文已影响0人  红凉梦

2020.3.3 修改 微博分享 oc--js--未提交

微博分享,以及MTA统计

Method Parameter Description Return 示例
shareWeibochat(data,function(retCode,retMsg) //image、text、webUrl可以允许单项为空,图片、文字、与多媒体不能组合分享
var data = {
"image" : {"imagePath" : "/images/app.png"} ,
"text" :{ "text" : "我要分享的内容是2月14日情人节" } ,
"webUrl" : { "actionUrl" : "https://weibo.com",
"defaultText" : "默认网页",
"description" : "描述文字",
"thumbData" : "/images/app.png",
"title" : "测试网页"
}
}
微博分享 Utils.shareWeibochat(data,function(retCode,retMsg){
log(retCode+','+retMsg);
});
Utils.beginLoadPage('xxxx.html'); 2 开始进入界面 3 $M.page.addEvent('onLoad', function(param{
Utils.beginLoadPage('index.html');
});
Utils.endLoadPage('xxx.html'); 2 离开界面时使用,与开始结合使用,否则无效 3 Utils.endLoadPage('xxx.html');
$M.page.goTo('/xxx.htmlx', null, false);
Utils.eventIDClick(eventid,lableType); @param event_id 事件的ID,ID需要先在MTA前台配置好才能生效
lableType:事件类型
统计点击事件 function share(){Utils.eventIDClick("share","shareType");}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>new title</title>
</head>
<body>
<nav title="测试分享"/>
<div  width="100%" >
<input type="label" value="分享到" />
<select id="select" value="session" width="100%" >
  <option text="会话" value="session" ></option>
  <option text="朋友圈" value="timeline" ></option>
  <option text="收藏" value="favorite" ></option>
</select>
  </div>
  <input type="button"  value="分享微博" onClick="shareWeibo" width="100%"/>
  
  </body>
  <script>
  $M.page.addEvent('onLoad', function(param){
        Utils.beginLoadPage('index.html');
  });

    function shareWeibo(){
        var data = {
            "image" :"" /* {
                    "imagePath" : "/images/app.png"
                     } */,
            "text" :"" /* {
                    "text" : "我要分享的内容是2月14日情人节"
                     } */,
             "webUrl" : {
                     "actionUrl" : "https://weibo.com",
                    "defaultText" : "默认网页",
                    "description" : "描述文字",
                    "thumbData" : "/images/app.png",
                    "title" : "测试网页"
                    } 
            }
    
        Utils.shareWeibochat(data,function(retCode,retMsg){
            log(retCode+','+retMsg);
        });
    }

  
 
  </script>
  </html>

OC端

//
//  SkyShareWeiboSdk.m
//  EmpBridge
//
//  Created by GuoYanjun on 2019/12/30.
//  Copyright © 2019 LiuLei. All rights reserved.
//

#import "SkyShareWeiboSdk.h"
#import "SkyDataRef.h"
#import "SKY_FileUrl.h"
#import "ResourceManager.h"
#import "SKY_Image.h"
@implementation SkyShareWeiboSdk
-(instancetype)init{
    if (self =[super init]) {
        [SkyWeiboManager sharedManager].delegate = self;
    }
    return self;
}

-(SkyDataRef *)messageShare:(NSDictionary *)param{
    SkyWeiboManager *mydele=[[SkyWeiboManager alloc]init];
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = @"https://api.weibo.com/oauth2/default.html";
    authRequest.scope = @"all";
    
    
    WBMessageObject *message = [WBMessageObject message];
    if ([param stringValue:@"image"] && ![[param stringValue:@"image"] isKindOfClass:[NSNull class]] && [param stringValue:@"image"].length >0){
        NSDictionary *imageDic =param[@"image"];
        NSString *imagePath =[imageDic stringValue:@"imagePath"];
                NSString *resUrl1 = [[ResourceManager instance] getResourcePathFormRes:imagePath];
        UIImage * thumbImage = nil;
        UIImage *originalImage = nil;
        originalImage = [SKY_FileUrl imageWithUrl:resUrl1];
        thumbImage = [SKY_Image compressImage:originalImage withScale:100];
        NSData * imageData = [NSData dataWithContentsOfFile:[SKY_FileUrl getRealPath:resUrl1]];
        WBImageObject *wbImg = [[WBImageObject alloc] init];
        wbImg.imageData =imageData;
        message.imageObject = wbImg;
    }
    if ([param stringValue:@"text"] && ![[param stringValue:@"text"] isKindOfClass:[NSNull class]] && [param stringValue:@"text"].length >0){
        NSDictionary *textDic =param[@"text"];
        NSString *text =[textDic stringValue:@"text"];
        message.text =text;
    }

    if ([param stringValue:@"webUrl"] && ![[param stringValue:@"webUrl"] isKindOfClass:[NSNull class]] && [param stringValue:@"webUrl"].length >0){
        NSDictionary *webUrlDic = param[@"webUrl"];
        NSString *title = [webUrlDic stringValue:@"title"];
        NSString *defaultText = [webUrlDic stringValue:@"defaultText"];
        NSString *description = [webUrlDic stringValue:@"description"];
        NSString *thumbData = [webUrlDic stringValue:@"thumbData"];
        NSString *actionUrl = [webUrlDic stringValue:@"actionUrl"];

       NSString *resUrl1 = [[ResourceManager instance] getResourcePathFormRes:thumbData];
       UIImage * thumbImage = nil;
       UIImage *originalImage = nil;
       originalImage = [SKY_FileUrl imageWithUrl:resUrl1];
       thumbImage = [SKY_Image compressImage:originalImage withScale:100];
        message.text = [NSString stringWithFormat:@"%@ %@ %@",NSLocalizedString(description, nil),actionUrl,NSLocalizedString(title, nil)];
        WBImageObject *imageOb = [WBImageObject object];
        imageOb.imageData =UIImagePNGRepresentation(thumbImage);
        message.imageObject = imageOb;
    }


    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:mydele.wbtoken];
    [WeiboSDK sendRequest:request];
    return successObjToDataRef(NULL);
}

- (void)managerDidRecvGetMessageReq:(WBSendMessageToWeiboResponse *)response{
    
     if (response.statusCode == WeiboSDKResponseStatusCodeSuccess) {
            //NSLog(@"微博----分享成功!!!");
             [self triggerEvent:@"ShareSuccess",intToDataRef(0),stringToDataRef(@"分享成功"),Nil];
     }else if(response.statusCode == WeiboSDKResponseStatusCodeUserCancel)
         {
     //        NSLog(@"微博----用户取消发送");
            [self triggerEvent:@"ShareSuccess",intToDataRef(-1),stringToDataRef(@"取消分享"),Nil];
         }else if (response.statusCode == WeiboSDKResponseStatusCodeSentFail){
     //        NSLog(@"微博----发送失败!");
            [self triggerEvent:@"ShareSuccess",intToDataRef(-2),stringToDataRef(@"发送失败"),Nil];
         }
}

@end

上一篇下一篇

猜你喜欢

热点阅读