iPhone程序中将内容分享到新浪微博
2011-05-27 17:30
330 查看
因为要用到,所以找来,和大家分享:
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];
ps:苹果不支持64位编码格式,所以需要自己编写编码方法
static char encodingTable[64] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};
- (NSString *) base64EncodingWithLineLength:(unsigned int) lineLength data:(NSData *)dataObject {
const unsigned char *bytes = [dataObject bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:[dataObject length]];
unsigned long ixtext = 0;
unsigned long lentext = [dataObject length];
long ctremaining = 0;
unsigned char inbuf[3], outbuf[4];
short i = 0;
short charsonline = 0, ctcopy = 0;
unsigned long ix = 0;
while( YES ) {
ctremaining = lentext - ixtext;
if( ctremaining <= 0 ) break;
for( i = 0; i < 3; i++ ) {
ix = ixtext + i;
if( ix < lentext ) inbuf[i] = bytes[ix];
else inbuf [i] = 0;
}
outbuf [0] = (inbuf [0] & 0xFC) >> 2;
outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
outbuf [3] = inbuf [2] & 0x3F;
ctcopy = 4;
switch( ctremaining ) {
case 1:
ctcopy = 2;
break;
case 2:
ctcopy = 3;
break;
}
for( i = 0; i < ctcopy; i++ )
[result appendFormat:@"%c", encodingTable[outbuf[i]]];
for( i = ctcopy; i < 4; i++ )
[result appendString:@"="];
ixtext += 3;
charsonline += 4;
if( lineLength > 0 ) {
if (charsonline >= lineLength) {
charsonline = 0;
[result appendString:@"/n"];
}
}
}
return [NSString stringWithString:result];
}
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];
ps:苹果不支持64位编码格式,所以需要自己编写编码方法
static char encodingTable[64] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};
- (NSString *) base64EncodingWithLineLength:(unsigned int) lineLength data:(NSData *)dataObject {
const unsigned char *bytes = [dataObject bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:[dataObject length]];
unsigned long ixtext = 0;
unsigned long lentext = [dataObject length];
long ctremaining = 0;
unsigned char inbuf[3], outbuf[4];
short i = 0;
short charsonline = 0, ctcopy = 0;
unsigned long ix = 0;
while( YES ) {
ctremaining = lentext - ixtext;
if( ctremaining <= 0 ) break;
for( i = 0; i < 3; i++ ) {
ix = ixtext + i;
if( ix < lentext ) inbuf[i] = bytes[ix];
else inbuf [i] = 0;
}
outbuf [0] = (inbuf [0] & 0xFC) >> 2;
outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
outbuf [3] = inbuf [2] & 0x3F;
ctcopy = 4;
switch( ctremaining ) {
case 1:
ctcopy = 2;
break;
case 2:
ctcopy = 3;
break;
}
for( i = 0; i < ctcopy; i++ )
[result appendFormat:@"%c", encodingTable[outbuf[i]]];
for( i = ctcopy; i < 4; i++ )
[result appendString:@"="];
ixtext += 3;
charsonline += 4;
if( lineLength > 0 ) {
if (charsonline >= lineLength) {
charsonline = 0;
[result appendString:@"/n"];
}
}
}
return [NSString stringWithString:result];
}
相关文章推荐
- [转载]iPhone程序中将内容分享到新浪微博
- iPhone程序中将内容分享到新浪微博
- iPhone程序中将内容分享到新浪微博
- iPhone程序中将内容分享到新浪微博(zz)
- iPhone程序中将内容分享到新浪微博
- iPhone程序中将内容分享到新浪微博
- iPhone程序中将内容分享到新浪微博
- iPhone程序中将内容分享到新浪微博
- iPhone程序中将内容分享到新浪微博
- unity3d利用sharesdk分享内容至微信、新浪微博、qq空间等国内主流社交平台(ios篇)
- 分享一个用Xcode4实现基于Webservice用户登录的iphone程序
- Android工作中遇到的问题:友盟分享到新浪微博,内容被放入草稿箱
- 分享iPhone判断程序运行设备的代码
- 改良版twitter for iphone的照片分享程序
- 内容正文选中一段文字分享到qq微博、新浪微博的代码
- 分享iPhone开发者需掌握的音频内容
- 通过友盟分享内容到新浪微博错误解决方法
- 使用Intent.ACTION_SEND分享图片和文字内容(新浪微博,短信等)
- Android 应用程序之间内容分享 当不存在能够分享的程序 [整理]
- js实现将选中的内容分享到新浪微博,腾讯微博