iOS - 新浪微博分享的简单使用

写在前面

其实微信、QQ分享、新浪微博分享楼主本来应该写一篇,但是担心写在一起太多太乱不容易查看,对于前面的基本配置请看微信分享的那篇文章,我这里同样直接写如何调用它的SDK来实现图文分享

1.向新浪微博注册

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 如果是调试模式 就打开这个
    //[WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:WBAPPID];
}

2.handle open url

- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary *)options {
    // 这里建议判断下
    return [WeiboSDK handleOpenURL:url delegate:self];
}

#pragma mark -- WeiboSDKDelegate
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
    if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
    {
        if (response.statusCode == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"成功" message:@"新浪微博分享成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"失败" message:@"新浪微博分享失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    else if ([response isKindOfClass:WBAuthorizeResponse.class]){
        if (response.statusCode == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"成功" message:@"新浪微博授权成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"失败" message:@"新浪微博授权失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    
}

- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {
    
}

3.开始使用

// 发布纯文本
- (void)shareSinaWeiboWithText:(NSString *)text {
    if (![WeiboSDK isWeiboAppInstalled]) {
        [self showLoadSinaWeiboClient];
    }else {
        
        WBMessageObject *message = [WBMessageObject message];
        message.text = text;
        
        WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
        [WeiboSDK sendRequest:request];
    }

}

// 发布图片文字等。
- (void)shareSinaWeiboWithText:(NSString *)text image:(UIImage *)image{
    if (![WeiboSDK isWeiboAppInstalled]) {
        [self showLoadSinaWeiboClient];
        }else {    
        WBMessageObject *message = [WBMessageObject message];
        message.text = text;
        
        // 消息的图片内容中,图片数据不能为空并且大小不能超过10M
        WBImageObject *imageObject = [WBImageObject object];
        imageObject.imageData = UIImageJPEGRepresentation(image, 1.0);
        message.imageObject = imageObject;
        
        WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
        [WeiboSDK sendRequest:request];
    }

}

4.测试

// 图片文字等。


iOS - 新浪微博分享的简单使用_第1张图片
1.png

总结

这些都是最基本最基本的用法,详细的可以参考他们的API文档

你可能感兴趣的:(iOS - 新浪微博分享的简单使用)