【问题标题】:SLComposeViewController: Different behavior of "No Twitter Accounts" alert depending on whether or not Twitter app is installedSLComposeViewController:“无 Twitter 帐户”警报的不同行为取决于是否安装了 Twitter 应用程序
【发布时间】:2016-06-30 16:27:05
【问题描述】:

我在我的应用中使用以下代码进行 Twitter 分享:

SLComposeViewController *twitter = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitter setInitialText:@"some text"];

twitter.completionHandler = ^(SLComposeViewControllerResult result)
{
    // do something...
    [topViewController dismissViewControllerAnimated:YES completion:nil];
};
[topViewController presentViewController:twitter animated:YES completion:nil];

现在,如果没有设置 Twitter 帐户并且 Twitter 应用未安装,我会收到以下警报:

这正是它应该做的,按下“取消”会关闭警报视图和 Twitter 撰写视图。按“设置”也会关闭两个视图并打开设置。不错。

现在,如果没有设置 Twitter 帐户,但 Twitter 应用程序已安装,我会收到以下警报:

请注意,没有“取消”和“设置”按钮,只有“确定”按钮。如果按下,警报视图会消失,但 Twitter 撰写视图会保留在那里,“发布”按钮变灰。因此,用户只能再次按“取消”以关闭撰写视图并自己进入设置。不太好。

请注意,在展示 SLComposeViewController 之前是否检查服务可用性并没有什么区别:

[SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter];

您知道为什么会有这种行为差异吗?是否有办法获得相同的行为,无论是否安装了 Twitter 应用程序?

在 iOS 9.2.1、iPad Air(第二代)和 iPhone 6 Plus 上测试。

【问题讨论】:

标签: ios objective-c twitter slcomposeviewcontroller


【解决方案1】:

检查 [this] (Opening the Settings app from another app)。 滚动到底部,有一个尚未被投票(奇怪地)包含图像的答案。 那里的链接向您展示了如何从您的应用导航到设置/推特。 下一步是研究使用 ACAccountStore,检查是否没有用户通过数组登录。

let accountStore = ACAccountStore()
let accountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)

accountStore.requestAccessToAccountsWithType(accountType, options: nil, completion: { (granted, error) in
if (granted) {
         let arrayOfAccts = accountStore.accountsWithAccountType(accountType)
         if (arrayOfAccts.count > 0) {
         //regular function of twitter
         }
         //else is when there are no accounts logged into twitter
         else {
         //bring up a custom UIAlertAction with two options; Cancel and Settings.
         //settings should use the openURL to prefs:root=TWITTER
         } 
}

当用户未登录 twitter 且未安装应用程序时,它将模仿它的工作方式,为您提供取消和设置。

至于为什么你得到不同的警报设置我无法理解,这可能是 iOS 苹果的事情。

这一行:

[SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter];

检查是否安装了 Twitter 应用程序。 希望这会有所帮助。

【讨论】:

  • 感谢您的回答,@Nathan!发布的解决方案效果很好。不幸的是,用户必须确认访问 Twitter 帐户。我还使用 RDC 的链接解决方案链接到设置应用程序,因为它似乎适用于 iOS 6 到 9。但总而言之,如果 RDC 的,我最终可能会保持一切原样并忍受原始错误解决方案不经过审查过程。这种风险和必要的帐户请求可能只是为了解决这个相当小的错误而矫枉过正。但无论如何,这是一个很好的解决方案。
猜你喜欢
  • 1970-01-01
  • 2011-11-25
  • 2012-01-05
  • 1970-01-01
  • 2011-11-08
  • 2019-03-09
  • 2020-03-06
  • 2013-04-23
  • 1970-01-01
相关资源
最近更新 更多