1、登錄騰訊開放平台,創建一個應用,獲取APP_KEY和APP_ID
2、下載對應的SDK
3、下載 IOS_API%E8%B0%83%E7%94%A8%E8%AF%B4%E6%98%8E">iOS SDK環境搭建
1、下載SDK到本地後,將SDK拖入工程中
2、添加所需的依賴庫
Security.framework SystemConfiguration.framework CoreGraphics.Framework CoreTelephony.framework libiconv.tdb libsqlite3.tdb libstdc++.tdb libz.tdb
3、在工程配置中的“Build Settings”一欄中找到“Linking”配置區,給“Other Linker Flags”配置項添加屬性值“-fobjc-arc”
1、添加URL SChemes,新的scheme = tencent + appid。
2、在plist文件中,設置ATS
3、在plist文件中,添加白名單
LSApplicationQueriesSchemes mqqapi mqq mqqOpensdkSSoLogin mqqconnect mqqopensdkdataline mqqopensdkgrouptribeshare mqqopensdkfriend mqqopensdkapi mqqopensdkapiV2 mqqopensdkapiV3 mqzoneopensdk wtloginmqq wtloginmqq2 mqqwpa mqzone mqzonev2 mqzoneshare wtloginqzone mqzonewx mqzoneopensdkapiV2 mqzoneopensdkapi19 mqzoneopensdkapi mqqbrowser mttbrowser
添加之後如下圖:
2、在AppDelegate.m 中添加代碼:
#import "AppDelegate.h" #import#import @interface AppDelegate () @property (nonatomic, strong) TencentOAuth *tencentOAuth; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //初始化SDK _tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"222222" andDelegate:self]; //設置應用需要用戶授權的API列表 NSArray *_permissions = [NSArray arrayWithObjects:@"get_user_info", @"get_simple_userinfo", @"add_t", nil]; //調用SDK登錄 [_tencentOAuth authorize:_permissions inSafari:NO]; [_tencentOAuth accessToken] ; [_tencentOAuth openId] ; return YES; } //登錄成功 - (void)tencentDidLogin { NSLog(@"登錄完成"); if (_tencentOAuth.accessToken && 0 != [_tencentOAuth.accessToken length]) { // 記錄登錄用戶的OpenID、Token以及過期時間 NSLog(@"%@",_tencentOAuth.accessToken); } else { NSLog(@"登錄不成功 沒有獲取accesstoken"); } } //非網絡錯誤導致登錄失敗 -(void)tencentDidNotLogin:(BOOL)cancelled { if (cancelled) { NSLog(@"用戶取消登錄"); } else { NSLog(@"登錄失敗"); } } //網絡錯誤導致登錄失敗 -(void)tencentDidNotNetWork { NSLog(@"無網絡連接,請設置網絡"); } // 處理來至QQ的請求 - (void)onReq:(QQBaseReq *)req{ NSLog(@" ----req %@",req); } //處理來至QQ的響應 - (void)onResp:(QQBaseResp *)resp{ NSLog(@" ----resp %@",resp); } //處理QQ在線狀態的回調 - (void)isOnlineResponse:(NSDictionary *)response{ } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ return [TencentOAuth HandleOpenURL:url];} - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [TencentOAuth HandleOpenURL:url]; }
效果圖:
我在集成中遇到了一個配置錯誤的問題,如下圖:
解決辦法:我在QQ開放平台上注冊了一個應用,獲得了APPID,但是我並沒有提交審核,有可能是沒有審核的APPID不可用(推測而已),你可以換一個騰訊提供的APPID:222222。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPjxpbWcgYWx0PQ=="這裡寫圖片描述" src="https://www.ios5.online/ios/UploadFiles_8070/201703/2017030817283185.png" title="\" />
//初始化SDK _tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"222222" andDelegate:self];
DEMO下載
就是這兩個地方,設置對了,就可以了。
1、登錄微信開放平台,創建一個應用,獲取APP_KEY和APP_ID
2、下載對應的SDK
1、將下載好的SDK拖入工程
2、添加依賴庫
SystemConfiguration.framework libz.tdb libsqlite3.0.tdb libc++.tdb Security.framework CoreTelephony.framework CFNetwork.framework
如果某些依賴庫已經有了,就不需要重復添加了。
3、在你的工程文件中選擇Build Setting,在”Other Linker Flags”中加入”-Objc -all_load”
4、在Xcode中,選擇你的工程設置項,選中“TARGETS”一欄,在“info”標簽欄的“URL type“添加“URL scheme”為你所注冊的應用程序id
1、在AppDelegate.m 中導入頭文件
#import "WXApi.h"
2、添加協議:
WXApiDelegate
3、注冊微信SDK:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [WXApi registerApp:@"wxd477edab60670232"]; return YES; }
4、頁面跳轉處理
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ if([[url scheme] hasPrefix:@"tencent"]) { return [TencentOAuth HandleOpenURL:url]; }else if([[url scheme] hasPrefix:@"wx"]){ return [WXApi handleOpenURL:url delegate:self]; } return YES; } - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ if([[url scheme] hasPrefix:@"tencent"]) { return [TencentOAuth HandleOpenURL:url]; } else if([[url scheme] hasPrefix:@"wx"]) { return [WXApi handleOpenURL:url delegate:self]; } return YES; }
如果項目中集成了多種第三方登錄的,需要進行判斷。
5、發送請求微信登錄
在你需要調用微信登錄的按鈕方法中添加如下代碼:
SendAuthReq* req = [[SendAuthReq alloc] init]; req.scope = @"snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact"; // @"post_timeline,sns" req.state = @"xxx"; [WXApi sendAuthReq:req viewController:self delegate:nil];
效果圖
Demo下載
1、登錄新浪微博開放平台,創建一個應用,獲取APPID和APPKEY.
2、下載對應的SDK
1、下載好SDK後,將SDK拖入工程
2、在工程中引入靜態庫之後,需要在編譯時添加-ObjC,Target->Build Settings->Linking 下 Other Linker Flags 添加 -ObjC。(如果添加過了,就不需要重復添加了)
3、添加依賴庫 (如果某些依賴庫添加過了,就不需要重復添加了)
QuartzCore.framework ImageIO.framework SystemConfiguration.framework Security.framework CoreTelephony.framework CoreText.framework UIKit.framework Foundation.framework CoreGraphics.framework libz.tdb libsqlite3.tdb
4、添加URL Schemes
在AppDelegate.m 在添加頭文件
#import "WeiboSDK.h"
添加協議
WeiboSDKDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [WeiboSDK registerApp:@"2045436852"]; return YES; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ if([[url scheme] hasPrefix:@"tencent"]) { return [TencentOAuth HandleOpenURL:url]; }else if([[url scheme] hasPrefix:@"wx"]){ return [WXApi handleOpenURL:url delegate:self]; } else { return [WeiboSDK handleOpenURL:url delegate:self]; } } - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ if([[url scheme] hasPrefix:@"tencent"]) { return [TencentOAuth HandleOpenURL:url]; } else if([[url scheme] hasPrefix:@"wx"]) { return [WXApi handleOpenURL:url delegate:self]; } else { return [WeiboSDK handleOpenURL:url delegate:self]; } return YES; }
在你需要調用新浪微博登錄的按鈕方法中添加代碼:
WBMessageObject *message = [WBMessageObject message]; message.text = @"測試使用"; WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message]; [WeiboSDK sendRequest:request];
新浪微博登錄需要用真機測試。