![新浪微博 分享 功能的实现](https://100zi.50zi.cn/uploadfile/img/15/551/4baca1ac0a96530a72588a7e7f5ac40a.jpg)
1.首先在http://open./中申请成为开发者,再创建不同的应用,获得相应的App Key(在下面链接中的source即为app key)
2.登录认证:
NSString*authString = [NSStringstringWithFormat:@"%@:%@",sinaIDField.text,sinaPasswordField.text];
NSData*authData = [authStringdataUsingEncoding:NSUTF8StringEncoding];
NSString*authValue = [NSStringstringWithFormat:@"Basic %@",[authDatabase64EncodingWithLineLength:80]];
NSURL*url = [NSURLURLWithString:@"http://api./account/verify_credentials.xml?source=3930264715"];
NSMutableURLRequest*request = [[NSMutableURLRequestalloc]initWithURL:url];
[requestsetHTTPMethod:@"GET"];
[requestsetValue:authValueforHTTPHeaderField:@"Authorization"];
NSURLResponse*response;
NSError*error;
[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:&error];
[requestrelease];
NSHTTPURLResponse*httpResponse = (NSHTTPURLResponse*)response;
intstatusCode = [httpResponsestatusCode];
NSLog(@"status code = %d",statusCode);
if(statusCode !=200) {
alertTitle =@"帐号或密码错误";
alertMassage =@"请您输入正确的帐号和密码!";
}else{
alertTitle =@"";
alertMassage =@"登录成功!";
}
}
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:alertTitle
message:alertMassage
delegate:nilcancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertshow];
[alertrelease];
3.将内容(图片文字)发送到新浪微博
NSString*authString = [NSStringstringWithFormat:@"%@:%@",[[NSUserDefaultsstandardUserDefaults]objectForKey:@"sinaID"],[[NSUserDefaultsstandardUserDefaults]objectForKey:@"sinaPassword"]];
NSData*authData = [authStringdataUsingEncoding:NSUTF8StringEncoding];
NSString*authValue = [NSStringstringWithFormat:@"Basic %@",[authDatabase64EncodingWithLineLength:80]];
NSString*boundary =@"0xKhTmLbOuNdArYckkk";
NSString*filename =@"test.jpg";
NSData*imageData =UIImageJPEGRepresentation(shareImage,1);
NSString*bodyPrefixString= [NSStringstringWithFormat:@"--%@/r/n", boundary];
NSString*bodySuffixString= [NSStringstringWithFormat:@"/r/n--%@--/r/n", boundary];
NSString*contentDisposition = [NSStringstringWithFormat:@"Content-Disposition: form-data; name="pic"; filename="%@"/r/n", filename];
NSString*contentImageType= [NSStringstringWithFormat:@"Content-Type: image/%@/r/n", [filenamepathExtension]];
NSString*contentTransfer=@"Content-Transfer-Encoding: binary/r/n/r/n";
NSString*bodyUpdateField = [NSStringstringWithFormat:@"Content-Disposition: form-data;name="status"/r/n/r/n%@/r/n",[NSStringstringWithFormat:@"%@",textView.text]];
NSMutableData*postBody = [NSMutableDatadata];
[postBodyappendData:[bodyPrefixStringdataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:NO]];
[postBodyappendData:[bodyUpdateFielddataUsingEncoding:NSUTF8StringEncoding]];
[postBodyappendData:[bodyPrefixStringdataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:NO]];
[postBodyappendData:[contentDispositiondataUsingEncoding:NSUTF8StringEncoding]];
[postBodyappendData:[contentImageTypedataUsingEncoding:NSUTF8StringEncoding]];
[postBodyappendData:[contentTransferdataUsingEncoding:NSUTF8StringEncoding]];
[postBodyappendData:imageData];
[postBodyappendData:[bodySuffixStringdataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:NO]];
NSString*baseURL = [NSStringstringWithFormat:@"http://api./statuses/upload.xml?source=3930264715"];
NSURL*url = [NSURLURLWithString:baseURL];
NSMutableURLRequest*mainRequest = [[NSMutableURLRequestalloc]initWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0f];
[mainRequestsetHTTPMethod:@"POST"];
[mainRequestsetValue:authValueforHTTPHeaderField:@"Authorization"];
NSString*contentType = [NSStringstringWithFormat:@"multipart/form-data; boundary=%@", boundary,nil];
[mainRequestsetValue:contentTypeforHTTPHeaderField:@"Content-Type"];
[mainRequestsetHTTPBody:postBody];
NSURLResponse*shareResponse;
NSError*error;
NSData*responseData = [NSURLConnectionsendSynchronousRequest:mainRequestreturningResponse:&shareResponseerror:&error];
NSString*responseString = [[NSStringalloc]initWithData:responseDataencoding:NSUTF8StringEncoding];
NSHTTPURLResponse*httpResponse = (NSHTTPURLResponse*)shareResponse;
intstatusCode = [httpResponsestatusCode];
NSLog(@"status code = %d",statusCode);
BOOLsucceed =NO;
if(statusCode ==200) {
succeed =YES;
}
[mainRequestrelease];
NSLog(@"response string : %@",responseString);
[responseStringrelease];
[uploadWaitingstopAnimating];
NSString*message =nil;
if(succeed) {
message =@"分享成功";
}else{
message =@"分享失败";
}
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@""message:message
delegate:self
cancelButtonTitle:@"确定"otherButtonTitles:nil];
[alertshow];
[alertrelease];