2024-05-27 该文章已经无效了
一些参考地址:
Twitter官网地址
TwitterSDK获取,工程配置
Twitter登录接入
Twitter遇到问题,可以在Twitter工程的Issues里搜索
接入Twitter
具体查看上面链接
创建应用
1、创建应用
进入网址:https://apps.twitter.com/ 创建应用,获得key,secret,勾选
Allow this application to be used to Sign in with Twitter
2、在Callback URLs添加 twitterkit-consumerkey://
不然会返回下面的错误
[TwitterKit] Error obtaining user auth token.
error: Request failed: forbidden (403)
具体查看地址
工程配置
1、库文件导入
这个链接里,获取SDKTwitterSDK获取
下载引入库文件
TwitterCore.framework
TwitterKit.framework
TwitterKitResources.bundle
需要而外添加
SafariServices.framework
//如果工程配置了Other Linker Flags为-ObjC的话需要添加下面两个库文件
CoreLocation.framework
MapKit.framework
2、Info.plist配置
配置URL types和LSApplicationQueriesSchemes
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>twitterkit-<consumerKey></string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitter</string>
<string>twitterauth</string>
</array>
如图:
代码调用
一、SDK初始化
在Appdelegate里添加
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
return [[Twitter sharedInstance] application:app openURL:url options:options];
}
TwitterSDK初始化,要在调用登录之前调用
[[TWTRTwitter sharedInstance] startWithConsumerKey:@"6UFxIZuZIhAy4Mi8xxxxxxxx" consumerSecret:@"MmOJhdd13jR5a3TmEpey5RdXVXBG6qNJZpES4rFXhxxxxxxxx"];
二、使用Twitter按钮登录
TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
if (session) {
NSLog(@"signed in as %@", [session userName]);
NSLog(@"signed in as %@", [session userID]);
NSLog(@"signed in as %@", [session authToken]);
NSLog(@"signed in as %@", [session authTokenSecret]);
} else {
NSLog(@"error: %@", [error localizedDescription]);
}
}];
三、使用自定义Button
- (IBAction)loginWithTwitter:(UIButton *)sender {
TWTRSession *session = [[Twitter sharedInstance] sessionStore].session;
if(session) {
//如果已经授权过了,判断session是否为空,直接返回用户信息。
NSLog(@"userName = %@", [session userName]);
NSLog(@"userID = %@", [session userID]);
NSLog(@"authToken = %@", [session authToken]);
NSLog(@"authTokenSecret = %@", [session authTokenSecret]);
self->_userId = [session userID];
return;
}
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
if (session) {
NSLog(@"userName = %@", [session userName]);
NSLog(@"userID = %@", [session userID]);
NSLog(@"authToken = %@", [session authToken]);
NSLog(@"authTokenSecret = %@", [session authTokenSecret]);
self->_userId = [session userID];
} else {
NSLog(@"error: %@", [error localizedDescription]);
}
}];
}
四、注销账号
Twitter账号注销
- (IBAction)logout:(id)sender {
if(_userId) {
[[[Twitter sharedInstance] sessionStore] logOutUserID:_userId];
}
}
常见问题
如果在Other Linker Flags里面添加了-ObjC
就会出现下面这个错误
解决办法就是单独对需要加-ObjC标记的库,单独引用
使用-force_load+framewrok目录
-force_load$(PROJECT_DIR)/xxx/xxx/xxx.framework/xxx
或者添加
CoreLocation.framework
MapKit.framework
错误提示如下
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_CLLocationManager", referenced from:
objc-class-ref in TwitterKit(TWTRSETweetComposerViewController.o)
"_OBJC_CLASS_$_MKMapView", referenced from:
objc-class-ref in TwitterKit(TWTRSELocationMapTableViewHeaderView.o)
"_MKCoordinateRegionMakeWithDistance", referenced from:
-[TWTRSELocationMapTableViewHeaderView initWithCenterCoordinates:] in TwitterKit(TWTRSELocationMapTableViewHeaderView.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)