美文网首页
iOS 检测本机twitter账号是否已登录

iOS 检测本机twitter账号是否已登录

作者: 蛮荒星域 | 来源:发表于2017-10-23 14:53 被阅读470次

    前言

    简单介绍一下背景,外企,所以在做分享功能的时候公司要求集成Facebook分享和Twitter分享。为了便捷,直接使用了UMShare最新的SDK。实测过程中发现twitter分享未实现,友盟Demo同样也是。

    通过查看log信息(有时甚至都没有log信息),得出结论(友盟给出了同样的结论),必须本机中登录了Twitter账号才可以分享,否则无法进行twitter分享。

    需要解决的问题:本机中是否登录了twitter账号。(问题不是是否安装了twitter客户端,这个好解决,友盟已经集成了相应的方法)

    方法一

    导入头文件

    #import<Accounts/Accounts.h>
    

    然后:

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
    
    

    如果twitterAccounts有一个元素,那么就是存在已登录的twitter账号,可以进行下去了。如果数组个数为0则没有登录账号。但是还有个情况,为null。原因是:还没有进行授权的情况下,查询到的数组为null。所以修改下逻辑。

    - (void)checkTwitterLoginStatus1 {
        // checkAccessTwitterAccount
        ACAccountStore *accountStore = [[ACAccountStore alloc] init];
        ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
        NSLog(@"twitterAccounts = %@", twitterAccounts);
        
        if (!twitterAccounts) {
            [accountStore requestAccessToAccountsWithType: accountType options:nil completion:^(BOOL granted, NSError *error) {
                if (error) {
                    NSLog(@"error = %@", [error localizedDescription]);
                }
                
                NSLog(@"Twitter auth : %@", granted ? @"YES" : @"NO");
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    if(granted){
                        NSLog(@"授权通过了");
                        NSLog(@"可以授权");
                        NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
                        if (twitterAccounts.count != 0) {
                            NSLog(@"有授权 -- YES");
                        } else {
                            NSLog(@"没有授权 -- NO");
                        }
                    }else{
                        NSLog(@"授权未通过 -- NO");
                    }
                });
            }];
        } else if (twitterAccounts.count == 0) {
            NSLog(@"无可用的账户 -- NO");
        } else {
            NSLog(@"有授权 -- YES");
        }
    }
    
    

    在获取授权之后就可以进行twitter分享了,没有授权的话建议加个弹框告知用户“twitter账号未登录,请登录”

    方法二

    首先需要导入头文件:

    #import <Social/Social.h>
    
    [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]
    

    用这个办法可以判断本机是否已经配置了twitter账号服务。准确说是服务,如果本机上安装了twitter的app,这里返回的也是YES。我在Stack Overflow上看到很多都是采用了这个办法,不知道他们这里是否存在疑惑。
    (检测twitter app的方法:
    [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"twitter://"]]或是直接使用友盟提供的也可以)

    - (void)checkTwitterLoginStatus2 {
        if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
            NSLog(@"有配置账号");
        } else {
            NSLog(@"UnAvailable");
        }
    }
    

    暂时没有Demo,如果需要的话,可以私信或者评论区留言。谢谢!如果文中有误或者有更好的办法希望可以多交流。

    相关文章

      网友评论

          本文标题: iOS 检测本机twitter账号是否已登录

          本文链接:https://www.haomeiwen.com/subject/uylauxtx.html