Skip to content

Commit

Permalink
feat: Add shareSingle telegram (iOS only) (#1061)
Browse files Browse the repository at this point in the history
* feat: add telegram share on ios (text and number)

* feat: telegram share ios url and url with message

* docs: add telegram share single ios
  • Loading branch information
akinncar committed Jul 29, 2021
1 parent 5ed224b commit 667171b
Show file tree
Hide file tree
Showing 10 changed files with 538 additions and 26 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
example/
website/

# generated by bob
Expand Down
19 changes: 19 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ const App = () => {
}
};

const shareToTelegram = async () => {
const shareOptions = {
message: 'Example Telegram',
url: 'https://google.com',
social: Share.Social.TELEGRAM,
};

try {
const ShareResponse = await Share.shareSingle(shareOptions);
setResult(JSON.stringify(ShareResponse, null, 2));
} catch (error) {
console.log('Error =>', error);
setResult('error: '.concat(getErrorString(error)));
}
};

const sharePdfBase64 = async () => {
const shareOptions = {
title: '',
Expand Down Expand Up @@ -204,6 +220,9 @@ const App = () => {
<View style={styles.button}>
<Button onPress={shareToInstagramStory} title="Share to IG Story" />
</View>
<View style={styles.button}>
<Button onPress={shareToTelegram} title="Share to Telegram" />
</View>
{Platform.OS === 'ios' && (
<View style={styles.button}>
<Button onPress={shareToFiles} title="Share To Files" />
Expand Down
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ PODS:
- React-Core (= 0.63.2)
- React-cxxreact (= 0.63.2)
- React-jsi (= 0.63.2)
- RNShare (3.7.1):
- React
- RNShare (6.4.0):
- React-Core
- Yoga (1.14.0)
- YogaKit (1.18.1):
- Yoga (~> 1.14)
Expand Down Expand Up @@ -460,10 +460,10 @@ SPEC CHECKSUMS:
React-RCTText: 1b6773e776e4b33f90468c20fe3b16ca3e224bb8
React-RCTVibration: 4d2e726957f4087449739b595f107c0d4b6c2d2d
ReactCommon: a0a1edbebcac5e91338371b72ffc66aa822792ce
RNShare: ea0393515826ea8d27a0e17cd3d6f3cc210630d9
RNShare: 129b187a54301c9f32ec7249fb4431e30a8d6ab8
Yoga: 7740b94929bbacbddda59bf115b5317e9a161598
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

PODFILE CHECKSUM: 0399d18cc42b344f43b18ebc8951bce82966aca6

COCOAPODS: 1.9.3
COCOAPODS: 1.10.1
17 changes: 9 additions & 8 deletions example/ios/example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram-stories</string>
<string>instagram</string>
<string>fb</string>
<string>whatsapp</string>
<string>mailto</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram-stories</string>
<string>instagram</string>
<string>fb</string>
<string>whatsapp</string>
<string>mailto</string>
<string>tg</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
Expand Down
6 changes: 6 additions & 0 deletions ios/RNShare.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#import "FacebookStories.h"
#import "GooglePlusShare.h"
#import "EmailShare.h"
#import "TelegramShare.h"
#import "RNShareActivityItemSource.h"
#import "Utils.h"

Expand Down Expand Up @@ -110,6 +111,7 @@ - (NSDictionary *)constantsToExport
@"WHATSAPP": @"whatsapp",
@"INSTAGRAM": @"instagram",
@"INSTAGRAMSTORIES": @"instagramstories",
@"TELEGRAM": @"telegram",
@"EMAIL": @"email",

@"SHARE_BACKGROUND_IMAGE": @"shareBackgroundImage",
Expand Down Expand Up @@ -164,6 +166,10 @@ - (NSDictionary *)constantsToExport
NSLog(@"TRY OPEN instagram-stories");
InstagramStories *shareCtl = [[InstagramStories alloc] init];
[shareCtl shareSingle:options failureCallback: failureCallback successCallback: successCallback];
} else if([social isEqualToString:@"telegram"]) {
NSLog(@"TRY OPEN telegram");
TelegramShare *shareCtl = [[TelegramShare alloc] init];
[shareCtl shareSingle:options failureCallback: failureCallback successCallback: successCallback];
} else if([social isEqualToString:@"email"]) {
NSLog(@"TRY OPEN email");
[shareCtl shareSingle:options failureCallback: failureCallback successCallback: successCallback];
Expand Down
53 changes: 53 additions & 0 deletions ios/TelegramShare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// TelegranShare.h
// RNShare
//
// Created by Akinn Rosa on 07-27-21.
//

#import <UIKit/UIKit.h>
// import RCTConvert
#if __has_include(<React/RCTConvert.h>)
#import <React/RCTConvert.h>
#elif __has_include("RCTConvert.h")
#import "RCTConvert.h"
#else
#import "React/RCTConvert.h" // Required when used as a Pod in a Swift project
#endif
// import RCTBridge
#if __has_include(<React/RCTBridge.h>)
#import <React/RCTBridge.h>
#elif __has_include("RCTBridge.h")
#import "RCTBridge.h"
#else
#import "React/RCTBridge.h" // Required when used as a Pod in a Swift project
#endif
// import RCTUIManager
#if __has_include(<React/RCTUIManager.h>)
#import <React/RCTUIManager.h>
#elif __has_include("RCTUIManager.h")
#import "RCTUIManager.h"
#else
#import "React/RCTUIManager.h" // Required when used as a Pod in a Swift project
#endif
// import RCTLog
#if __has_include(<React/RCTLog.h>)
#import <React/RCTLog.h>
#elif __has_include("RCTLog.h")
#import "RCTLog.h"
#else
#import "React/RCTLog.h" // Required when used as a Pod in a Swift project
#endif
// import RCTUtils
#if __has_include(<React/RCTUtils.h>)
#import <React/RCTUtils.h>
#elif __has_include("RCTUtils.h")
#import "RCTUtils.h"
#else
#import "React/RCTUtils.h" // Required when used as a Pod in a Swift project
#endif
@interface TelegramShare : NSObject <RCTBridgeModule>

- (void) shareSingle:(NSDictionary *)options failureCallback:(RCTResponseErrorBlock)failureCallback successCallback:(RCTResponseSenderBlock)successCallback;

@end
48 changes: 48 additions & 0 deletions ios/TelegramShare.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// TelegramShare.m.m
// RNShare
//
// Created by Akinn Rosa on 07-27-21.
//

#import "TelegramShare.h"
#import <AVFoundation/AVFoundation.h>
@import Photos;

@implementation TelegramShare
RCT_EXPORT_MODULE();
- (void)shareSingle:(NSDictionary *)options
failureCallback:(RCTResponseErrorBlock)failureCallback
successCallback:(RCTResponseSenderBlock)successCallback {

NSString *text = [RCTConvert NSString:options[@"message"]];
text = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) text, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));

NSString *url = [RCTConvert NSString:options[@"url"]];

NSString *telegramMsg = [NSString stringWithFormat:@"tg://msg?text=%@", text];
NSString *telegramMsgUrl = [NSString stringWithFormat:@"tg://msg_url?text=%@&url=%@", text, url];

NSString * urlTelegram = url ? telegramMsgUrl : telegramMsg;
NSURL * shareURL = [NSURL URLWithString:urlTelegram];

if ([[UIApplication sharedApplication] canOpenURL: shareURL]) {
[[UIApplication sharedApplication] openURL: shareURL];
successCallback(@[]);
} else {
// Cannot open telegram
NSString *stringURL = @"https://itunes.apple.com/app/telegram-messenger/id686449807";
NSURL *url = [NSURL URLWithString:stringURL];

[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}];

NSString *errorMessage = @"Not installed";
NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)};
NSError *error = [NSError errorWithDomain:@"com.rnshare" code:1 userInfo:userInfo];

NSLog(@"%@", errorMessage);
failureCallback(error);
}
}

@end
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@babel/runtime": "^7.11.2",
"@commitlint/cli": "11.0.0",
"@commitlint/config-conventional": "11.0.0",
"@react-native-community/eslint-config": "^3.0.0",
"@semantic-release/commit-analyzer": "^8.0.1",
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.1.1",
Expand Down
4 changes: 2 additions & 2 deletions website/docs/share-single.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ You can pass the option that will be handled by the native code, similar to `Sha
| **SNAPCHAT** || 🚫 | 🚫 |
| **MESSENGER** || 🚫 | 🚫 |
| **LINKEDIN** || 🚫 | 🚫 |
| **TELEGRAM** || 🚫 | 🚫 |
| **TELEGRAM** || | 🚫 |

## Static values for Instagram Stories

Expand Down Expand Up @@ -185,4 +185,4 @@ Share.shareSingle({
url: 'some share url',
social: Share.Social.TELEGRAM,
})
```
```

0 comments on commit 667171b

Please sign in to comment.