1. 接入前配置
- Download and unzip Twitter Kit
- Add
TwitterKit
to “Embedded Binaries” in your Xcode project settings(测试发现不添加也可以) - Add
TwitterKit
andTwitterCore
to “Linked Frameworks and Libraries” in your Xcode project settings - Add
SafariServices.framework
to use SFSafariViewController - In your app’s Info.plist, add URL Schemes by adding code below after
1
2
3
4
5
6
7
8
9
10
11
12
13
14<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> - Make sure to import the framework header:
#import <TwitterKit/TWTRKit.h>
1
2
3- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[Twitter sharedInstance] startWithConsumerKey:@"hTpkPVU4pThkM0" consumerSecret:@"ovEqziMzLpUOF163Qg2mj"];
} - Implement the application:openURL:options method in your Application Delegate, and pass along the redirect URL to Twitter Kit2. Twitter后台配置 https://apps.twitter.com/app
1
2
3- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
return [[Twitter sharedInstance] application:app openURL:url options:options];
}
From June 12th 2018 callback locking will no longer be optional. The correct callback format for iOS apps is:twitterkit-MY_CONSUMER_KEY://
3. 接入相关功能
- Log In Button
1
2
3
4
5
6
7
8
9TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
if (session) {
NSLog(@"signed in as %@", [session userName]);
} else {
NSLog(@"error: %@", [error localizedDescription]);
}
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton]; - Log In Method
1
2
3
4
5
6
7[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
if (session) {
NSLog(@"signed in as %@", [session userName]);
} else {
NSLog(@"error: %@", [error localizedDescription]);
}
}]; - Request User Email Address
1
2
3
4
5
6
7
8TWTRAPIClient *client = [TWTRAPIClient clientWithCurrentUser];
[client requestEmailForCurrentUser:^(NSString *email, NSError *error) {
if (email) {
NSLog(@"signed in as %@", email);
} else {
NSLog(@"error: %@", [error localizedDescription]);
}
}];