日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发之share第三方登录以及分享

發(fā)布時間:2023/12/13 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发之share第三方登录以及分享 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

(1)官方下載ShareSDK?iOS?2.8.8,地址:http://sharesdk.cn/

(2)根據(jù)實際情況,引入相關(guān)的庫,參考官方文檔。

(3)在項目的AppDelegate中一般情況下有三個操作,第一是注冊ShareSDK,第二是注冊各個平臺的賬號,第三是關(guān)于微信等應(yīng)用的回調(diào)處理。

?

[objc]?view plaincopy print?
  • //??
  • //??AppDelegate.m??
  • //??ShareSDKTest??
  • //??
  • //??Created?by?wangdalei?on?14-6-23.??
  • //??Copyright?(c)?2014年?王大雷.?All?rights?reserved.??
  • //??
  • ??
  • #import?"AppDelegate.h"??
  • #import?"RootViewController.h"??
  • #import?<ShareSDK/ShareSDK.h>??
  • #import?"WeiboApi.h"??
  • #import?<TencentOpenAPI/QQApiInterface.h>??
  • #import?<TencentOpenAPI/TencentOAuth.h>??
  • #import?"WXApi.h"??
  • #import?<TencentOpenAPI/QQApiInterface.h>??
  • #import?<TencentOpenAPI/TencentOAuth.h>??
  • ??
  • @implementation?AppDelegate??
  • @synthesize?rootVC;??
  • ??
  • -?(BOOL)application:(UIApplication?*)application?didFinishLaunchingWithOptions:(NSDictionary?*)launchOptions?{??
  • ????self.window?=?[[UIWindow?alloc]?initWithFrame:[[UIScreen?mainScreen]?bounds]];??
  • ????if?(self.rootVC==nil)?{??
  • ????????self.rootVC?=?[[RootViewController?alloc]initWithNibName:@"RootViewController"?bundle:nil];??
  • ????}??
  • ????UINavigationController?*rootNav?=?[[UINavigationController?alloc]initWithRootViewController:self.rootVC];??
  • ????self.window.rootViewController?=?rootNav;??
  • ????self.window.backgroundColor?=?[UIColor?whiteColor];??
  • ????[self.window?makeKeyAndVisible];??
  • ??????
  • ??????
  • ????<span?style="color:#ff0000;">[ShareSDK?registerApp:@"1a2e7ab5fb6c"];</span>??
  • ??????
  • ???<span?style="color:#3366ff;">?//添加新浪微博應(yīng)用?注冊網(wǎng)址?http://open.weibo.com??wdl@pmmq.com?此處需要替換成自己應(yīng)用的??
  • ????[ShareSDK?connectSinaWeiboWithAppKey:@"3201194191"??
  • ???????????????????????????????appSecret:@"0334252914651e8f76bad63337b3b78f"??
  • ?????????????????????????????redirectUri:@"http://appgo.cn"];??
  • ??????
  • ????//添加騰訊微博應(yīng)用?注冊網(wǎng)址?http://dev.t.qq.com?wdl@pmmq.com?此處需要替換成自己應(yīng)用的??
  • ????[ShareSDK?connectTencentWeiboWithAppKey:@"801307650"??
  • ??????????????????????????????????appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"??
  • ????????????????????????????????redirectUri:@"http://www.sharesdk.cn"??
  • ???????????????????????????????????wbApiCls:[WeiboApi?class]];??
  • ??????
  • ????//添加QQ空間應(yīng)用?注冊網(wǎng)址??http://connect.qq.com/intro/login/?wdl@pmmq.com?此處需要替換成自己應(yīng)用的??
  • ????[ShareSDK?connectQZoneWithAppKey:@"100371282"??
  • ???????????????????????????appSecret:@"aed9b0303e3ed1e27bae87c33761161d"??
  • ???????????????????qqApiInterfaceCls:[QQApiInterface?class]??
  • ?????????????????????tencentOAuthCls:[TencentOAuth?class]];??
  • ??????
  • ????//此參數(shù)為申請的微信AppID?wdl@pmmq.com?此處需要替換成自己應(yīng)用的??
  • ????[ShareSDK?connectWeChatWithAppId:@"wx4868b35061f87885"?wechatCls:[WXApi?class]];??
  • ??????
  • ????//添加QQ應(yīng)用?該參數(shù)填入申請的QQ?AppId?wdl@pmmq.com?此處需要替換成自己應(yīng)用的??
  • ????[ShareSDK?connectQQWithQZoneAppKey:@"100371282"??
  • ?????????????????????qqApiInterfaceCls:[QQApiInterface?class]??
  • ???????????????????????tencentOAuthCls:[TencentOAuth?class]];</span>??
  • ??????
  • ????return?YES;??
  • }??
  • ??
  • ??
  • -?(void)applicationWillResignActive:(UIApplication?*)application?{??
  • ????//?Sent?when?the?application?is?about?to?move?from?active?to?inactive?state.?This?can?occur?for?certain?types?of?temporary?interruptions?(such?as?an?incoming?phone?call?or?SMS?message)?or?when?the?user?quits?the?application?and?it?begins?the?transition?to?the?background?state.??
  • ????//?Use?this?method?to?pause?ongoing?tasks,?disable?timers,?and?throttle?down?OpenGL?ES?frame?rates.?Games?should?use?this?method?to?pause?the?game.??
  • }??
  • ??
  • -?(void)applicationDidEnterBackground:(UIApplication?*)application?{??
  • ????//?Use?this?method?to?release?shared?resources,?save?user?data,?invalidate?timers,?and?store?enough?application?state?information?to?restore?your?application?to?its?current?state?in?case?it?is?terminated?later.???
  • ????//?If?your?application?supports?background?execution,?this?method?is?called?instead?of?applicationWillTerminate:?when?the?user?quits.??
  • }??
  • ??
  • -?(void)applicationWillEnterForeground:(UIApplication?*)application?{??
  • ????//?Called?as?part?of?the?transition?from?the?background?to?the?inactive?state;?here?you?can?undo?many?of?the?changes?made?on?entering?the?background.??
  • }??
  • ??
  • -?(void)applicationDidBecomeActive:(UIApplication?*)application?{??
  • ????//?Restart?any?tasks?that?were?paused?(or?not?yet?started)?while?the?application?was?inactive.?If?the?application?was?previously?in?the?background,?optionally?refresh?the?user?interface.??
  • }??
  • ??
  • -?(void)applicationWillTerminate:(UIApplication?*)application?{??
  • ????//?Called?when?the?application?is?about?to?terminate.?Save?data?if?appropriate.?See?also?applicationDidEnterBackground:.??
  • }??
  • ??
  • ??
  • <span?style="color:#ff6600;">#pragma?mark?-?WX回調(diào)??
  • ??
  • -?(BOOL)application:(UIApplication?*)application?handleOpenURL:(NSURL?*)url?{??
  • ????return?[ShareSDK?handleOpenURL:url?wxDelegate:self];??
  • }??
  • ??
  • -?(BOOL)application:(UIApplication?*)application?openURL:(NSURL?*)url?sourceApplication:(NSString?*)sourceApplication?annotation:(id)annotation?{??
  • ????return?[ShareSDK?handleOpenURL:url?sourceApplication:sourceApplication?annotation:annotation?wxDelegate:self];??
  • }??
  • ??
  • #pragma?mark?-?WXApiDelegate??
  • ??
  • /*!?@brief?收到一個來自微信的請求,第三方應(yīng)用程序處理完后調(diào)用sendResp向微信發(fā)送結(jié)果?
  • ?*?
  • ?*?收到一個來自微信的請求,異步處理完成后必須調(diào)用sendResp發(fā)送處理結(jié)果給微信。?
  • ?*?可能收到的請求有GetMessageFromWXReq、ShowMessageFromWXReq等。?
  • ?*?@param?req?具體請求內(nèi)容,是自動釋放的?
  • ?*/??
  • -(void)?onReq:(BaseReq*)req{??
  • ??????
  • }??
  • ??
  • /*!?@brief?發(fā)送一個sendReq后,收到微信的回應(yīng)?
  • ?*?
  • ?*?收到一個來自微信的處理結(jié)果。調(diào)用一次sendReq后會收到onResp。?
  • ?*?可能收到的處理結(jié)果有SendMessageToWXResp、SendAuthResp等。?
  • ?*?@param?resp具體的回應(yīng)內(nèi)容,是自動釋放的?
  • ?*/??
  • -(void)?onResp:(BaseResp*)resp{??
  • ??????
  • }??
  • </span>??
  • @end??
  • ?

    ?

    (4)信息分享。

    ?

    [objc]?view plaincopy print?
  • -(IBAction)share:(id)sender{??
  • ????NSString?*imagePath?=?[[NSBundle?mainBundle]?pathForResource:@"card"??ofType:@"png"];??
  • ????//構(gòu)造分享內(nèi)容??
  • ????id<ISSContent>?publishContent?=?[ShareSDK?content:@"分享內(nèi)容測試"??
  • ???????????????????????????????????????defaultContent:@"默認(rèn)分享內(nèi)容測試,沒內(nèi)容時顯示"??
  • ????????????????????????????????????????????????image:[ShareSDK?imageWithPath:imagePath]??
  • ????????????????????????????????????????????????title:@"pmmq"??
  • ??????????????????????????????????????????????????url:@"http://www.sharesdk.cn"??
  • ??????????????????????????????????????????description:@"這是一條測試信息"??
  • ????????????????????????????????????????????mediaType:SSPublishContentMediaTypeNews];??
  • ????[ShareSDK?showShareActionSheet:nil??
  • ?????????????????????????shareList:nil??
  • ???????????????????????????content:publishContent??
  • ?????????????????????statusBarTips:YES??
  • ???????????????????????authOptions:nil??
  • ??????????????????????shareOptions:?nil??
  • ????????????????????????????result:^(ShareType?type,?SSResponseState?state,?id<ISSPlatformShareInfo>?statusInfo,?id<ICMErrorInfo>?error,?BOOL?end)?{??
  • ????????????????????????????????if?(state?==?SSResponseStateSuccess)??
  • ????????????????????????????????{??
  • ????????????????????????????????????NSLog(@"分享成功");??
  • ????????????????????????????????}??
  • ????????????????????????????????else?if?(state?==?SSResponseStateFail)??
  • ????????????????????????????????{??
  • ????????????????????????????????????NSLog(@"分享失敗");??
  • ????????????????????????????????}??
  • ????????????????????????????}];??
  • }??

  • (5)登錄、登出、獲取授權(quán)信息、關(guān)注制定微博

    ?

    [objc]?view plaincopy print?
  • //??
  • //??LoginViewController.m??
  • //??ShareSDKTest??
  • //??
  • //??Created?by?wangdalei?on?14-6-23.??
  • //??Copyright?(c)?2014年?王大雷.?All?rights?reserved.??
  • //??
  • ??
  • #import?"LoginViewController.h"??
  • #import?<ShareSDK/ShareSDK.h>??
  • ??
  • @interface?LoginViewController?()??
  • ??
  • -(IBAction)loginWithSina:(id)sender;??
  • ??
  • -(IBAction)loginWithQQ:(id)sender;??
  • ??
  • -(IBAction)loginoutWithSina:(id)sender;??
  • ??
  • -(IBAction)loginoutWithQQ:(id)sender;??
  • ??
  • -(IBAction)guanzhuUs:(id)sender;??
  • ??
  • -(void)reloadStateWithType:(ShareType)type;??
  • ??
  • @end??
  • ??
  • @implementation?LoginViewController??
  • ??
  • -?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil?{??
  • ????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];??
  • ????if?(self)?{??
  • ????}??
  • ????return?self;??
  • }??
  • ??
  • -?(void)viewDidLoad?{??
  • ????[super?viewDidLoad];??
  • }??
  • ??
  • -?(void)didReceiveMemoryWarning?{??
  • ????[super?didReceiveMemoryWarning];??
  • }??
  • ??
  • -?(IBAction)loginWithSina:(id)sender?{??
  • ????[ShareSDK?getUserInfoWithType:ShareTypeSinaWeibo?authOptions:nil?result:^(BOOL?result,?id<ISSPlatformUser>?userInfo,?id<ICMErrorInfo>?error)?{??
  • ????????NSLog(@"%d",result);??
  • ????????if?(result)?{??
  • ????????????//成功登錄后,判斷該用戶的ID是否在自己的數(shù)據(jù)庫中。??
  • ????????????//如果有直接登錄,沒有就將該用戶的ID和相關(guān)資料在數(shù)據(jù)庫中創(chuàng)建新用戶。??
  • ????????????[self?reloadStateWithType:ShareTypeSinaWeibo];??
  • ????????}??
  • ????}];??
  • }??
  • ??
  • ??
  • -(IBAction)loginWithQQ:(id)sender{??
  • ????[ShareSDK?getUserInfoWithType:ShareTypeQQSpace?authOptions:nil?result:^(BOOL?result,?id<ISSPlatformUser>?userInfo,?id<ICMErrorInfo>?error)?{??
  • ????????NSLog(@"%d",result);??
  • ????????if?(result)?{??
  • ????????????//成功登錄后,判斷該用戶的ID是否在自己的數(shù)據(jù)庫中。??
  • ????????????//如果有直接登錄,沒有就將該用戶的ID和相關(guān)資料在數(shù)據(jù)庫中創(chuàng)建新用戶。??
  • ????????????[self?reloadStateWithType:ShareTypeQQSpace];??
  • ????????}??
  • ????}];??
  • }??
  • ??
  • -(IBAction)loginoutWithSina:(id)sender{??
  • ????[ShareSDK?cancelAuthWithType:ShareTypeSinaWeibo];??
  • ????[self?reloadStateWithType:ShareTypeSinaWeibo];??
  • }??
  • ??
  • -(IBAction)loginoutWithQQ:(id)sender{??
  • ????[ShareSDK?cancelAuthWithType:ShareTypeQQSpace];??
  • ????[self?reloadStateWithType:ShareTypeQQSpace];??
  • }??
  • ??
  • -(void)reloadStateWithType:(ShareType)type{??
  • ????//現(xiàn)實授權(quán)信息,包括授權(quán)ID、授權(quán)有效期等。??
  • ????//此處可以在用戶進(jìn)入應(yīng)用的時候直接調(diào)用,如授權(quán)信息不為空且不過期可幫用戶自動實現(xiàn)登錄。??
  • ????id<ISSPlatformCredential>?credential?=?[ShareSDK?getCredentialWithType:type];??
  • ????UIAlertView?*alertView?=?[[UIAlertView?alloc]?initWithTitle:NSLocalizedString(@"TEXT_TIPS",?@"提示")??
  • ????????????????????????????????????????????????????????message:[NSString?stringWithFormat:??
  • ?????????????????????????????????????????????????????????????????@"uid?=?%@\ntoken?=?%@\nsecret?=?%@\n?expired?=?%@\nextInfo?=?%@",??
  • ?????????????????????????????????????????????????????????????????[credential?uid],??
  • ?????????????????????????????????????????????????????????????????[credential?token],??
  • ?????????????????????????????????????????????????????????????????[credential?secret],??
  • ?????????????????????????????????????????????????????????????????[credential?expired],??
  • ?????????????????????????????????????????????????????????????????[credential?extInfo]]??
  • ???????????????????????????????????????????????????????delegate:nil??
  • ??????????????????????????????????????????????cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW",?@"知道了")??
  • ??????????????????????????????????????????????otherButtonTitles:nil];??
  • ????[alertView?show];??
  • }??
  • ??
  • //關(guān)注用戶??
  • -(IBAction)guanzhuUs:(id)sender{??
  • ????[ShareSDK?followUserWithType:ShareTypeSinaWeibo?????????//平臺類型??
  • ???????????????????????????field:@"ShareSDK"????????????????//關(guān)注用戶的名稱或ID??
  • ???????????????????????fieldType:SSUserFieldTypeName????????//字段類型,用于指定第二個參數(shù)是名稱還是ID??
  • ?????????????????????authOptions:nil????????????????????????//授權(quán)選項??
  • ????????????????????viewDelegate:nil????????????????????????//授權(quán)視圖委托??
  • ??????????????????????????result:^(SSResponseState?state,?id<ISSPlatformUser>?userInfo,?id<ICMErrorInfo>?error)?{??
  • ??????????????????????????????if?(state?==?SSResponseStateSuccess)?{??
  • ??????????????????????????????????NSLog(@"關(guān)注成功");??
  • ??????????????????????????????}?else?if?(state?==?SSResponseStateFail)?{??
  • ??????????????????????????????????NSLog(@"%@",?[NSString?stringWithFormat:@"關(guān)注失敗:%@",?error.errorDescription]);??
  • ??????????????????????????????}??
  • ??????????????????????????}];??
  • }??
  • ??
  • ??
  • @end ?
  • ?

    ?

    ?

    ?

    ?

    DEMO下載地址:http://download.csdn.net/download/daleiwang/7734321

    轉(zhuǎn)載于:https://www.cnblogs.com/lys-iOS-study/p/5693924.html

    總結(jié)

    以上是生活随笔為你收集整理的iOS开发之share第三方登录以及分享的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。