iOS 接入Twitter 相关注意点

1. 接入前配置

  • Download and unzip Twitter Kit
  • Add TwitterKit to “Embedded Binaries” in your Xcode project settings(测试发现不添加也可以)
  • Add TwitterKit and TwitterCore 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 Kit
    1
    2
    3
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
    return [[Twitter sharedInstance] application:app openURL:url options:options];
    }
    2. Twitter后台配置 https://apps.twitter.com/app

Twitter apps dashboard

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
    9
    TWTRLogInButton *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
    8
    TWTRAPIClient *client = [TWTRAPIClient clientWithCurrentUser];
    [client requestEmailForCurrentUser:^(NSString *email, NSError *error) {
    if (email) {
    NSLog(@"signed in as %@", email);
    } else {
    NSLog(@"error: %@", [error localizedDescription]);
    }
    }];
-------------本文结束感谢您的阅读-------------
最近的文章

iOS APNS device token特性

device token的一些特性: 开发环境获取的deviceToken和发布环境获取的deviceToken是不一样的 在一台设备中,deviceToken是系统级别的,不同App获得的deviceToken是相同的 deviceToken会过期 单个App的更新deviceToken不会 …

继续阅读
更早的文章

iOS runtime 相关实例

避免按钮快速点击多次相应问题 创建UIButton 分类 123456789101112&#x2F;&#x2F;&#x2F;&#x2F; UIButton+time.h&#x2F;&#x2F;#import &lt;UIKit&#x2F;UIKit.h&gt;@interface UIButt …

继续阅读