nativescript-twitter
NativeScript Twitter Kit for Android & IOS
npm i --save nativescript-twitter

Nativescript Twitter

Twitter SDK 的 NativeScript 实现(部分功能)

npm npm

NS 3.0+

tns plugin add nativescript-twitter

NS < 3.0

npm install nativescript-twitter@"^1.x"

示例实现

Android

//app.ts or main.ts
import * as app from "application";
if (app.android) {
const TNSTwitter = require("nativescript-twitter").TNSTwitter;
TNSTwitter.init("key", "secret");
}

IOS

注意 Twitter Kit 会查找格式为 twitterkit-<consumerKey> 的 URL 模式,其中 consumerKey 是您应用程序的 Twitter API 密钥,例如 twitterkit-dwLf79lNQfsJ。

在您应用程序的 Info.plist 中,在 <dict> 之后添加以下代码以添加 URL Scheme,请参阅

// Info.plist
<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
//custom-app-delegate.ts
import * as utils from "utils/utils";
declare const UIResponder, UIApplicationDelegate, Twitter;
export class CustomAppDelegate extends UIResponder implements UIApplicationDelegate {
public static ObjCProtocols = [UIApplicationDelegate];
applicationDidFinishLaunchingWithOptions(application, launchOptions) {
utils.ios.getter(Twitter,Twitter.sharedInstance).startWithConsumerKeyConsumerSecret("key" ,"secret");
return true;
}
applicationOpenURLOptions(application, url, options) {
return utils.ios.getter(Twitter,Twitter.sharedInstance).applicationOpenURLOptions(application, url, options);
}
}
//app.ts or main.ts
import * as app from "application";
if (app.ios) {
app.ios.delegate = require('./custom-app-delegate').CustomAppDelegate;
}

重要:确保在 Page 标签上包含 xmlns:twitter="nativescript-twitter"

<twitter:TNSTwitterButton id="twitter"/>

监听用户认证成功或失败时

import * as frame from "ui/frame";
frame.topmost().getViewById('twitter').on('loginStatus', (args) => {
if (args.object.get("value") === 'failed') {
console.log(args.object.get("message"))
} else {
TNSTwitter.getCurrentUser(args.object.get("userID")).then(
(user) => {
console.dump(user)
}, err => {
console.dump(err)
})
}

});

发送 API 请求

import { CustomApiService } from "nativescript-twitter"
const api = new CustomApiService();
api.makeRequest("https://api.twitter.com/1.1/account/verify_credentials.json", "get")
.then(
data => {
console.log(data)
}, err => {
console.log(err.message)
});

Angular

import { registerElement } from "nativescript-angular/element-registry";
registerElement("TNSTwitterButton", () => require("nativescript-twitter").TNSTwitterButton);
<TNSTwitterButton id="twitter"/>