iOS新浪微博、腾讯微博分享功能实例

2020-01-18 18:00:19于丽

腾讯的实现方式和新浪的不一样。下面是腾讯的调用方式


// MyViewController.h
#import "WeiboApi.h"
@interface MyViewController :UIViewController<WeiboAuthDelegate,WeiboRequestDelegate> {
}
@property(nonatomic,retain)WeiboApi   *qqwb;
@end
// MyViewController.m
#import "AppDelegate.h"
- (void)viewDidLoad
{
 [superviewDidLoad];
 AppDelegate *delegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;
 if (nil != delegate.qqwbAppDelete) {
  self.qqwb = delegate.qqwbAppDelete;
 }
 else {
  self.qqwb = [[WeiboApialloc]initWithAppKey:qqAppKeyandSecret:qqAppSecretandRedirectUri:qqAppURL];
  delegate.qqwbAppDelete = _qqwb;
 }
}
- (void)buttonAction:(id)sender {
 UIButton *button = (UIButton *)sender;
 if (0 == button.tag) {
  //新浪
  [selfsendAuthWithType:0];
 }
 else {
  //腾讯
  [selfsendAuthWithType:1];
 }
}
- (void)sendAuthWithType:(NSInteger)aIndex {
 if (0 == aIndex) {
  if ([self isAuthValid]) {
   //显示内容界面
  }
  else {
   [[NSNotificationCenterdefaultCenter] postNotificationName:@"Auth"object:nil];
  }
 }
 else {
  if ([self isAuthValid]) {
   //显示内容界面
  }
  else {
   [_qqwb loginWithDelegate:selfandRootController:self];
  }
 }
}

- (void)sendDataWithPic:(NSString *)sText ImageData:(NSData *)aImageData {
 NSMutableDictionary* parameters = [NSMutableDictionarydictionary];
 [parameters setObject:@"xml"forKey:@"format"];
 [parameters setObject:sText forKey:@"content"];
 [parameters setObject:aImageData forKey:@"pic"];
 [_qqwb requestWithParams:parameters
      apiName:[NSStringstringWithFormat:@"%@",@"t/add_pic"]
     httpMethod:@"POST"delegate:self];
}

- (void)sendText:(NSString *)sText {
 NSMutableDictionary* parameters = [NSMutableDictionarydictionary];
 [parameters setObject:@"xml"forKey:@"format"];
 [parameters setObject:sText forKey:@"content"];
 [_qqwb requestWithParams:parameters
      apiName:[NSStringstringWithFormat:@"%@",@"t/add"]
     httpMethod:@"POST"delegate:self];
}

#pragma Mark WeiboAuthDelegate
- (void)DidAuthFinished:(WeiboApi *)wbapi {
 //授权成功,后显示内容界面
}

#pragma Mark WeiboRequestDelegate
- (void)didReceiveRawData:(NSData *)data reqNo:(int)reqno {
 //发布成功
}
- (void)didFailWithError:(NSError *)error reqNo:(int)reqno {
 //发布失败
}
//以下是处理sina的授权验证函数,qq的未写。
- (void)removeAuthData
{
 self.sinaid = nil;
 self.sinatoken =nil;
 self.sinadate =nil;
}
- (BOOL)isLoggedIn
{
 returnsinaid && sinatoken && sinadate;
}
- (BOOL)isAuthorizeExpired
{
 NSDate *now = [NSDatedate];
 /*if (0 == bSina) {*/
  return ([nowcompare:sinadate] ==NSOrderedDescending);
 //}
 /*else {
  AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
  return [delegate.qqwbAppDelete isAuthorizeExpired];
 }*/
}
- (BOOL)isAuthValid
{
 return ([selfisLoggedIn] && ![selfisAuthorizeExpired]);
}