前言
簡(jiǎn)單介紹一下背景怎披,外企,所以在做分享功能的時(shí)候公司要求集成Facebook分享和Twitter分享。為了便捷疏尿,直接使用了UMShare最新的SDK。實(shí)測(cè)過(guò)程中發(fā)現(xiàn)twitter分享未實(shí)現(xiàn)易桃,友盟Demo同樣也是褥琐。
通過(guò)查看log信息(有時(shí)甚至都沒有l(wèi)og信息),得出結(jié)論(友盟給出了同樣的結(jié)論)晤郑,必須本機(jī)中登錄了Twitter賬號(hào)才可以分享敌呈,否則無(wú)法進(jìn)行twitter分享嚼鹉。
需要解決的問(wèn)題:本機(jī)中是否登錄了twitter賬號(hào)。(問(wèn)題不是是否安裝了twitter客戶端驱富,這個(gè)好解決锚赤,友盟已經(jīng)集成了相應(yīng)的方法)
方法一
導(dǎo)入頭文件
#import<Accounts/Accounts.h>
然后:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
如果twitterAccounts有一個(gè)元素,那么就是存在已登錄的twitter賬號(hào)褐鸥,可以進(jìn)行下去了线脚。如果數(shù)組個(gè)數(shù)為0則沒有登錄賬號(hào)。但是還有個(gè)情況叫榕,為null浑侥。原因是:還沒有進(jìn)行授權(quán)的情況下,查詢到的數(shù)組為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(@"授權(quán)通過(guò)了");
NSLog(@"可以授權(quán)");
NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
if (twitterAccounts.count != 0) {
NSLog(@"有授權(quán) -- YES");
} else {
NSLog(@"沒有授權(quán) -- NO");
}
}else{
NSLog(@"授權(quán)未通過(guò) -- NO");
}
});
}];
} else if (twitterAccounts.count == 0) {
NSLog(@"無(wú)可用的賬戶 -- NO");
} else {
NSLog(@"有授權(quán) -- YES");
}
}
在獲取授權(quán)之后就可以進(jìn)行twitter分享了,沒有授權(quán)的話建議加個(gè)彈框告知用戶“twitter賬號(hào)未登錄,請(qǐng)登錄”
方法二
首先需要導(dǎo)入頭文件:
#import <Social/Social.h>
[SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]
用這個(gè)辦法可以判斷本機(jī)是否已經(jīng)配置了twitter賬號(hào)服務(wù)荞下。準(zhǔn)確說(shuō)是服務(wù)伶选,如果本機(jī)上安裝了twitter的app,這里返回的也是YES尖昏。我在Stack Overflow上看到很多都是采用了這個(gè)辦法仰税,不知道他們這里是否存在疑惑。
(檢測(cè)twitter app的方法:
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"twitter://"]]
或是直接使用友盟提供的也可以)
- (void)checkTwitterLoginStatus2 {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
NSLog(@"有配置賬號(hào)");
} else {
NSLog(@"UnAvailable");
}
}
暫時(shí)沒有Demo抽诉,如果需要的話陨簇,可以私信或者評(píng)論區(qū)留言。謝謝迹淌!如果文中有誤或者有更好的辦法希望可以多交流河绽。