Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support discord share for android and ios (#1525)
Co-authored-by: liuhengkang <hengkang.liu@js.design>
- Loading branch information
Showing
9 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cl.json.social; | ||
|
||
import android.content.ActivityNotFoundException; | ||
import android.content.Intent; | ||
import java.io.File; | ||
import android.os.Environment; | ||
import android.net.Uri; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
|
||
public class DiscordShare extends SingleShareIntent { | ||
|
||
private static final String PACKAGE = "com.discord"; | ||
private static final String PLAY_STORE_LINK = "https://play.google.com/store/apps/details?id=com.discord"; | ||
|
||
public DiscordShare(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Override | ||
public void open(ReadableMap options) throws ActivityNotFoundException { | ||
super.open(options); | ||
// extra params here | ||
this.openIntentChooser(); | ||
} | ||
|
||
@Override | ||
protected String getPackage() { | ||
return PACKAGE; | ||
} | ||
|
||
@Override | ||
protected String getDefaultWebLink() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected String getPlayStoreLink() { | ||
return PLAY_STORE_LINK; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#import <UIKit/UIKit.h> | ||
// import RCTConvert | ||
#import <React/RCTConvert.h> | ||
// import RCTBridge | ||
#import <React/RCTBridge.h> | ||
// import RCTUIManager | ||
#import <React/RCTUIManager.h> | ||
// import RCTLog | ||
#import <React/RCTLog.h> | ||
// import RCTUtils | ||
#import <React/RCTUtils.h> | ||
@interface DiscordShare : NSObject <RCTBridgeModule> | ||
|
||
- (void) shareSingle:(NSDictionary *)options reject:(RCTPromiseRejectBlock)reject resolve:(RCTPromiseResolveBlock)resolve; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#import "DiscordShare.h" | ||
#import <AVFoundation/AVFoundation.h> | ||
@import Photos; | ||
|
||
@implementation DiscordShare | ||
RCT_EXPORT_MODULE(); | ||
- (void)shareSingle:(NSDictionary *)options | ||
reject:(RCTPromiseRejectBlock)reject | ||
resolve:(RCTPromiseResolveBlock)resolve { | ||
|
||
NSString *text = [RCTConvert NSString:options[@"message"]]; | ||
text = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) text, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8)); | ||
|
||
NSString *url = [RCTConvert NSString:options[@"url"]]; | ||
|
||
NSString *discordMsg = [NSString stringWithFormat:@"discord://message?text=%@", text]; | ||
NSString *discordMsgUrl = [NSString stringWithFormat:@"discord://message?text=%@&url%@", text, url]; | ||
|
||
NSString * urlDiscord = url ? discordMsgUrl : discordMsg; | ||
NSURL * shareURL = [NSURL URLWithString:urlDiscord]; | ||
|
||
|
||
if ([[UIApplication sharedApplication] canOpenURL: shareURL]) { | ||
[[UIApplication sharedApplication] openURL: shareURL]; | ||
resolve(@[@true, @""]); | ||
} else { | ||
NSString *stringURL = @"https://apps.apple.com/us/app/discord-chat-talk-hangout/id985746746"; | ||
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); | ||
reject(@"Not installed",@"Not installed",error); | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters