This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Similar behaviors for Android & iOS * Updated README with arguments type for callbacks
- Loading branch information
1 parent
3b9ac6a
commit be7eee9
Showing
8 changed files
with
258 additions
and
26 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# v1.0.0 (2017-04-04) | ||
|
||
First release. |
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,12 @@ | ||
{ | ||
"name": "cordova-plugin-in-app-youtube", | ||
"version": "1.0.0", | ||
"cordova": { | ||
"id": "cordova-plugin-in-app-youtube", | ||
"platforms": [ | ||
"android", | ||
"ios", | ||
] | ||
}, | ||
"description": "Open YouTube video in app" | ||
} |
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,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" | ||
id="cordova-plugin-in-app-youtube" | ||
version="1.0.0"> | ||
<name>Cordova Plugin in app YouTube</name> | ||
<description></description> | ||
<license>MIT</license> | ||
<keywords></keywords> | ||
<repo>https://github.com/davidefavia/cordova-plugin-in-app-youtube.git</repo> | ||
<issue>https://github.com/davidefavia/cordova-plugin-in-app-youtube/issues</issue> | ||
|
||
<!-- android --> | ||
<platform name="android"> | ||
<js-module src="www/plugin.js" name="plugin"> | ||
<runs/> | ||
<clobbers target="InAppYouTube" /> | ||
</js-module> | ||
<config-file target="res/xml/config.xml" parent="/*"> | ||
<feature name="InAppYouTube"> | ||
<param name="android-package" value="com.davidefavia.InAppYouTube" /> | ||
<param name="onload" value="true" /> | ||
</feature> | ||
</config-file> | ||
<source-file src="src/android/com/davidefavia/InAppYouTube.java" target-dir="src/com/davidefavia/" /> | ||
</platform> | ||
|
||
<!-- ios --> | ||
<platform name="ios"> | ||
<js-module src="www/plugin.js" name="plugin"> | ||
<runs/> | ||
<clobbers target="InAppYouTube" /> | ||
</js-module> | ||
<config-file target="config.xml" parent="/*"> | ||
<feature name="InAppYouTube"> | ||
<param name="ios-package" value="InAppYouTube" onload="true" /> | ||
</feature> | ||
</config-file> | ||
<!-- http://stackoverflow.com/a/34963435 --> | ||
<config-file platform="ios" target="*-Info.plist" parent="LSApplicationQueriesSchemes" mode="add"> | ||
<array> | ||
<string>youtube</string> | ||
</array> | ||
</config-file> | ||
<header-file src="src/ios/InAppYouTube.h" /> | ||
<source-file src="src/ios/InAppYouTube.m" /> | ||
</platform> | ||
</plugin> |
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,72 @@ | ||
/** | ||
*/ | ||
package com.davidefavia; | ||
|
||
import org.apache.cordova.CallbackContext; | ||
import org.apache.cordova.CordovaInterface; | ||
import org.apache.cordova.CordovaPlugin; | ||
import org.apache.cordova.CordovaWebView; | ||
import org.apache.cordova.PluginResult; | ||
import org.apache.cordova.PluginResult.Status; | ||
import org.json.JSONObject; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
|
||
import android.content.Intent; | ||
import android.net.Uri; | ||
import android.util.Log; | ||
import android.content.ActivityNotFoundException; | ||
|
||
import java.util.Date; | ||
|
||
public class InAppYouTube extends CordovaPlugin { | ||
private static final String TAG = "InAppYouTube"; | ||
|
||
public void initialize(CordovaInterface cordova, CordovaWebView webView) { | ||
super.initialize(cordova, webView); | ||
|
||
Log.d(TAG, "Initializing InAppYouTube"); | ||
} | ||
|
||
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { | ||
if(action.equals("openVideo")) { | ||
String videoId = args.getString(0); | ||
JSONObject options = args.getJSONObject(1); | ||
this.openVideo(videoId, options, callbackContext); | ||
} | ||
return true; | ||
} | ||
|
||
private void openVideo(String videoId, JSONObject options, CallbackContext callbackContext) { | ||
Boolean isFullScreen = options.optBoolean("fullscreen"); | ||
String webUrl; | ||
if(isFullScreen) { | ||
webUrl = "https://www.youtube.com/embed/" + videoId; | ||
} else { | ||
webUrl = "https://www.youtube.com/watch?v=" + videoId; | ||
} | ||
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + videoId)); | ||
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUrl)); | ||
Log.d(TAG, "fullscreen: " + String.valueOf(options.optBoolean("fullscreen"))); | ||
String type; | ||
try { | ||
appIntent.putExtra("force_fullscreen", isFullScreen); | ||
this.cordova.getActivity().startActivity(appIntent); | ||
type = "application"; | ||
} catch (ActivityNotFoundException ex) { | ||
this.cordova.getActivity().startActivity(webIntent); | ||
type = "webview"; | ||
} | ||
try { | ||
JSONObject json = new JSONObject(); | ||
json.put("videoId", videoId); | ||
json.put("options", options); | ||
json.put("type", type); | ||
callbackContext.success(json); | ||
} catch (JSONException e) { | ||
Log.e(TAG, e.toString()); | ||
callbackContext.error(e.toString()); | ||
} | ||
} | ||
|
||
} |
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,8 @@ | ||
#import <Cordova/CDVPlugin.h> | ||
|
||
@interface InAppYouTube : CDVPlugin { | ||
} | ||
|
||
- (void)openVideo:(CDVInvokedUrlCommand *)command; | ||
|
||
@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,47 @@ | ||
#import "InAppYouTube.h" | ||
|
||
#import <Cordova/CDVAvailability.h> | ||
|
||
@implementation InAppYouTube | ||
|
||
- (void)pluginInitialize { | ||
} | ||
|
||
- (void)openVideo:(CDVInvokedUrlCommand *)command { | ||
NSString* videoId = [command.arguments objectAtIndex:0]; | ||
NSDictionary* options = [command.arguments objectAtIndex:1]; | ||
BOOL isFullScreen = [options[@"fullscreen"] boolValue]; | ||
|
||
NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:@"youtube://watch?v=%@",videoId]]; | ||
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/watch?v=%@",videoId]]; | ||
if(isFullScreen) { | ||
linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/embed/%@",videoId]]; | ||
} | ||
|
||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; | ||
[dictionary setObject:videoId forKey:@"videoId"]; | ||
[dictionary setObject:options forKey:@"options"]; | ||
|
||
// http://stackoverflow.com/a/35409477 | ||
if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) { | ||
// @TODO Check for fullscreen | ||
// Can open the youtube app URL so launch the youTube app with this URL | ||
[[UIApplication sharedApplication] openURL:linkToAppURL]; | ||
[dictionary setObject:@"application" forKey:@"type"]; | ||
} else { | ||
// Can't open the youtube app URL so launch Safari instead | ||
// https://useyourloaf.com/blog/openurl-deprecated-in-ios10/ | ||
UIApplication *application = [UIApplication sharedApplication]; | ||
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) { | ||
[application openURL:linkToWebURL options:@{} completionHandler:nil]; | ||
} else { | ||
[application openURL:linkToWebURL]; | ||
} | ||
[dictionary setObject:@"webview" forKey:@"type"]; | ||
} | ||
|
||
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; | ||
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; | ||
} | ||
|
||
@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,13 @@ | ||
var exec = require('cordova/exec'); | ||
|
||
var PLUGIN_NAME = 'InAppYouTube'; | ||
|
||
var noop = function() {}; | ||
|
||
var InAppYouTube = { | ||
openVideo: function(videoId, options, successCallback, errorCallback) { | ||
exec(successCallback || noop, errorCallback || noop, PLUGIN_NAME, 'openVideo', [videoId, options || {}]); | ||
} | ||
}; | ||
|
||
module.exports = InAppYouTube; |