SDK版本3.3.2 测试时间20220329
1.首先要有APPKey,这个不归我管,没有要自行申请
2.代码实现:
授权代码:假设类名是 WeiBoShare
+(void)WeiBoAuth
{
#ifdef DEBUG
[WeiboSDK enableDebugMode:YES];
#endif
if ([WeiboSDK isWeiboAppInstalled]) {
NSLog(@"weibo installed");
}
else
{
NSLog(@"weibo not installed");
}
[WeiboSDK banGetIdfa:NO]; //不要获取IDFA
[WeiboSDK registerApp:WeiBoAPPID universalLink:WeiBoUlink]; //UniversalLink跟申请微信的配置差不多,微博这边压根就没啥指导,可以去微信开放平台参考一下。
#ifdef DEBUG
//下面这行是测试是否已经连接成功。在打开app的时候可以看到连接图标,调用微博的。调通后不需要这句。
[WeiboSDK checkUniversalLink:^(WBULCheckStep step, NSError * _Nullable error) {
NSLog(@"111step=%ld error=%@",step,error);
}];
#endif
}
在AppDelegate.m里面的didFinishLaunchingWithOptions函数调用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//----
[WeiBoShare WeiBoAuth];
//-----
}
需要实现的反馈函数(可以放AppDelegate.m,也可以指定Delegate):
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
NSLog(@"return:%@",request);
}
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
//从微博返回的一些信息,对于分享没多大意义
}
//回调需要考虑2种情况,例子简单,我都丢AppDeletege.m,注意 #import "WeiBoSDK.h"
//设置Universal Links系统回调
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
return [WeiboSDK handleOpenUniversalLink:userActivity delegate:(id)self];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
分享网页:
-(void)shareWeiBo:(NSDictionary *)info
{
if (![WeiboSDK isWeiboAppInstalled]) {
return;
}
if (![WeiboSDK isCanShareInWeiboAPP]) {
return;
}
WBMessageObject *message = [WBMessageObject message];
NSString *title = info[@"title"];
NSString *desc = info[@"desc"];
NSString *link = info[@"link"];
//听说WBWebpageObject没法用,到时候只能用这个text接口了,3.3.2版本还能用WBWebpageObject,只不过样式很挫。
message.text = @""; //这里不填写,貌似跳不到微博编辑界面;
NSString *thumb_str = info[@"thumb"];
NSURL *thumb_url = [NSURL URLWithString:thumb_str];
NSData *orgData = nil;
if (thumb_url) {
orgData = [NSData dataWithContentsOfURL:thumb_url];
}
WBWebpageObject *pageObj = [[WBWebpageObject alloc] init];
pageObj.objectID = [NSString stringWithFormat:@"%lld",(long long)[NSDate date].timeIntervalSince1970*1000];
pageObj.scheme = link; //说是回调的网页,但是不知道啥用
pageObj.title = title;
pageObj.description = desc;
pageObj.webpageUrl = link; //这里很坑,说是弃用了,但是如果不传递参数就会报错。
NSData *resData = orgData; //32k限制,这里没写缩放图片的代码
pageObj.thumbnailData = resData;
// 图片分享
//WBImageObject *imageObj = [WBImageObject object];
//imageObj.imageData = resData;
//message.imageObject = imageObj;
message.mediaObject = pageObj;
//分享多媒体,数据对象类为WBBaseMediaObject (已经弃用,不必实现这段代码)
WBSendMessageToWeiboRequest *request =[WBSendMessageToWeiboRequest requestWithMessage:message];
[WeiboSDK sendRequest:request completion:^(BOOL success) {
NSLog(@"success=%@",success?@"true":@"false");
}];
}
见过微信SDK恨的牙痒痒,但是调通后用的还可以,但是微博分享SDK做的很差,这个啥功能都砍了,啥都要💰,另外让我对微博观感差的就是随意关联了个恶心的号到我手机号,还是被封杀了的,弄的我调试还得换新号,吐血三升。。。
最后关于info.plist设置:
这个版本不需要太多,只需要下面两个key上填点就搞定,NSAppTransportSecurity已经不需要配置。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>sinaweibo</string>
<string>sinaweibohd</string>
<string>sinaweibosso</string>
<string>sinaweibohdsso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<string>weibosdk3.3</string>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>weibo</string>
<key>CFBundleURLSchemes</key>
<array>
<string>wb{微博APPID}</string>
</array>
</dict>
</array>
20220330更新:
WBWebpageObject目测太挫了,还是用文字+图片的形式更帅气。
//..... 前省略。。。
message.text = [NSString stringWithFormat:@"%@\n%@\n%@",title,desc,link];
WBImageObject *imageObj = [WBImageObject object];
imageObj.imageData = orgData;
imageObj.delegate = (id)self;
message.imageObject = imageObj;
//.....
|