iPhone程序中将内容分享到新浪微博

转载 2012年03月21日 16:35:50
因为要用到,所以找来,和大家分享:

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:@"--%@rn", boundary];

NSString *bodySuffixString = [NSString stringWithFormat:@"rn--%@--rn", boundary];

NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name="pic"; filename="%@"rn", filename];

NSString *contentImageType = [NSString stringWithFormat:@"Content-Type: image/%@rn", [filename pathExtension]];

NSString *contentTransfer = @"Content-Transfer-Encoding: binaryrnrn";

 

NSString *bodyUpdateField = [NSString stringWithFormat:@"Content-Disposition: form-data;name="status"rnrn%@rn",[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];

相关文章推荐

iPhone程序中将内容分享到新浪微博(zz)

iPhone程序中将内容分享到新浪微博 1. 首先在http://open.t.sina.com.cn/中申请成为开发者,再创建不同的应用,获得相应的App Key (在下面链接中的sourc...

iPhone程序中将内容分享到新浪微博

因为要用到,所以找来,和大家分享: 1. 首先在http://open.t.sina.com.cn/中申请成为开发者,再创建不同的应用,获得相应的App Key (在下面链接中的source即为ap...

iPhone程序中将内容分享到新浪微博

因为要用到,所以找来,和大家分享: 1.  首先在http://open.t.sina.com.cn / 中申请成为开发者,再创建不同的应用,获得相应的 App Key  (在下面链接中...

iphone尾巴新浪微博

  • 2012-06-17 23:21
  • 2.25MB
  • 下载

(droid分享)新浪微博开发系列【九】之开发者信息显示和程序退出

此部分就是我的个人信息的显示和退出程序,好了,贴代码,在此说明一下,可能代码里调用一些方法在本篇中没有,但是全部在我的代码里,也不要索取图片,要看全部代码的请移步http://blog.csdn.ne...

新浪微博内容采集规则

新浪微博来自iphone

  • 2012-02-03 15:03
  • 604KB
  • 下载

2011年沈大海讲师Iphone的新浪微博客户端OAuth源代码

对于Iphone的新浪微博客户端其实现基本流程如下  1.根据Appkey 请求 http://api.t.sina.com.cn/oauth/requtst_token 获取未授权的request...
  • sdhjob
  • sdhjob
  • 2011-08-23 10:34
  • 3036

新浪微博iPhone接口版

  • 2011-03-29 22:24
  • 2.48MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)