一、集成微博SDK
1.添加SDK文件
2.添加编译选项
新浪微博 SDK 附带的文档中有这么一个说明:
在工程中引入静态库之后,需要在编译时添加 -ObjC 编译选项,避免静态库中类 加载 不全造成程序崩溃。方法:程序 Target->Buid Settings->Linking 下 Other Linker Flags 项添加 -ObjC
然而会崩溃,然后在网上看到遇到同样崩溃错误的人有提到在编译时添加 -all_load 编译选项时也可以解决问题。
3.添加FrameWork文件到工程
二、使用微博分享
1.设置工程回调URL Schemes
什么是URL Schemes?
URL Schemes是苹果给出的用来跳转到系统应用或者跳转到别人的应用的一种机制。同时还可以在应用之间传数据。
备注:一个应用是可以有多个URL Schemes的。你可以再次点击“+”来添加一个URL Schemes
如下图所示:其中Identifier应为自己的SSO授权回调地址,URL Scheme则为"wb"+"app key"。
2.添加URL Schemes白名单
iOS9以后,应用需要在“Info.plist”中将要使用的URL Schemes列为白名单,才可正常检查其他应用是否安装。
如下图所示:在Info.plist中添加key为"LSApplicationQueriesSchemes" ,值为Array。在Array中添加以下String类型的值:"sinaweibohd","sinaweibo","weibosdk","weibosdk2.5"
另附“QQ/QQ空间/支付宝/微信SDK”相关白名单
<key>LSApplicationQueriesSchemes</key>
<array>
<!-- 微信 URL Scheme 白名单-->
<string>wechat</string>
<string>weixin</string>
<!-- 新浪微博 URL Scheme 白名单-->
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<!-- QQ、Qzone URL Scheme 白名单-->
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqzoneopensdk</string>
<!-- 支付宝 URL Scheme 白名单-->
<string>alipay</string>
<string>alipayshare</string>
</array>
3.代码部分
(1)AppDelegate.m
注册app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//向微博客户端程序注册第三方应用
[WeiboSDK registerApp:kAppKey];
//设置WeiboSDK的调试模式
[WeiboSDK enableDebugMode:YES];
// Override point for customization after application launch.
return YES;
}
在AppDelegate.m中复写
将在从微博客户端回到App时调用
//ios9以上
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<NSString *,id> *)options {
[WeiboSDK handleOpenURL:url delegate:self];
return YES;
}
//ios9 以下
- (BOOL)application:(UIApplication*)application openURL:(NSURL *)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation{
[WeiboSDK handleOpenURL:url delegate:self];
return YES;
}
收到一个来自微博客户端程序的响应
#pragma mark -- WeiboSDKDelegate
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
{
NSString *title = NSLocalizedString(@"发送结果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode, NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil),response.requestUserInfo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:nil];
WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
[alert show];
}
}
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {
}
(2)分享操作
WBMessageObject *wbmsg = [WBMessageObject message];
wbmsg.text = _textField.text;
WBImageObject *wbImg = [[WBImageObject alloc] init];
NSData *imageData = UIImagePNGRepresentation(_imageView.image);
wbImg.imageData = imageData;
wbmsg.imageObject = wbImg;
WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
authRequest.redirectURI = @"http://www.sina.com";
authRequest.scope = @"all";
WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:wbmsg authInfo:authRequest access_token:nil];
/*app之间数据交互*/
// request.userInfo = @{@"ShareMessageFrom": @"ViewController",
// @"Other_Info_1": [NSNumber numberWithInt:123],
// @"Other_Info_2": @[@"obj1", @"obj2"],
// @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
[WeiboSDK sendRequest:request];
这就是微博分享的环境搭建和基本使用的全部内容了。