Skip to content

Commit

Permalink
feat(twitter): switch ios sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Nov 27, 2021
1 parent 0953ec9 commit e2c4a7c
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 56 deletions.
16 changes: 5 additions & 11 deletions packages/twitter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,26 @@ Here is how callbacks would look like:

### iOS


Configure Info.Plist like below, replace `consumerKey` tag with your own key:

```xml
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>twitterkit-<consumerKey></string>
<string>yourscheme></string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitter</string>
<string>twitterauth</string>
</array>
```

```ts
import { Twitter, TwitterSignIn } from '@nativescript/twitter';

TwitterSignIn.init(TWITTER_COMSUMER_KEY, TWITTER_CONSUMER_SECRET);
Twitter.callback = 'yourscheme://';

Twitter.init(TWITTER_COMSUMER_KEY, TWITTER_CONSUMER_SECRET);

Twitter.logIn()
TwitterSignIn.logIn()
.then((session) => {
// session.authToken
// session.authTokenSecret
Expand Down
15 changes: 14 additions & 1 deletion packages/twitter/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ export class TwitterUser implements ITwitterUser {
get android() {
return this.native;
}

toJSON() {
return {
formattedScreenName: this.formattedScreenName,
isProtected: this.isProtected,
isVerified: this.isVerified,
name: this.name,
profileImageUrl: this.profileImageUrl,
profileUrl: this.profileUrl,
screenName: this.screenName,
userId: this.userId
}
}
}

export class Twitter {
Expand Down Expand Up @@ -180,7 +193,7 @@ export class TwitterSignIn {
}

sessionManager.clearActiveSession();
} catch (e) {}
} catch (e) { }
}

static getCurrentUser() {
Expand Down
1 change: 1 addition & 0 deletions packages/twitter/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export declare class Session implements ISession {
}

export declare class Twitter {
static callback: string;
static init(consumerKey: string, consumerSecret: string);
}

Expand Down
153 changes: 113 additions & 40 deletions packages/twitter/index.ios.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Application, Device } from '@nativescript/core';
import { ISession, ITwitterUser } from './common';

export class TwitterError extends Error {
Expand All @@ -14,9 +15,10 @@ export class TwitterError extends Error {
}

let appDelegateInitialized = false;
let appDelegate: TwitterAppDelegateImpl;
@NativeClass
let appDelegate;

@ObjCClass(UIApplicationDelegate)
@NativeClass
class TwitterAppDelegateImpl extends UIResponder implements UIApplicationDelegate {
static get sharedInstance() {
if (!appDelegate) {
Expand All @@ -26,14 +28,27 @@ class TwitterAppDelegateImpl extends UIResponder implements UIApplicationDelegat
}

applicationOpenURLOptions(app: UIApplication, url: NSURL, options: NSDictionary<string, any>): boolean {
return TWTRTwitter.sharedInstance().applicationOpenURLOptions(app, url, options);
TNSTwitter.handleOpenURL(url.absoluteString, Twitter.callback, false);
return true;
}

}


if (parseFloat(Device.sdkVersion) >= 13) {
(<any>TwitterAppDelegateImpl).prototype.applicationConfigurationForConnectingSceneSessionOptions = (application: UIApplication, connectingSceneSession: UISceneSession, options: UISceneConnectionOptions) => {
return UISceneConfiguration.configurationWithNameSessionRole(
"Default Configuration",
connectingSceneSession.role
)
}
}


export class TwitterUser implements ITwitterUser {
#native: TWTRUser;
static fromNative(user: TWTRUser) {
if (user instanceof TWTRUser) {
#native: any;
static fromNative(user: any) {
if (user) {
const usr = new TwitterUser();
usr.#native = user;
return usr;
Expand All @@ -42,35 +57,35 @@ export class TwitterUser implements ITwitterUser {
}

get formattedScreenName(): string {
return this.native.formattedScreenName;
return this.native.screen_name;
}

get isProtected(): boolean {
return this.native.isProtected;
return this.native.protected;
}

get isVerified(): boolean {
return this.isVerified;
return this.native.verified;
}

get name(): string {
return this.native.name;
}

get profileImageUrl(): string {
return this.native.profileImageURL;
return this.native.profile_image_url_https;
}

get profileUrl(): string {
return this.native.profileURL?.absoluteString;
return this.native.url;
}

get screenName(): string {
return this.native.screenName;
return this.native.screen_name;
}

get userId(): string {
return this.native.userID;
return this.native.id_str;
}

get native() {
Expand All @@ -80,12 +95,26 @@ export class TwitterUser implements ITwitterUser {
get ios() {
return this.native;
}


toJSON() {
return {
formattedScreenName: this.formattedScreenName,
isProtected: this.isProtected,
isVerified: this.isVerified,
name: this.name,
profileImageUrl: this.profileImageUrl,
profileUrl: this.profileUrl,
screenName: this.screenName,
userId: this.userId
}
}
}

export class Session implements ISession {
#native: TWTRSession;
static fromNative(ts: TWTRSession) {
if (ts instanceof TWTRSession) {
#native: any;
static fromNative(ts) {
if (ts) {
const session = new Session();
session.#native = ts;
return session;
Expand Down Expand Up @@ -117,9 +146,12 @@ export class Session implements ISession {
}
}


export class Twitter {
static _twitter: TNSTwitter;
static callback: string;
static init(consumerKey: string, consumerSecret: string) {
TWTRTwitter.sharedInstance().startWithConsumerKeyConsumerSecret(consumerKey, consumerSecret);
this._twitter = TNSTwitter.alloc().init(consumerKey, consumerSecret);

if (!appDelegateInitialized) {
GULAppDelegateSwizzler.proxyOriginalDelegate();
Expand All @@ -132,38 +164,79 @@ export class Twitter {
export class TwitterSignIn {
static logIn() {
return new Promise((resolve, reject) => {
TWTRTwitter.sharedInstance().logInWithCompletion((session, error) => {
if (session) {
resolve(Session.fromNative(session));
Twitter._twitter.authorizeWithBrowser(this.topViewController, Twitter.callback, (user, error) => {
if (user) {
try {
resolve(Session.fromNative(JSON.parse(user)));
} catch (e) {
reject(TwitterError.fromNative(e));
}
} else {
reject(TwitterError.fromNative(error));
}
});
})
});
}
static logOut() {
const store = TWTRTwitter.sharedInstance().sessionStore;
if (store) {
const userId = store.session()?.userID;
store.logOutUserID?.(userId);
}
}

static logOut() { }

static getCurrentUser(): Promise<TwitterUser> {
return new Promise((resolve, reject) => {
const store = TWTRTwitter.sharedInstance().sessionStore;
if (store) {
const userId = store.session()?.userID || '';
TWTRAPIClient.clientWithCurrentUser().loadUserWithIDCompletion(userId, (user, error) => {
if (error) {
reject(TwitterError.fromNative(error));
} else {
resolve(TwitterUser.fromNative(user));
Twitter._twitter.verifyUser(false, false, true, (user, error) => {
if (error) {
reject(TwitterError.fromNative(error));
} else {
try {
resolve(TwitterUser.fromNative(JSON.parse(user)));
} catch (e) {
reject(TwitterError.fromNative(e));
}
});
} else {
resolve(null);
}

}
})
});
}



private static get topViewController(): UIViewController | undefined {
const root = this.rootViewController;
if (!root) {
return undefined;
}
return this.findTopViewController(root);
}

private static get rootViewController(): UIViewController | undefined {
const keyWindow = UIApplication.sharedApplication.keyWindow;
return keyWindow ? keyWindow.rootViewController : undefined;
}

private static findTopViewController(root: UIViewController): UIViewController | undefined {
const presented = root.presentedViewController;
if (presented != null) {
return this.findTopViewController(presented);
}
if (root instanceof UISplitViewController) {
const last = root.viewControllers.lastObject;
if (last == null) {
return root;
}
return this.findTopViewController(last);
} else if (root instanceof UINavigationController) {
const top = root.topViewController;
if (top == null) {
return root;
}
return this.findTopViewController(top);
} else if (root instanceof UITabBarController) {
const selected = root.selectedViewController;
if (selected == null) {
return root;
}
return this.findTopViewController(selected);
} else {
return root;
}
}
}
2 changes: 1 addition & 1 deletion packages/twitter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/twitter",
"version": "1.0.0-alpha.3",
"version": "1.0.0-alpha.7",
"description": "Twitter for your NativeScript applications",
"main": "index",
"typings": "index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion packages/twitter/platforms/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pod 'TwitterKit5'
platform :ios, '10.0'
pod 'Swifter', :git => 'https://github.com/mattdonnelly/Swifter.git', :tag => "2.5.0"
pod 'GoogleUtilities', '~> 7.5'
Loading

0 comments on commit e2c4a7c

Please sign in to comment.