ios - 无法在 iOS 11 上使用 TwitterKit 发送推文

标签 ios twitter tweets twitterkit

我正在尝试使用 TwitterKit 通过提供的 TWTRComposer 类来撰写推文。这是我调用的函数:

-(void) tweet:(UIViewController *) root {
    TWTRComposer *composer = [[TWTRComposer alloc] init];

    [composer setText:@"just setting up my Twitter Kit"];

    // Called from a UIViewController
    [composer showFromViewController:root completion:^(TWTRComposerResult result) {
        if (result == TWTRComposerResultCancelled) {
            NSLog(@"Tweet composition cancelled");
        }
        else {
            NSLog(@"Sending Tweet!");
        }
    }];
}

这有两个问题:

  1. 当我的设备上安装了 Twitter 应用程序时,会出现 TWTRComposer 对话框,但不久之后,顶部会出现另一个警告,标题为:“没有 Twitter 帐户”,并且以下消息:“没有配置 Twitter 帐户。您可以在“设置”中添加或创建 Twitter 帐户。” 我可以关闭这个对话框,然后发送推文(成功),但这显然是非常糟糕的用户体验。此外,此错误对话框很奇怪,因为 iOS 11 的“设置”中不再有 Twitter。
  2. 当我的设备上安装 Twitter 应用程序时,根本不会显示 TWTRComposer 对话框。而是立即调用 showFromViewController 方法中的完成 block ,结果类型为 TWTRComposerResultCancelled。

我觉得这可能与 Twitter 的登录问题有某种关系。因为我正在开发的应用程序不包括使用 Twitter 注册/登录。但是,我的印象是 TWTRComposer 处理所有登录。

非常感谢您的帮助,感谢您的阅读!

最佳答案

你是对的:由于 iOS 11 的变化,你需要在调用 TWTRComposer 之前登录。

iOS 11 no longer supports using Twitter through the built-in social framework. Instead, you can use Twitter Kit 3 to Tweet, log in users, and use the Twitter API. The following guide shows how to migrate your old code.

登录(按以下顺序,如果可能,iOS 版 Twitter/SFSafariViewController/UIWebView。检查 prerequisites )然后撰写:

目标:

// Check if current session has users logged in
if ([[Twitter sharedInstance].sessionStore hasLoggedInUsers]) {
    TWTRComposerViewController *composer = [TWTRComposerViewController emptyComposer];
    [fromController presentViewController:composer animated:YES completion:nil];
} else {
    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
        if (session) {
            TWTRComposerViewController *composer = [TWTRComposerViewController emptyComposer];
            [fromController presentViewController:composer animated:YES completion:nil];
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"No Twitter Accounts Available" message:@"You must log in before presenting a composer." preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

swift :

if Twitter.sharedInstance().sessionStore.hasLoggedInUsers() {
    // App must have at least one logged-in user to compose a Tweet
    let composer = TWTRComposerViewController.emptyComposer()
    present(composer, animated: true, completion: nil)
} else {
    // Log in, and then check again
    Twitter.sharedInstance().logIn { session, error in
        if session != nil { // Log in succeeded
            let composer = TWTRComposerViewController.emptyComposer()
            self.present(composer, animated: true, completion: nil)
        } else {
            let alert = UIAlertController(title: "No Twitter Accounts Available", message: "You must log in before presenting a composer.", preferredStyle: .alert)
            self.present(alert, animated: false, completion: nil)
        }
    }
}

文档:

关于ios - 无法在 iOS 11 上使用 TwitterKit 发送推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45514354/

相关文章:

ios - Xcode 中的线程 1 : EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subside = 0x0)

javascript - 当您使用 javascript 填充需要子元素的元素时,如何编写有效的 XHTML 1.0 Strict 代码?

java.lang.NoClassDefFoundError : org/mule/api/ConnectionException when trying to use twitter connector 错误

python-2.7 - 是否可以在没有 Twitter API 的情况下读取推文 URL 的推文文本?

python - python twitter api 1.1获取转推ID

ios - 在 Debug模式下禁用结构答案

ipad - UIPopoverController & UIImagePickerControl : "Popovers cannot be presented from a view which does not have a window"

ios - swift 4 : 'MessageKind' has no member 'url'

iphone - 使用 Web 服务的教程?

html - 使用 '#'(标签)发送一条消息