参考(tiny4cocoa):http://tiny4cocoa.com/thread-1771-1-1.html
参考(csdn):http://blog.csdn.net/sjzsp/article/details/6338842
1. 首先在http://open.t.sina.com.cn / 中申请成为开发者,再创建不同的应用,获得相应的 App Key (在下面链接中的source即为app key)
2. 登录认证:
NSString *authString = [ NSString stringWithFormat : @"%@:%@" , sinaIDField . text ,sinaPasswordField . text ];
NSData *authData = [authString dataUsingEncoding : NSUTF8StringEncoding ];
NSString *authValue = [ NSString stringWithFormat : @"Basic %@" ,[authData base64EncodingWithLineLength : 80 ]];
NSURL *url = [ NSURL URLWithString : @"http://api.t.sina.com.cn/account/verify_credentials.xml?source=3930264715 " ];
NSMutableURLRequest *request = [[ NSMutableURLRequest alloc ] initWithURL :url];
[request setHTTPMethod : @"GET" ];
[request setValue :authValue forHTTPHeaderField : @"Authorization" ];
NSURLResponse *response;
NSError *error;
[ NSURLConnection sendSynchronousRequest :request returningResponse :&response error :&error];
[request release ];
NSHTTPURLResponse *httpResponse = ( NSHTTPURLResponse *)response;
int statusCode = [httpResponse statusCode ];
NSLog ( @"status code = %d" ,statusCode);
if (statusCode != 200 ) {
alertTitle = @" 帐号或密码错误 " ;
alertMassage = @" 请您输入正确的帐号和密码! " ;
} else {
alertTitle = @"" ;
alertMassage = @" 登录成功! " ;
}
}
UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle :alertTitle
message :alertMassage
delegate : nil cancelButtonTitle : @" 确定 "
otherButtonTitles : nil ];
[alert show ];
[alert release ];
3. 将内容(图片 文字)发送到新浪微博
NSString *authString = [ NSString stringWithFormat : @"%@:%@" ,[[ NSUserDefaults standardUserDefaults ] objectForKey : @"sinaID" ],[[ NSUserDefaults standardUserDefaults ] objectForKey : @"sinaPassword" ]];
NSData *authData = [authString dataUsingEncoding : NSUTF8StringEncoding ];
NSString *authValue = [ NSString stringWithFormat : @"Basic %@" ,[authData base64EncodingWithLineLength : 80 ]];
NSString *boundary = @"0xKhTmLbOuNdArYckkk" ;
NSString *filename = @" test .jpg" ;
NSData *imageData = UIImageJPEGRepresentation (shareImage, 1 );
NSString *bodyPrefixString = [ NSString stringWithFormat : @"--%@/r/n" , boundary];
NSString *bodySuffixString = [ NSString stringWithFormat : @"/r/n--%@--/r/n" , boundary];
NSString *contentDisposition = [ NSString stringWithFormat : @"Content-Disposition: form-data; name="pic"; filename="%@"/r/n" , filename];
NSString *contentImageType = [ NSString stringWithFormat : @"Content-Type: image/%@/r/n" , [filename pathExtension ]];
NSString *contentTransfer = @"Content-Transfer-Encoding: binary/r/n/r/n" ;
NSString *bodyUpdateField = [ NSString stringWithFormat : @"Content-Disposition: form-data;name="status"/r/n/r/n%@/r/n" ,[ NSString stringWithFormat : @"%@" , textView . text ]];
NSMutableData *postBody = [ NSMutableData data ];
[postBody appendData :[bodyPrefixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];
[postBody appendData :[bodyUpdateField dataUsingEncoding : NSUTF8StringEncoding ]];
[postBody appendData :[bodyPrefixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];
[postBody appendData :[contentDisposition dataUsingEncoding : NSUTF8StringEncoding ]];
[postBody appendData :[contentImageType dataUsingEncoding : NSUTF8StringEncoding ]];
[postBody appendData :[contentTransfer dataUsingEncoding : NSUTF8StringEncoding ]];
[postBody appendData :imageData];
[postBody appendData :[bodySuffixString dataUsingEncoding : NSUTF8StringEncoding allowLossyConversion : NO ]];
NSString *baseURL = [ NSString stringWithFormat :@"http://api.t.sina.com.cn/statuses/upload.xml?source=3930264715 " ];
NSURL *url = [ NSURL URLWithString :baseURL];
NSMutableURLRequest *mainRequest = [[ NSMutableURLRequest alloc ] initWithURL :url
cachePolicy : NSURLRequestUseProtocolCachePolicy
timeoutInterval : 30.0f ];
[mainRequest setHTTPMethod : @"POST" ];
[mainRequest setValue :authValue forHTTPHeaderField : @"Authorization" ];
NSString *contentType = [ NSString stringWithFormat : @"multipart/form-data; boundary=%@" , boundary, nil ];
[mainRequest setValue :contentType forHTTPHeaderField : @"Content-Type" ];
[mainRequest setHTTPBody :postBody];
NSURLResponse *shareResponse;
NSError *error;
NSData *responseData = [ NSURLConnection sendSynchronousRequest :mainRequest returningResponse:&shareResponse error :&error];
NSString *responseString = [[ NSString alloc ] initWithData :responseData encoding :NSUTF8StringEncoding ];
NSHTTPURLResponse *httpResponse = ( NSHTTPURLResponse *)shareResponse;
int statusCode = [httpResponse statusCode ];
NSLog ( @"status code = %d" ,statusCode);
BOOL succeed = NO ;
if (statusCode == 200 ) {
succeed = YES ;
}
[mainRequest release ];
NSLog ( @"response string : %@" ,responseString);
[responseString release ];
[ uploadWaiting stopAnimating ];
NSString *message = nil ;
if (succeed) {
message = @" 分享成功 " ;
} else {
message = @" 分享失败 " ;
}
UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle : @"" message :message
delegate : self
cancelButtonTitle : @" 确定 " otherButtonTitles : nil ];
[alert show ];
[alert release ];
分享到:
相关推荐
ios分享到新浪微博的源码,供有需要的人使用!
对于新浪微博分享,里面有详细的文档介绍
iOS 新浪微博分享,调用官方的sdk, 欢迎学习,下载
本文demo的核心功能:微博授权、自定义表情键盘、图文混排 分页刷新控件集成文章:...iOS集成新浪微博API教程:https://blog.csdn.net/z929118967/article/details/75201062
ios新浪微博分享,新浪官方文档,详细代码介绍
iOS天气查询小demo 可将天气信息分享到新浪微博 输入未做判断,可自行改进。城市名称请去ViewDidLoad中查询输入测试 欢迎学习交流。
如对你有帮助,动一动你的小手,不要忘记star哦! https://github.com/dongpeng66/DPScreenshots.git
主要为大家详细介绍了iOS新浪微博、腾讯微博分享功能实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近有空整理一下项目,做了一个仿赤兔、新浪微博动态列表(带评论、点赞、转发和分享,自动计算行高功能)的DEMO。HKPTimeLine ,喜欢的可以star一下哦。 csdn : ...
网上最常见的IPHONE新浪微博客户端代码。做的很好,和大家分享。
本人自己实现的支持腾讯新浪微博分享,支持outh2.0
本文实例为大家分享了Android九...开发部分在下面ios新浪微博分享(2)这部分: 开发需要下载官方的sdkhttp://open.weibo.com/wiki/SDK#iOS_SDK 本人下载的版本 新建一个viewcontrroler==WeiBoViewController 效果图
iOS开发中,手机截图分享功能.类似新浪微博app
使用shareSDK进行登录和分享,方便快捷。可在我的博客查看详细讲解,博客地址:http://blog.csdn.net/baidu_21172753
IOS应用源码之【框架】ShareKit - 分享到开心,豆瓣,腾讯,新浪微博.rar
新浪微博,人人,腾讯微博分享信息例子,只需要导入就可以了 非常简单方便
利用ShareSDK实现第三方分享,支持的平台有(QQ,微信,新浪微博),需去mob官网绑定自己的应用和去第三方平台申请Appkey (上传的是空文件,不要下载..)文件太大了 , 包含shareSDK 所以 请去GitHub上下载 :...