ios 分享到新浪微博

来源:互联网 发布:美莱村惨案 知乎 编辑:程序博客网 时间:2024/04/28 00:52
  1. 参考(tiny4cocoa):http://tiny4cocoa.com/thread-1771-1-1.html  
  2.   
  3. 参考(csdn):http://blog.csdn.net/sjzsp/article/details/6338842  
  4. 1.  首先在http://open.t.sina.com.cn / 中申请成为开发者,再创建不同的应用,获得相应的 App Key  (在下面链接中的source即为app key)  
  5.    
  6. 2.  登录认证:  
  7. NSString  *authString = [ NSString   stringWithFormat : @"%@:%@" , sinaIDField . text ,sinaPasswordField . text ];  
  8. NSData  *authData = [authString  dataUsingEncoding : NSUTF8StringEncoding ];  
  9. NSString  *authValue = [ NSString   stringWithFormat : @"Basic %@" ,[authData base64EncodingWithLineLength : 80 ]];  
  10.    
  11. NSURL  *url = [ NSURL   URLWithString : @"http://api.t.sina.com.cn/account/verify_credentials.xml?source=3930264715 " ];  
  12. NSMutableURLRequest  *request = [[ NSMutableURLRequest   alloc ]  initWithURL :url];  
  13. [request  setHTTPMethod : @"GET" ];  
  14. [request  setValue :authValue  forHTTPHeaderField : @"Authorization" ];  
  15.    
  16. NSURLResponse  *response;  
  17. NSError  *error;  
  18.    
  19. [ NSURLConnection   sendSynchronousRequest :request  returningResponse :&response  error :&error];  
  20.    
  21. [request  release ];  
  22.    
  23. NSHTTPURLResponse  *httpResponse = ( NSHTTPURLResponse  *)response;  
  24.    
  25. int  statusCode = [httpResponse  statusCode ];  
  26.    
  27. NSLog ( @"status code = %d" ,statusCode);  
  28.    
  29. if  (statusCode !=  200 ) {  
  30. alertTitle =  @" 帐号或密码错误 " ;  
  31. alertMassage =  @" 请您输入正确的帐号和密码! " ;  
  32. else  {  
  33. alertTitle =  @"" ;  
  34. alertMassage =  @" 登录成功! " ;  
  35. }  
  36. }  
  37. UIAlertView  *alert = [[ UIAlertView   alloc ]  initWithTitle :alertTitle  
  38. message :alertMassage  
  39. delegate : nil   cancelButtonTitle : @" 确定 "  
  40. otherButtonTitles : nil ];  
  41. [alert  show ];  
  42. [alert  release ];  
  43.    
  44.    
  45. 3. 将内容(图片   文字)发送到新浪微博  
  46. NSString  *authString = [ NSString   stringWithFormat : @"%@:%@" ,[[ NSUserDefaults  standardUserDefaults ]  objectForKey : @"sinaID" ],[[ NSUserDefaults   standardUserDefaults ] objectForKey : @"sinaPassword" ]];  
  47. NSData  *authData = [authString  dataUsingEncoding : NSUTF8StringEncoding ];  
  48. NSString  *authValue = [ NSString   stringWithFormat : @"Basic %@" ,[authData base64EncodingWithLineLength : 80 ]];  
  49.    
  50.    
  51. NSString  *boundary =  @"0xKhTmLbOuNdArYckkk" ;  
  52. NSString  *filename =  @" test .jpg" ;  
  53.    
  54. NSData  *imageData =  UIImageJPEGRepresentation (shareImage, 1 );  
  55.    
  56.    
  57. NSString  *bodyPrefixString = [ NSString   stringWithFormat : @"--%@/r/n" , boundary];  
  58. NSString  *bodySuffixString = [ NSString   stringWithFormat : @"/r/n--%@--/r/n" , boundary];  
  59. NSString  *contentDisposition = [ NSString   stringWithFormat : @"Content-Disposition: form-data; name="pic"; filename="%@"/r/n" , filename];  
  60. NSString  *contentImageType = [ NSString   stringWithFormat : @"Content-Type: image/%@/r/n" , [filename  pathExtension ]];  
  61. NSString  *contentTransfer  =  @"Content-Transfer-Encoding: binary/r/n/r/n" ;  
  62.    
  63. NSString  *bodyUpdateField = [ NSString   stringWithFormat : @"Content-Disposition: form-data;name="status"/r/n/r/n%@/r/n" ,[ NSString   stringWithFormat : @"%@" , textView . text ]];  
  64. NSMutableData  *postBody = [ NSMutableData   data ];  
  65.    
  66. [postBody  appendData :[bodyPrefixString  dataUsingEncoding : NSUTF8StringEncoding  allowLossyConversion : NO ]];  
  67. [postBody  appendData :[bodyUpdateField  dataUsingEncoding : NSUTF8StringEncoding  ]];  
  68. [postBody  appendData :[bodyPrefixString  dataUsingEncoding : NSUTF8StringEncoding  allowLossyConversion : NO ]];  
  69. [postBody  appendData :[contentDisposition  dataUsingEncoding : NSUTF8StringEncoding  ]];  
  70. [postBody  appendData :[contentImageType  dataUsingEncoding : NSUTF8StringEncoding  ]];  
  71. [postBody  appendData :[contentTransfer  dataUsingEncoding : NSUTF8StringEncoding ]];  
  72. [postBody  appendData :imageData];  
  73. [postBody  appendData :[bodySuffixString  dataUsingEncoding : NSUTF8StringEncoding  allowLossyConversion : NO ]];  
  74.    
  75.    
  76. NSString  *baseURL = [ NSString   stringWithFormat :@"http://api.t.sina.com.cn/statuses/upload.xml?source=3930264715 " ];  
  77. NSURL  *url = [ NSURL   URLWithString :baseURL];  
  78. NSMutableURLRequest  *mainRequest = [[ NSMutableURLRequest   alloc ]  initWithURL :url  
  79. cachePolicy : NSURLRequestUseProtocolCachePolicy  
  80. timeoutInterval : 30.0f ];  
  81.    
  82. [mainRequest  setHTTPMethod : @"POST" ];  
  83. [mainRequest  setValue :authValue  forHTTPHeaderField : @"Authorization" ];  
  84. NSString  *contentType = [ NSString   stringWithFormat : @"multipart/form-data; boundary=%@" , boundary,  nil ];  
  85. [mainRequest  setValue :contentType  forHTTPHeaderField : @"Content-Type" ];  
  86. [mainRequest  setHTTPBody :postBody];  
  87.    
  88. NSURLResponse  *shareResponse;  
  89. NSError  *error;  
  90.    
  91. NSData  *responseData = [ NSURLConnection   sendSynchronousRequest :mainRequest  returningResponse:&shareResponse  error :&error];  
  92. NSString  *responseString = [[ NSString   alloc ]  initWithData :responseData  encoding :NSUTF8StringEncoding ];  
  93.    
  94. NSHTTPURLResponse  *httpResponse = ( NSHTTPURLResponse  *)shareResponse;  
  95.    
  96. int  statusCode = [httpResponse  statusCode ];  
  97.    
  98. NSLog ( @"status code = %d" ,statusCode);  
  99.    
  100. BOOL  succeed =  NO ;  
  101. if  (statusCode ==  200 ) {  
  102. succeed =  YES ;  
  103. }  
  104. [mainRequest  release ];  
  105.    
  106.    
  107. NSLog ( @"response string : %@" ,responseString);  
  108.    
  109. [responseString  release ];  
  110.    
  111. [ uploadWaiting   stopAnimating ];  
  112.    
  113. NSString  *message =  nil ;  
  114. if  (succeed) {  
  115. message =  @" 分享成功 " ;  
  116. else  {  
  117. message =  @" 分享失败 " ;  
  118. }  
  119. UIAlertView  *alert = [[ UIAlertView   alloc ]  initWithTitle : @""   message :message  
  120. delegate : self  
  121. cancelButtonTitle : @" 确定 "   otherButtonTitles : nil ];  
  122. [alert  show ];  
  123. [alert  release ]; 
原创粉丝点击