一、新浪微博分享的准备工作
1.新浪微博SDK在github托管https://github.com/mobileresearch/weibo_ios_sdk_sso-oauth,下载好后,直接拖到项目中。
2.添加依赖
3.在info.plist中加入LSApplicationQueriesSchemes,type是数组
这数组中新建四个item,type是string,value如下:
4.选择项目,targets,点击info,点开下面的URL Types,点击加号
在URL Schemes里填写wb后面填写微博的appid。
二、新浪微博分享的代码实现。
1.在AppDelegate.h文件中设置两个属性
@property (strong, nonatomic) NSString *wbtoken;
@property (strong, nonatomic) NSString *wbCurrentUserID;
2.初始化微博sdk
在头文件中输入#import "WeiboSDK.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WeiboSDK registerApp:@"882182138"];
return YES;
}
3.在控制器最上方输入#import "WeiboSDK.h",点击分享按钮后的代码如下:
- (IBAction)sinaShare:(id)sender {
// if (![WeiboSDK isWeiboAppInstalled]) {
// [self showLoadSinaWeiboClient];
// }else {
AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
authRequest.redirectURI = @"http://www.sina.com";
authRequest.scope = @"all";
WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:myDelegate.wbtoken];
request.userInfo = @{@"ShareMessageFrom": @"ViewController",
@"Other_Info_1": [NSNumber numberWithInt:123],
@"Other_Info_2": @[@"obj1", @"obj2"],
@"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
// request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
[WeiboSDK sendRequest:request];
NSLog(@"sina");
// }
}
- (WBMessageObject *)messageToShare
{
WBMessageObject *message = [WBMessageObject message];
message.text = NSLocalizedString(@"测试通过WeiboSDK发送文字到微博!http://www.baidu.com", nil);
WBImageObject *image = [WBImageObject object];
image.imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"]];
message.imageObject = image;
// WBWebpageObject *webpage = [WBWebpageObject object];
// webpage.objectID = @"identifier1";
// webpage.title = @"测试通过WeiboSDK发送文字到微博!";
// webpage.description = [NSString stringWithFormat:NSLocalizedString(@"分享网页内容简介-%.0f", nil), [[NSDate date] timeIntervalSince1970]];
// webpage.thumbnailData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"]];
// webpage.webpageUrl = @"http://resources.icloudcity.cn/resources/html/201610/news_1010.html";
// message.mediaObject = webpage;
return message;
}
三、分享成功后的回调
1.从微博端打开第三方APP会调用此方法
// 从微信端打开第三方APP会调用此方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
[WeiboSDK handleOpenURL:url delegate:self];
return YES;
}
- (BOOL)application:(UIApplication*)application openURL:(NSURL *)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation{
[WeiboSDK handleOpenURL:url delegate:self];
return YES;
}
2.让AppDelegate遵守WeiboSDKDelegate协议
3.实现协议里的方法
#pragma mark -- WeiboSDKDelegate
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
{
if(response.statusCode == WeiboSDKResponseStatusCodeSuccess){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"分享成功"
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"分享失败"
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:nil];
[alert show];
}
WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];
if (accessToken)
{
self.wbtoken = accessToken;
}
NSString* userID = [sendMessageToWeiboResponse.authResponse userID];
if (userID) {
self.wbCurrentUserID = userID;
}
}
}
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {
}