nativescript-twitter-updated
NativeScript Twitter Kit for Android & IOS
npm i --save nativescript-twitter-updated
- 版本: 22.0.4
- GitHub:
- NPM: https://npmjs.net.cn/package/nativescript-twitter-updated
- 下载量
- 昨天: 8
- 上周: 11
- 上个月: 30
Nativescript Twitter
Twitter SDK 的 NativeScript 实现(部分功能)
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 方案,以获取源代码 Source
// 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;
}
重要:请确保在页面标签中包含 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"/>