iOS集成twitter分享

有个新项目中需要用到twitter分享,按照从前的经验,注册推特账号,然后申请开发者,之后创建app,得到API Key,API Key Secret,Bearer Token,然后集成友盟SDK,通过友盟来调用twitter分享。

意外的发现分享失败了。

************Share fail with error Error Domain=TwitterAPIErrorDomain Code=453 "Request failed: forbidden (403)" UserInfo={NSLocalizedFailureReason=Twitter API error : You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve (code 453), TWTRNetworkingStatusCode=403, NSErrorFailingURLKey=https://api.twitter.com/1.1/statuses/update.json, NSLocalizedDescription=Request failed: forbidden (403)}*********

大体应该是当前创建的twitter应用只支持V2版本的API,而友盟则会调用V1.1版本的API。

查了一下,新注册的twitter开发者只支持V2的API,如果想要额外的访问权限,可以选择申请免费的高级访问权限。而在2021年11月15日之前申请成功的开发者,将自动转换为高级访问权限。

而友盟调用twitter分享用的twitterkit库,这个库在2019年就已经停止维护了,目前只支持V1.1的API。

尝试去提升访问权限,却意外发现twitter的App状态变为受限了。

This App has violated Twitter Rules and policies. As a result, certain functions will be limited. An email has been sent to xx*****@xx*****.com with details. For assistance, submit a support ticket.

应该是调用V1.1的API次数太多导致的吧。

最终的解决方案:

采用最简单粗暴的方式,直接用openURL硬拉起twitter。

NSString *encodedString = [NSString stringWithFormat:@"twitter://post?message=%@\n%@",title, url];
encodedString = [encodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:encodedString]];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中实现Twitter登录功能的Demo可以按照以下步骤进行: 1. 首先,在Twitter开发者网站上创建一个应用,获取应用的API Key和Secret Key,并将它们添加到项目的Info.plist文件的URL schemes和URL whitelist中。 2. 在AppDelegate中导入Twitter框架并添加以下代码: ```swift import TwitterKit func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { TWTRTwitter.sharedInstance().start(withConsumerKey: "YOUR_API_KEY", consumerSecret: "YOUR_API_SECRET") return true } ``` 3. 在需要实现登录的ViewController中导入Twitter框架并添加以下代码: ```swift import TwitterKit @IBAction func twitterLoginButtonTapped(_ sender: UIButton) { TWTRTwitter.sharedInstance().logIn { (session, error) in if (session != nil) { print("Twitter login successful with user ID: \(session!.userID)") } else { print("Twitter login error: \(error!.localizedDescription)") } } } ``` 在上述代码中,当用户点击Twitter登录按钮时,调用`TWTRTwitter.sharedInstance().logIn`方法会打开Twitter登录界面,用户在该界面上登录成功后,会返回一个session,其中包含用户的userID和token等信息。根据返回的session是否为nil,判断登录是否成功。 注意:在使用此Demo之前,需要在项目的Build Settings中的Other Linker Flags添加`-ObjC`标志,否则可能会出现链接错误。 以上就是实现Twitter登录功能的iOS开发Demo。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值