完整的iOS新浪微博分享功能開發(fā)
本文實例為大家分享了iOS新浪微博分享功能的具體代碼,供大家參考,具體內容如下
做新浪分享 需先去http://open.weibo.com/apps注冊開發(fā)者app 很簡單!
第1步
第2步
3
設置你的應用的信息
找到自己的appkey
還需要設置自己的kAppRedirectURL測試可以隨便寫個!
開發(fā)部分在下面ios新浪微博分享(2)這部分:
開發(fā)需要下載官方的sdkhttp://open.weibo.com/wiki/SDK#iOS_SDK
本人下載的版本
新建一個viewcontrroler==WeiBoViewController
效果圖
h文件
#import #import "SinaWeb/SinaWeibo/SinaWeibo.h" #import "SinaWeb/SinaWeibo/SinaWeiboRequest.h" @interface WeiBoViewController : UIViewController<</span>SinaWeiboDelegate,SinaWeiboRequestDelegate> { UIButton *_shareButton; UITextView *_textView; UIView *_shareView; UIActivityIndicatorView *_indicator;} @property (strong, nonatomic) UIButton *shareButton; @property (strong, nonatomic) UITextView *textView; @property (strong, nonatomic) UIView *shareView; @property (strong, nonatomic) UIActivityIndicatorView *indicator; @property (readonly, nonatomic) SinaWeibo *sinaWeibo; - (void) addButton; - (void) addShareView; - (void) share:(UIButton*) sender; - (void) removeShare:(UIButton*) sender; - (void) sendShare:(UIButton*) sender; - (void) exitShare:(UIButton*) sender; @end
m文件
#import "WeiBoViewController.h" #define kAppKey @"appkey" #define kAppSecret @"appSecret" #define kAppRedirectURL @"重定向url" @interface WeiBoViewController () @end @implementation WeiBoViewController @synthesize shareButton = _shareButton; @synthesize textView = _textView; @synthesize shareView = _shareView; @synthesize indicator = _indicator; @synthesize sinaWeibo = _sinaWeibo; - (SinaWeibo*)sinaWeibo { _sinaWeibo.delegate=self; return _sinaWeibo; } - (void)viewDidLoad { [super viewDidLoad]; _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [_indicator setFrame:CGRectMake(0, 0, 50, 50)]; _indicator.center = self.view.center; [self.view addSubview:_indicator]; _sinaWeibo = [[SinaWeibo alloc] initWithAppKey:kAppKey appSecret:kAppSecret appRedirectURI:kAppRedirectURL andDelegate:self]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *sinaWeiboInfo = [defaults objectForKey:@"SinaWeiboAuthData"]; if ([sinaWeiboInfo objectForKey:@"AccessTokenKey"] && [sinaWeiboInfo objectForKey:@"ExpirationDateKey"] && [sinaWeiboInfo objectForKey:@"UserIDKey"]) { _sinaWeibo.accessToken = [sinaWeiboInfo objectForKey:@"AccessTokenKey"]; _sinaWeibo.expirationDate = [sinaWeiboInfo objectForKey:@"ExpirationDateKey"]; _sinaWeibo.userID = [sinaWeiboInfo objectForKey:@"UserIDKey"]; } [self addButton]; } - (void) addButton { _shareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; UIImage*butimg=[UIImage imageNamed:@"button_background@2x.png"]; UIImage*logobutimg=[UIImage imageNamed:@"logo@2x.png"]; [self.shareButton setFrame:CGRectMake(10, 10, butimg.size.width, butimg.size.height)]; [self.shareButton setBackgroundImage:butimg forState:UIControlStateNormal]; [self.shareButton setImage:logobutimg forState:UIControlStateNormal]; [self.shareButton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.shareButton]; } //分享按鈕響應方法 - (void) share:(UIButton*) sender { SinaWeibo *sinaWeibo = [self sinaWeibo]; BOOL authValid = sinaWeibo.isAuthValid; if (!authValid) { [sinaWeibo logIn]; } else { NSString *postStatusText = @"[哈哈]"; SinaWeibo *sinaWeibo = [self sinaWeibo]; //只發(fā)送漢字 // [sinaWeibo requestWithURL:@"statuses/update.json" params:[NSMutableDictionary dictionaryWithObjectsAndKeys:postStatusText,@"status", nil] httpMethod:@"POST" delegate:self]; //圖片和連接 和文字 [sinaWeibo requestWithURL:@"statuses/upload.json" params:[NSMutableDictionary dictionaryWithObjectsAndKeys: @"要發(fā)布的微博文本內容,超鏈接http://baidu.com", @"status", [UIImage imageNamed:@"Icon.png"], @"pic", nil] httpMethod:@"POST" delegate:self]; [_shareView removeFromSuperview]; [self.indicator startAnimating]; } } //登陸成功后回調方法 - (void) sinaweiboDidLogIn:(SinaWeibo *)sinaweibo { NSLog(@"%@--%@--%@--%@",sinaweibo.accessToken,sinaweibo.expirationDate, sinaweibo.userID,sinaweibo.refreshToken); NSDictionary *authData = [NSDictionary dictionaryWithObjectsAndKeys: sinaweibo.accessToken, @"AccessTokenKey", sinaweibo.expirationDate, @"ExpirationDateKey", sinaweibo.userID, @"UserIDKey", sinaweibo.refreshToken, @"refresh_token", nil]; [[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"SinaWeiboAuthData"]; [[NSUserDefaults standardUserDefaults] synchronize]; //可以在此選在授權成功后直接發(fā)送 } //取消按鈕回調方法 - (void) removeShare:(UIButton*) sender { [_shareView removeFromSuperview]; } //發(fā)送按鈕回調方法 - (void) sendShare:(UIButton*) sender { NSString *postStatusText = self.textView.text; SinaWeibo *sinaWeibo = [self sinaWeibo]; [sinaWeibo requestWithURL:@"statuses/updates.json" params:[NSMutableDictionary dictionaryWithObjectsAndKeys:postStatusText,@"status", nil] httpMethod:@"POST" delegate:self]; [_shareView removeFromSuperview]; [self.indicator startAnimating]; } //退出登陸回調方法 - (void) exitShare:(UIButton*) sender { SinaWeibo *sinaWeibo = [self sinaWeibo]; [sinaWeibo logOut]; [_shareView removeFromSuperview]; NSLog(@"退出登陸"); } //請求完成回調該方法 - (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result { [self.indicator stopAnimating]; UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"發(fā)送成功" message:@"提示" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil]; [alert show]; [alert release]; NSLog(@"發(fā)送成功"); } //請求失敗回調該方法 - (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error { [self.indicator stopAnimating]; UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"發(fā)送失敗,請檢測網(wǎng)絡鏈接" message:@"提示" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil]; [alert show]; [alert release]; NSLog(@"發(fā)送失敗"); } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } @end
源碼下載:http://xiazai.jb51.net/201611/yuanma/SinaWeiboShare(jb51.net).rar
個人信息獲得
@interface UserInfoViewController : UIViewController<SinaWeiboRequestDelegate>//實現(xiàn)請求代理 BOOL authValid = self.sina.isAuthValid;//判斷是否授權了 if (authValid){ [self.sina requestWithURL:@"users/show.json" params:[NSMutableDictionarydictionaryWithObject:self.sina.userID forKey:@"uid"] httpMethod:@"GET" delegate:self]; } //請求失敗回調方法 - (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error{ if ([request.url hasSuffix:@"users/show.json"]){ [self.userInfoDic release], self.userInfoDic = nil; } } //請求成功回調方法 - (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result{ if ([request.url hasSuffix:@"users/show.json"]){ [self.userInfoDic release]; self.userInfoDic = [result retain]; //NSLog(@"用戶信息字典:%@", self.userInfoDic); 字典數(shù)據(jù) 返回字段下面 } }
返回字段說明
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Swift 2.1 為 UIView 添加點擊事件和點擊效果
本文主要介紹 Swift UIView,這里給大家提供代碼示例作為參考為UIView 添加點擊事件和點擊效果,希望能幫助IOS開發(fā)的同學2016-07-07使用UItableview在iOS應用開發(fā)中實現(xiàn)好友列表功能
這篇文章主要介紹了使用UItableview在iOS應用開發(fā)中實現(xiàn)一個好友列表功能的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12關于ios配置微信config出現(xiàn)驗簽失敗的問題解決
這篇文章主要介紹了關于ios配置微信config出現(xiàn)驗簽失敗的問題解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04iOS開發(fā)中使用UIScrollView實現(xiàn)無限循環(huán)的圖片瀏覽器
這篇文章主要介紹了iOS開發(fā)中使用UIScrollView實現(xiàn)無限循環(huán)的圖片瀏覽器的方法,感興趣的小伙伴們可以參考一下2016-03-03android中UIColletionView瀑布流布局實現(xiàn)思路以及封裝的實現(xiàn)
本篇文章主要介紹了android中UIColletionView瀑布流布局實現(xiàn)思路以及封裝的實現(xiàn),具有一定的參考價值,有興趣的可以了解一下。2017-02-02替代pod update速度慢的lg_pod_plugin安裝使用詳解
這篇文章主要介紹了替代pod update速度慢lg_pod_plugin安裝使用方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09