友盟集成--第三方分享、登录

集成设置:

1.导入SDK

common、share下文件加入项目:
UMCommon.framework
UMShare.framework
UMSocialUI文件夹
SocialLibraries文件夹

2.添加项目配置

Other Linker Flags加入-ObjC

3.依赖库:

UMCommon:
SystemConfiguration.framework//判断网络状态
CoreTelephony.framework//获取运营商标识
libz.tbd//数据压缩
libsqlite3.tbd//数据缓存

UMShare:
CoreGraphics.framework
libsqlite3.tbd
新浪:Photos.framework

4.第三方平台配置

4.1 配置SSO白名单 info.plist中加入LSApplicationQueriesSchemes
4.2 配置URL Scheme

初始化设置及功能实现

1.第三方分享
#import 
#import 
#import 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    /* 打开调试日志 */
    [[UMSocialManager defaultManager] openLog:YES];
    /* 设置友盟appkey */
    [UMConfigure initWithAppkey:@"USHARE_APPKEY" channel:@"App Store"];

    [self configUSharePlatforms];
    [self confitUShareSettings];
    [UMSocialGlobal shareInstance].isClearCacheWhenGetUserInfo = YES;

    return YES;
}

- (void)confitUShareSettings
{
    /*
     * 打开图片水印
     */
    //[UMSocialGlobal shareInstance].isUsingWaterMark = YES;
    
    /*
     * 关闭强制验证https,可允许http图片分享,但需要在info.plist设置安全域名
     NSAppTransportSecurity
     
     NSAllowsArbitraryLoads
     
     
     */
    [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
    
}

- (void)configUSharePlatforms
{
    /* 设置微信的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey: WECHAT_APPKEY appSecret:WECHAT_APPSECRET redirectURL:YOUR_URL];
    
    /*
     * 移除相应平台的分享,如微信收藏
     */
    //    [[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
    
    /* 设置分享到QQ互联的appID
     * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQ_APPKEY/*设置QQ平台的appID*/  appSecret:QQ_APPSECRET redirectURL:YOUR_URL];
    
    /* 设置新浪的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:SINA_APPKEY  appSecret:SINA_APPSECRET redirectURL:YOUR_URL];
    
    /* 设置的自定义按钮 */
    [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_UserDefine_Qrcode withPlatformIcon:[UIImage imageNamed:@"umsocial_qrcode"] withPlatformName:@"二维码"];
}

PS:在UMSocialPlatformConfig.h
//用户自定义的平台
UMSocialPlatformType_UserDefine_Begin = 1000,
UMSocialPlatformType_UserDefine_Qrcode = 1001,//自定义的平台写在1000与2000之间,且值设置为1000~2000之间
UMSocialPlatformType_UserDefine_End = 2000,

// 支持所有iOS系统(包括8.0) swift4.1(Xcode9.3)已废弃,OC项目不影响。新浪 平台外的其他平台可在swift项目中使用下面两种回调方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}

// 仅支持iOS9以上系统,iOS8及以下系统不会回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}

// 支持目前所有iOS系统
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}

在需要实现分享的页面:

#import 
//显示分享面板
- (void)showSharePlatform:(NSDictionary *)data {
    
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        // 根据获取的platformType确定所选平台进行下一步操作
        if (platformType == UMSocialPlatformType_UserDefine_Qrcore) {
            // 自定义按钮方法
        } else {
            [self shareWebPageToPlatformType:platformType userInfo:data];
        }
    }];
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType userInfo:(NSDictionary *)userInfo
{
    NSString *title = userInfo[@"title"];
    NSString *content = userInfo[@"content"];
    NSString *url = userInfo[@"url"];
    NSString *imageUrl = userInfo[@"imageUrl"];

    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:content thumImage:imageUrl];
    
    //设置网页地址
    shareObject.webpageUrl = url;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
        
    }];
}

2.第三方登录

#import 
#pragma mark - 微信登录
- (void)getAuthWithUserInfoFromWechat
{
    
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            NSLog(@"error == %@",error);
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"Wechat uid: %@", resp.uid);
            NSLog(@"Wechat openid: %@", resp.openid);
            NSLog(@"Wechat unionid: %@", resp.unionId);
            NSLog(@"Wechat accessToken: %@", resp.accessToken);
            NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
            NSLog(@"Wechat expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"Wechat name: %@", resp.name);
            NSLog(@"Wechat iconurl: %@", resp.iconurl);
            NSLog(@"Wechat gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
            
            // 通过第三方平台SDK源数据进行 登录、注册、绑定账号 等操作 需与后台配合设置接口
            // 登录、注册
            [LoginNetManager boolBindingWithType:2 unionId:resp.unionId completionHandler:^(ThirdInfoModel *model, NSError *error) {
                if (!error) {
                    if ([model.meta.code isEqualToNumber:@0]) {//账户已存在,直接登录
                        username = [NSString stringWithFormat:@"WX_%@",resp.unionId];
                        password = @"123456";
                        isThirdLogin = YES;
                        [self getLoginJson];
                    } else {//账户不存在,申请注册
                        ThirdRegisterViewController *viewController = [[ThirdRegisterViewController alloc] init];
                        viewController.loginType = 2;
                        viewController.userInfo = resp;
                        [self.navigationController pushViewController:viewController animated:YES];
                    }
                }
            }];

            // 绑定账号
            NSDictionary *qq = @{};
            NSDictionary *wechat = @{@"unionId":resp.unionId};
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSString *phone = [defaults objectForKey:@"loginName"];
            [LoginNetManager bindRegisterInfoWithType:2 phone:phone weChatAttentionInfoVO:wechat qqAttentionInfoVO:qq completionHandler:^(ThirdInfoModel *model, NSError *error) {
                if (!error) {
                    if ([model.meta.code isEqualToNumber:@0]) {
                        [self.view showMsg:@"绑定成功"];
                        [self getThirdRegeisteInfo];
                    }else{
                        [self.view showMsg:model.meta.message];
                    }
                }
            }];
        }
    }];
}

#pragma mark - QQ登录
- (void)getAuthWithUserInfoFromQQ
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            NSLog(@"error == %@",error);
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"QQ uid: %@", resp.uid); // 此值为QQ的openid数据
            NSLog(@"QQ openid: %@", resp.openid); // 此值为QQ的openid数据
            NSLog(@"QQ unionId: %@", resp.unionId);// 此值为QQ的union id数据,目前仅支持有在QQ互联获得union id获取权限的App id获取。
            NSLog(@"QQ accessToken: %@", resp.accessToken);
            NSLog(@"QQ expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"QQ name: %@", resp.name);
            NSLog(@"QQ iconurl: %@", resp.iconurl);
            NSLog(@"QQ gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            NSLog(@"QQ originalResponse: %@", resp.originalResponse);
            
            // 通过第三方平台SDK源数据进行 登录、注册、绑定账号 等操作 需与后台配合设置接口
            // 登录、注册
            [LoginNetManager boolBindingWithType:1 unionId:resp.openid completionHandler:^(ThirdInfoModel *model, NSError *error) {
                if (!error) {                    
                    if ([model.meta.code isEqualToNumber:@0]) {
                        username = [NSString stringWithFormat:@"QQ_%@",resp.openid];
                        password = @"123456";
                        isThirdLogin = YES;
                        [self getLoginJson];
                    } else {
                        ThirdRegisterViewController *viewController = [[ThirdRegisterViewController alloc] init];
                        viewController.loginType = 1;
                        viewController.userInfo = resp;
                        [self.navigationController pushViewController:viewController animated:YES]; 
                    }
                }
            }];

            // 绑定账号
            NSDictionary *wechat = @{};
            NSDictionary *qq = @{@"unionId":resp.openid};
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            NSString *phone = [defaults objectForKey:@"loginName"];
            [LoginNetManager bindRegisterInfoWithType:1 phone:phone weChatAttentionInfoVO:wechat qqAttentionInfoVO:qq completionHandler:^(ThirdInfoModel *model, NSError *error) {
                if (!error) {
                    if ([model.meta.code isEqualToNumber:@0]) {
                        [self.view showMsg:@"绑定成功"];
                        [self getThirdRegeisteInfo];
                    }else{
                        [self.view showMsg:model.meta.message];
                    }
                }
            }];
        }
    }];
}

#pragma mark - 新浪微博登录
- (void)getAuthWithUserInfoFromSina
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            NSLog(@"error == %@",error);
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"Sina uid: %@", resp.uid);
            NSLog(@"Sina accessToken: %@", resp.accessToken);
            NSLog(@"Sina refreshToken: %@", resp.refreshToken);
            NSLog(@"Sina expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"Sina name: %@", resp.name);
            NSLog(@"Sina iconurl: %@", resp.iconurl);
            NSLog(@"Sina gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            NSLog(@"Sina originalResponse: %@", resp.originalResponse);
        }
    }];
}

你可能感兴趣的:(友盟集成--第三方分享、登录)