0

我实现了一个 TWTRLoginButton 但由于某种原因完成块没有注册/被调用,我不知道为什么会这样......

登录按钮初始化:

self.twitterLoginView = TWTRLogInButton(logInCompletion: { session, error in
        if (session != nil) {
            print("signed in as \(session.userName)");
        } else {
            print("error: \(error.localizedDescription)");
        }
    })

单击按钮时出现错误:

TWTRLogInButton 是在没有设置 completionBlock 的情况下创建的

4

2 回答 2

0

当应用程序从 Twitter 重定向回来时,您需要让 TwitterKit 处理重新打开应用程序:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let twtrHandled = TWTRTwitter.sharedInstance().application(app, open: url, options: options)
    return twtrHandled
}
于 2018-03-26T08:38:32.407 回答
-1

在代码或故事板中取一个 UIButton,然后将按钮操作赋予以下方法。

 -(IBAction)twitterLogin:(id)sender
{

    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error){
        if (session)
        {
             [[Twitter sharedInstance]ini]
             [[[Twitter sharedInstance] APIClient] loadUserWithID:[session userID]
                                                       completion:^(TWTRUser *user,
                                                                    NSError *error)
              {
                  // handle the response or error
                  if (![error isEqual:nil]) {

                      NSLog(@"Twitter info   -> user = %@ ",user.description);
                      {
                          NSMutableDictionary *dict =[[NSMutableDictionary alloc]init];
                          [dict setValue:user.userID forKey:@"id"];
                          [dict setValue:user.name forKey:@"name"];
                          [dict setValue:user.screenName forKey:@"screenName"];
                          NSLog(@"User Description is %@",dict);
                      }

                  } else {
                  }
              }];

         } else {
             NSLog(@"error: %@", [error localizedDescription]);
         }
     }];

}
于 2015-12-24T07:08:24.117 回答