iOS 接入Twitter 相关注意点

作者: 72行代码 | 来源:发表于2019-04-23 17:07 被阅读1次

    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
    <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>
    - (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
    - (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
    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
    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
      if (session) {
          NSLog(@"signed in as %@", [session userName]);
      } else {
          NSLog(@"error: %@", [error localizedDescription]);
      }
    }];
    
    • Request User Email Address
    TWTRAPIClient *client = [TWTRAPIClient clientWithCurrentUser];
    [client requestEmailForCurrentUser:^(NSString *email, NSError *error) {
      if (email) {
          NSLog(@"signed in as %@", email);
      } else {
          NSLog(@"error: %@", [error localizedDescription]);
      }
    }];
    

    附:我的博客地址

    相关文章

      网友评论

        本文标题:iOS 接入Twitter 相关注意点

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