- 現(xiàn)在這個功能大部分人都是用UM和share去做的,今天我們以來回味下原味道
新浪登錄
- 新浪API網(wǎng)址大全:
http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2
,這里我們知道獲取完參數(shù)后各個信息的請求接口 - 新浪SDK下載地址:
http://open.weibo.com/wiki/SDK
- 集成的注意點:
- 1、首先肯定是去新浪開發(fā)者平臺去創(chuàng)建應(yīng)用,申請appKey,這一段這里不再啰嗦,大概他們會審一天的樣子,所以建議大家提前做這事
- 2、審核通過,拿到key以后,去下載SDK包,把SDK包中的
libWeiboSDK
文件夾添加到工程中去。 - 3、工程中我們需要配置三處地方:第一處添加URLScheme:是wb+你微博應(yīng)用的appkey(比如
wb2045436852
)。第二處:添加系統(tǒng)動態(tài)包,QuartzCore.framework
,ImageIO.framework
,SystemConfiguration.framework
,Security.framework
,CoreTelephony.framework
,CoreText.framework
,CoreGraphics.framework
,libz.tbd
,libsqlite3.tbd
。 第三處:在系統(tǒng)的infoplist文件中添加白名單,LSApplicationQueriesSchemes
中添加名單weibosdk2.5
,weibosdk
,sinaweibohd
,sinaweibo
。(如果你的服務(wù)器還沒有ssl證書,你還需要設(shè)置下網(wǎng)絡(luò)App Transport Security Settings
這里的tran..為YES) - 4、OK,環(huán)境配置完成。接下來是代碼了:appdelegate中,要做的事大概是:向新浪開發(fā)者注冊,判斷應(yīng)用是否是被新浪的回調(diào)打開,實現(xiàn)代理,查新浪API,獲取用戶新浪信息
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WeiboSDK enableDebugMode:YES];
[WeiboSDK registerApp:@"1292878617"];
return YES;
}
// 方法1
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
if ([url.host isEqualToString:@"response"]) {
[WeiboSDK handleOpenURL:url delegate:self];
}
return YES;
}
// 方法1有時候不走,可以用以前的方法,也就是方法2
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
在appdelegate中遵守微博代理(當(dāng)然,實際開發(fā)中這個代理肯定給某個工具類),并實現(xiàn)代理方法
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([response isKindOfClass:WBAuthorizeResponse.class])
{
// 登錄的回調(diào)這里
[self getWeiBoUserInfo:response.userInfo]; // 查微博API,請求用戶的微信信息
}
}
- (void)getWeiBoUserInfo:(NSDictionary *)userDic
{
NSString *access_token = userDic[@"access_token"];
NSNumber *uid = userDic[@"uid"];
NSString *paramter = [NSString stringWithFormat:@"access_token=%@&uid=%@", access_token, uid];
NSString *baseUrl = @"https://api.weibo.com/2/users/show.json";
NSString *urlStr = [baseUrl stringByAppendingFormat:@"?%@", paramter];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"1= %@", dic);
}
}];
[dataTask resume];
}
- 5、然后就完全90%了,剩下的只要寫個btn調(diào)用下方法就可以了
WBAuthorizeRequest *request = [WBAuthorizeRequest request];
request.redirectURI = kRedirectURI;
request.scope = @"all";
request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
@"Other_Info_1": [NSNumber numberWithInt:123],
@"Other_Info_2": @[@"obj1", @"obj2"],
@"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
[WeiboSDK sendRequest:request];
- 6、gameOver
qq登錄
SDK下載地址:http://wiki.connect.qq.com/sdk%E4%B8%8B%E8%BD%BD#SDKfor.E7.A7.BB.E5.8A.A8.E5.BA.94.E7.94.A8.E6.8E.A5.E5.85.A5
- 將iOS SDK中的
TencentOpenAPI.framework
和TencentOpenApi_IOS_Bundle.bundle
文件拷貝到應(yīng)用開發(fā)的目錄下 - 添加SDK依賴的系統(tǒng)庫文件。分別是”Security.framework”, “l(fā)ibiconv.dylib”,“SystemConfiguration.framework”,“CoreGraphics.Framework”、“l(fā)ibsqlite3.dylib”、“CoreTelephony.framework”、“l(fā)ibstdc++.dylib”、“l(fā)ibz.dylib”
- 需要注意的地方:
info
中添加URLTypes格式是:比如tencent1105042374。tencent+appid - 設(shè)置網(wǎng)絡(luò)請求,兼容http
- 添加各種白名單
<string>mqq</string>
<key>New item - 2</key>
<string>mqqapi</string>
<key>New item - 3</key>
<string>mqqwpa</string>
<key>New item - 4</key>
<string>mqqbrowser</string>
<key>New item - 5</key>
<string>mttbrowser</string>
<key>New item - 6</key>
<string>mqqOpensdkSSoLogin</string>
<key>New item - 7</key>
<string>mqqopensdkapiV2</string>
<key>New item - 8</key>
<string>mqqopensdkapiV3</string>
<key>New item - 9</key>
<string>wtloginmqq2</string>
<key>New item - 10</key>
<string>mqzone</string>
<key>New item - 11</key>
<string>mqzoneopensdk</string>
<key>New item - 12</key>
<string>mqzoneopensdkapi</string>
<key>New item - 13</key>
<string>mqzoneopensdkapi19</string>
<key>New item - 14</key>
<string>mqzoneopensdkapiV2</string>
<key>New item - 15</key>
<string>mqqapiwallet</string>
<key>New item - 16</key>
<string>mqqopensdkfriend</string>
<key>New item - 21</key>
<string>tencentapi.qzone.reqContent</string>
<key>New item - 17</key>
<string>mqqopensdkdataline</string>
<key>New item - 18</key>
<string>mqqgamebindinggroup</string>
<key>New item - 19</key>
<string>mqqopensdkgrouptribeshare</string>
<key>New item - 20</key>
<string>tencentapi.qq.reqContent</string>
- appdelegate中,
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
if ([url.host isEqualToString:@"qzapp"]) {
// 從qq跳回來的
[TencentOAuth HandleOpenURL:url];
}
return YES;
}
- VC中跳轉(zhuǎn)事件
import <TencentOpenAPI/TencentOAuth.h>
define kTencentAppId @"1105042374"
@interface ViewController ()<TencentSessionDelegate>
@property (nonatomic, strong) TencentOAuth *tencentOAuth;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kTencentAppId andDelegate:self];
}
- (IBAction)log:(id)sender {
NSArray* permissions = [NSArray arrayWithObjects:
kOPEN_PERMISSION_GET_USER_INFO,
kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
kOPEN_PERMISSION_ADD_SHARE,
nil];
[self.tencentOAuth authorize:permissions inSafari:NO];
}
pragma mark - 代理
- (void)tencentDidLogin
{
NSLog(@"登陸成功");
// 獲取用戶信息
[self.tencentOAuth getUserInfo];
}
- (void)tencentDidNotLogin:(BOOL)cancelled
{
NSLog(@"登錄失敗");
}
- (void)tencentDidNotNetWork
{
NSLog(@"網(wǎng)絡(luò)問題");
}
- (void)getUserInfoResponse:(APIResponse *)response
{
if (response.retCode == URLREQUEST_SUCCEED && kOpenSDKErrorSuccess == response.detailRetCode) {
NSMutableString *str = [NSMutableString stringWithFormat:@""];
for (id key in response.jsonResponse) {
[str appendString: [NSString stringWithFormat:
@"%@:%@\n", key, [response.jsonResponse objectForKey:key]]];
}
NSLog(@"%@", str);
}else{
NSString *errMsg = [NSString stringWithFormat:@"errorMsg:%@\n%@",
response.errorMsg, [response.jsonResponse objectForKey:@"msg"]];
NSLog(@"%@", errMsg);
}
}
- ok
微信登錄
- 1、大概流程是:先去注冊應(yīng)用,然后交錢,申請開通微信登錄功能
- 2、審核通過,拿到微信應(yīng)用的AppKey和AppScroet
- 3、系統(tǒng)環(huán)境配置,基本上只要再在上面的基礎(chǔ)上加一個Scheme(wxf707ade9f9ff3ebb 這個和新浪挺想象的)就可以了
- 4、在
appdelegate
中注冊微信App,遵守微信代理,并且實現(xiàn)代理方法 - 5、AppDelegate中關(guān)鍵代碼如下
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注冊微信
[WXApi registerApp:kWeiChatAppId withDescription:@"微信"];
return YES;
}
// 接受微信回調(diào)方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
if([url.host isEqualToString:@"oauth"]){
// 從微信跳回來的
[WXApi handleOpenURL:url delegate:self];
}
return YES;
}
// 微信代理方法
-(void)onResp:(BaseResp*)resp{
// SendAuthResp是微信登錄
if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *authResp = (SendAuthResp *)resp;
// 調(diào)用工具類,獲取用戶的基本數(shù)據(jù)
[[GetUserInfo sharedInstance] getUserWXInfoByCode:authResp.code];
}
}
- 5、寫個button調(diào)用微信登錄
static NSString *kAuthScope = @"snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact";
static NSString *kAuthState = @"xxx";
- (IBAction)login:(id)sender {
// 調(diào)用微信登錄
SendAuthReq *req = [[SendAuthReq alloc] init];
req.scope = kAuthScope;
req.state = @"123";
[WXApi sendReq:req];
}
- 6、至于獲取用戶的微信信息,這個和新浪一模一樣,都是在代理方法中獲取到access_token和一個類似uid一樣的,然后去找API接口
[微信API接口][1]
[1]:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317853&token=&lang=zh_CN