mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Merge v2.1.3-release into v2.2.0-release
# Conflicts: # cocos/ui/webview/WebViewImpl-ios.mm
This commit is contained in:
@@ -31,14 +31,23 @@ USING_NS_CC;
|
||||
|
||||
//-------------------------------------------------------------------------------------
|
||||
|
||||
#import <MediaPlayer/MediaPlayer.h>
|
||||
#import <AVKit/AVPlayerViewController.h>
|
||||
#import <CoreMedia/CMTime.h>
|
||||
#include "platform/CCApplication.h"
|
||||
#include "platform/ios/CCEAGLView-ios.h"
|
||||
#include "platform/CCFileUtils.h"
|
||||
|
||||
@interface UIVideoViewWrapperIos : NSObject<UIGestureRecognizerDelegate>
|
||||
@interface UIVideoViewWrapperIos : NSObject
|
||||
|
||||
@property (strong,nonatomic) MPMoviePlayerController * moviePlayer;
|
||||
typedef NS_ENUM(NSInteger, PlayerbackState) {
|
||||
PlayerbackStateUnknown = 0,
|
||||
PlayerbackStatePaused,
|
||||
PlayerbackStopped,
|
||||
PlayerbackStatePlaying,
|
||||
PlayerbackStateCompleted
|
||||
};
|
||||
|
||||
@property (strong, nonatomic) AVPlayerViewController * playerController;
|
||||
|
||||
- (void) setFrame:(int) left :(int) top :(int) width :(int) height;
|
||||
- (void) setURL:(int) videoSource :(std::string&) videoUrl;
|
||||
@@ -53,13 +62,12 @@ USING_NS_CC;
|
||||
- (void) setVisible:(BOOL) visible;
|
||||
- (void) setKeepRatioEnabled:(BOOL) enabled;
|
||||
- (void) setFullScreenEnabled:(BOOL) enabled;
|
||||
- (void) showPlaybackControls:(BOOL) value;
|
||||
- (BOOL) isFullScreenEnabled;
|
||||
- (void) cleanup;
|
||||
-(id) init:(void*) videoPlayer;
|
||||
|
||||
-(void) videoFinished:(NSNotification*) notification;
|
||||
-(void) playStateChange;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@@ -72,52 +80,29 @@ USING_NS_CC;
|
||||
bool _keepRatioEnabled;
|
||||
bool _fullscreen;
|
||||
CGRect _restoreRect;
|
||||
PlayerbackState _state;
|
||||
VideoPlayer* _videoPlayer;
|
||||
}
|
||||
|
||||
-(id)init:(void*)videoPlayer
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.moviePlayer = nullptr;
|
||||
self.playerController = [AVPlayerViewController new];
|
||||
|
||||
[self showPlaybackControls:TRUE];
|
||||
[self setKeepRatioEnabled:FALSE];
|
||||
|
||||
_videoPlayer = (VideoPlayer*)videoPlayer;
|
||||
_keepRatioEnabled = false;
|
||||
_state = PlayerbackStateUnknown;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
-(void) cleanup
|
||||
{
|
||||
if (self.moviePlayer != nullptr) {
|
||||
[self removeRegisteredObservers];
|
||||
|
||||
[self.moviePlayer stop];
|
||||
[self.moviePlayer.view removeFromSuperview];
|
||||
self.moviePlayer = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
-(void) removeRegisteredObservers {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
||||
name:MPMoviePlayerPlaybackDidFinishNotification
|
||||
object:self.moviePlayer];
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
||||
name:MPMoviePlayerPlaybackStateDidChangeNotification
|
||||
object:self.moviePlayer];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
||||
name:MPMovieDurationAvailableNotification
|
||||
object:nil];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
||||
name:MPMoviePlayerLoadStateDidChangeNotification
|
||||
object:nil];
|
||||
}
|
||||
|
||||
-(void) dealloc
|
||||
{
|
||||
[self cleanup];
|
||||
_videoPlayer = nullptr;
|
||||
[self.playerController release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
@@ -130,272 +115,179 @@ USING_NS_CC;
|
||||
_width = width;
|
||||
_top = top;
|
||||
_height = height;
|
||||
if (self.moviePlayer != nullptr) {
|
||||
_restoreRect = self.moviePlayer.view.frame;
|
||||
|
||||
if (_fullscreen)
|
||||
[self.moviePlayer.view setFrame:[[UIScreen mainScreen] bounds]];
|
||||
else
|
||||
[self.moviePlayer.view setFrame:CGRectMake(left, top, width, height)];
|
||||
}
|
||||
[self.playerController.view setFrame:CGRectMake(left, top, width, height)];
|
||||
}
|
||||
|
||||
-(void) setFullScreenEnabled:(BOOL) enabled
|
||||
{
|
||||
if (self.moviePlayer != nullptr) {
|
||||
_fullscreen = enabled;
|
||||
if (enabled)
|
||||
{
|
||||
[self.moviePlayer setFullscreen:enabled animated:(true)];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self setKeepRatioEnabled:_keepRatioEnabled];
|
||||
[self.moviePlayer.view setFrame:_restoreRect];
|
||||
}
|
||||
}
|
||||
// AVPlayerViewController doesn't provide API to enable fullscreen. But you can toggle
|
||||
// fullsreen by the playback controllers.
|
||||
}
|
||||
|
||||
-(BOOL) isFullScreenEnabled
|
||||
{
|
||||
if (self.moviePlayer != nullptr) {
|
||||
return [self.moviePlayer isFullscreen];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
-(BOOL) isPlaying
|
||||
{
|
||||
if(self.moviePlayer.playbackState == MPMoviePlaybackStatePlaying)
|
||||
return YES;
|
||||
else
|
||||
return NO;
|
||||
return (_state == PlayerbackState::PlayerbackStatePlaying);
|
||||
}
|
||||
|
||||
-(void) setURL:(int)videoSource :(std::string &)videoUrl
|
||||
{
|
||||
[self cleanup];
|
||||
|
||||
if (videoSource == 1) {
|
||||
self.moviePlayer = [[[MPMoviePlayerController alloc] init] autorelease];
|
||||
self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
|
||||
[self.moviePlayer setContentURL:[NSURL URLWithString:@(videoUrl.c_str())]];
|
||||
[self.moviePlayer prepareToPlay];
|
||||
} else {
|
||||
self.moviePlayer = [[[MPMoviePlayerController alloc] init] autorelease];
|
||||
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
|
||||
[self.moviePlayer setContentURL:[NSURL fileURLWithPath:@(videoUrl.c_str())]];
|
||||
[self.moviePlayer prepareToPlay];
|
||||
}
|
||||
self.moviePlayer.allowsAirPlay = NO;
|
||||
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
|
||||
self.moviePlayer.view.userInteractionEnabled = YES;
|
||||
if (videoSource == 1)
|
||||
self.playerController.player = [[[AVPlayer alloc] initWithURL:[NSURL URLWithString:@(videoUrl.c_str())]] autorelease];
|
||||
else
|
||||
self.playerController.player = [[[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:@(videoUrl.c_str())]] autorelease];
|
||||
|
||||
auto clearColor = [UIColor clearColor];
|
||||
self.moviePlayer.backgroundView.backgroundColor = clearColor;
|
||||
self.moviePlayer.view.backgroundColor = clearColor;
|
||||
for (UIView * subView in self.moviePlayer.view.subviews) {
|
||||
subView.backgroundColor = clearColor;
|
||||
}
|
||||
|
||||
if (_keepRatioEnabled) {
|
||||
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
|
||||
} else {
|
||||
self.moviePlayer.scalingMode = MPMovieScalingModeFill;
|
||||
}
|
||||
[self setKeepRatioEnabled:_keepRatioEnabled];
|
||||
[self showPlaybackControls:TRUE];
|
||||
|
||||
auto eaglview = (CCEAGLView*)cocos2d::Application::getInstance()->getView();
|
||||
[eaglview addSubview:self.moviePlayer.view];
|
||||
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(videoFinished:)
|
||||
name:MPMoviePlayerPlaybackDidFinishNotification
|
||||
object:self.moviePlayer];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(playStateChange)
|
||||
name:MPMoviePlayerPlaybackStateDidChangeNotification
|
||||
object:self.moviePlayer];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(metadataUpdate:)
|
||||
name:MPMovieDurationAvailableNotification
|
||||
object:nil];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(loadStateUpdate:)
|
||||
name:MPMoviePlayerLoadStateDidChangeNotification
|
||||
object:nil];
|
||||
|
||||
|
||||
//The setup code (in viewDidLoad in your view controller)
|
||||
UITapGestureRecognizer *singleFingerTap =
|
||||
[[UITapGestureRecognizer alloc] initWithTarget:self
|
||||
action:@selector(handleSingleTap:)];
|
||||
|
||||
[self.moviePlayer.view addGestureRecognizer:singleFingerTap];
|
||||
|
||||
singleFingerTap.delegate = self;
|
||||
[singleFingerTap release];
|
||||
|
||||
}
|
||||
|
||||
// this enables you to handle multiple recognizers on single view
|
||||
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
|
||||
return YES;
|
||||
}
|
||||
|
||||
//The event handling method
|
||||
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::CLICKED);
|
||||
}
|
||||
|
||||
// this allows you to dispatch touches
|
||||
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
|
||||
return YES;
|
||||
}
|
||||
|
||||
-(void) videoFinished:(NSNotification *)notification
|
||||
{
|
||||
if(_videoPlayer != nullptr)
|
||||
{
|
||||
if([self.moviePlayer playbackState] != MPMoviePlaybackStateStopped)
|
||||
{
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::COMPLETED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-(void) playStateChange
|
||||
{
|
||||
MPMoviePlaybackState state = [self.moviePlayer playbackState];
|
||||
switch (state) {
|
||||
case MPMoviePlaybackStatePlaying:
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
-(void) loadStateUpdate:(NSNotification*)notification
|
||||
{
|
||||
MPMovieLoadState state = [self.moviePlayer loadState];
|
||||
if(state == MPMovieLoadStatePlayable ||
|
||||
state == MPMovieLoadStatePlaythroughOK) {
|
||||
MPMoviePlaybackState playState = [self.moviePlayer playbackState];
|
||||
|
||||
if (playState != MPMoviePlaybackStatePlaying)
|
||||
[self.moviePlayer pause];
|
||||
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::READY_TO_PLAY);
|
||||
}
|
||||
}
|
||||
|
||||
-(void) metadataUpdate:(NSNotification *)notification
|
||||
{
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::META_LOADED);
|
||||
[eaglview addSubview:self.playerController.view];
|
||||
[self registerPlayerEventListener];
|
||||
}
|
||||
|
||||
-(void) seekTo:(float)sec
|
||||
{
|
||||
if (self.moviePlayer != NULL) {
|
||||
[self.moviePlayer setCurrentPlaybackTime:(sec)];
|
||||
}
|
||||
if (self.playerController.player)
|
||||
[self.playerController.player seekToTime:CMTimeMake(sec, 1)];
|
||||
}
|
||||
|
||||
-(float) currentTime
|
||||
{
|
||||
if (self.moviePlayer != NULL) {
|
||||
float ret = [self.moviePlayer currentPlaybackTime];
|
||||
if (!isnan(ret)) return ret;
|
||||
}
|
||||
if (self.playerController.player)
|
||||
return CMTimeGetSeconds([self.playerController.player currentTime]);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
-(float) duration
|
||||
{
|
||||
float duration = -1;
|
||||
if (self.moviePlayer != NULL) {
|
||||
duration = [self.moviePlayer duration];
|
||||
if(duration <= 0) {
|
||||
CCLOG("Video player's duration is not ready to get now!");
|
||||
}
|
||||
}
|
||||
return duration;
|
||||
if (self.playerController.player)
|
||||
return CMTimeGetSeconds(self.playerController.player.currentItem.asset.duration);
|
||||
|
||||
return -1;;
|
||||
}
|
||||
|
||||
-(void) setVisible:(BOOL)visible
|
||||
{
|
||||
if (self.moviePlayer != NULL) {
|
||||
[self.moviePlayer.view setHidden:!visible];
|
||||
if (!visible) {
|
||||
[self pause];
|
||||
}
|
||||
}
|
||||
[self.playerController.view setHidden:!visible];
|
||||
if (!visible)
|
||||
[self pause];
|
||||
}
|
||||
|
||||
-(void) setKeepRatioEnabled:(BOOL)enabled
|
||||
{
|
||||
_keepRatioEnabled = enabled;
|
||||
if (self.moviePlayer != NULL) {
|
||||
if (_keepRatioEnabled) {
|
||||
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
|
||||
} else {
|
||||
self.moviePlayer.scalingMode = MPMovieScalingModeFill;
|
||||
}
|
||||
}
|
||||
if (_keepRatioEnabled)
|
||||
self.playerController.videoGravity = AVLayerVideoGravityResizeAspect;
|
||||
else
|
||||
self.playerController.videoGravity = AVLayerVideoGravityResizeAspectFill;
|
||||
}
|
||||
|
||||
-(void) play
|
||||
{
|
||||
if (self.moviePlayer != NULL) {
|
||||
if (_fullscreen)
|
||||
{
|
||||
self.moviePlayer.scalingMode = MPMovieScalingModeFill;
|
||||
[self.moviePlayer.view setFrame:[[UIScreen mainScreen] bounds]];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self setKeepRatioEnabled:_keepRatioEnabled];
|
||||
[self.moviePlayer.view setFrame:CGRectMake(_left, _top, _width, _height)];
|
||||
}
|
||||
|
||||
[self.moviePlayer play];
|
||||
if (self.playerController.player && _state != PlayerbackStatePlaying) {
|
||||
[self.playerController.player play];
|
||||
_state = PlayerbackStatePlaying;
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
|
||||
}
|
||||
}
|
||||
|
||||
-(void) pause
|
||||
{
|
||||
if (self.moviePlayer != NULL) {
|
||||
if([self.moviePlayer playbackState] == MPMoviePlaybackStatePlaying) {
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
|
||||
}
|
||||
|
||||
[self.moviePlayer pause];
|
||||
if (self.playerController.player && _state == PlayerbackStatePlaying) {
|
||||
[self.playerController.player pause];
|
||||
_state = PlayerbackStatePaused;
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
|
||||
}
|
||||
}
|
||||
|
||||
-(void) resume
|
||||
{
|
||||
if (self.moviePlayer != NULL) {
|
||||
if([self.moviePlayer playbackState] == MPMoviePlaybackStatePaused)
|
||||
{
|
||||
[self.moviePlayer play];
|
||||
}
|
||||
}
|
||||
if (self.playerController.player && _state == PlayerbackStatePaused)
|
||||
[self play];
|
||||
}
|
||||
|
||||
-(void) stop
|
||||
{
|
||||
if (self.moviePlayer != NULL) {
|
||||
[self.moviePlayer pause];
|
||||
// AVPlayer doesn't have stop, so just pause it, and seek time to 0.
|
||||
if (self.playerController.player && _state != PlayerbackStopped) {
|
||||
[self seekTo:0];
|
||||
[self.playerController.player pause];
|
||||
_state = PlayerbackStopped;
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::STOPPED);
|
||||
}
|
||||
}
|
||||
|
||||
// Private functions
|
||||
|
||||
-(void) cleanup
|
||||
{
|
||||
[self stop];
|
||||
[self removePlayerEventListener];
|
||||
[self.playerController.view removeFromSuperview];
|
||||
}
|
||||
|
||||
-(void) removePlayerEventListener {
|
||||
if (self.playerController.player)
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
||||
name:AVPlayerItemDidPlayToEndTimeNotification
|
||||
object:self.playerController.player.currentItem];
|
||||
|
||||
[self.playerController.player removeObserver:self forKeyPath:@"status"];
|
||||
}
|
||||
}
|
||||
|
||||
-(void) registerPlayerEventListener
|
||||
{
|
||||
if (self.playerController.player)
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(videoFinished:)
|
||||
name:AVPlayerItemDidPlayToEndTimeNotification
|
||||
object:self.playerController.player.currentItem];
|
||||
|
||||
[self.playerController.player addObserver:self forKeyPath:@"status" options:0 context:nil];
|
||||
}
|
||||
}
|
||||
|
||||
-(void) showPlaybackControls:(BOOL)value
|
||||
{
|
||||
self.playerController.showsPlaybackControls = value;
|
||||
}
|
||||
|
||||
-(void) videoFinished:(NSNotification *)notification
|
||||
{
|
||||
if(_videoPlayer != nullptr) {
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::COMPLETED);
|
||||
_state = PlayerbackStateCompleted;
|
||||
|
||||
// Seek to 0 to make it playable again.
|
||||
[self seekTo:0];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
|
||||
change:(NSDictionary *)change context:(void *)context {
|
||||
|
||||
auto player = self.playerController.player;
|
||||
if (object == player && [keyPath isEqualToString:@"status"]) {
|
||||
if (player.status == AVPlayerStatusReadyToPlay) {
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::META_LOADED);
|
||||
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::READY_TO_PLAY);
|
||||
} else if (player.status == AVPlayerStatusFailed) {
|
||||
// something went wrong. player.error should contain some information
|
||||
NSLog(@"Failed to load video");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
//------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -28,35 +28,15 @@
|
||||
// Webview not available on tvOS
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
|
||||
|
||||
#import <WebKit/WKWebView.h>
|
||||
#import <WebKit/WKUIDelegate.h>
|
||||
#import <WebKit/WKNavigationDelegate.h>
|
||||
|
||||
#include "WebView-inl.h"
|
||||
#include "platform/CCApplication.h"
|
||||
#include "platform/ios/CCEAGLView-ios.h"
|
||||
#include "platform/CCFileUtils.h"
|
||||
|
||||
static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||
{
|
||||
std::string fixedBaseUrl;
|
||||
if (baseUrl.empty() || baseUrl.at(0) != '/') {
|
||||
fixedBaseUrl = [[[NSBundle mainBundle] resourcePath] UTF8String];
|
||||
fixedBaseUrl += "/";
|
||||
fixedBaseUrl += baseUrl;
|
||||
}
|
||||
else {
|
||||
fixedBaseUrl = baseUrl;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
while ((pos = fixedBaseUrl.find(" ")) != std::string::npos) {
|
||||
fixedBaseUrl.replace(pos, 1, "%20");
|
||||
}
|
||||
|
||||
if (fixedBaseUrl.at(fixedBaseUrl.length() - 1) != '/') {
|
||||
fixedBaseUrl += "/";
|
||||
}
|
||||
|
||||
return fixedBaseUrl;
|
||||
}
|
||||
|
||||
@interface UIWebViewWrapper : NSObject
|
||||
@property (nonatomic) std::function<bool(std::string url)> shouldStartLoading;
|
||||
@property (nonatomic) std::function<void(std::string url)> didFinishLoading;
|
||||
@@ -100,8 +80,8 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||
@end
|
||||
|
||||
|
||||
@interface UIWebViewWrapper () <UIWebViewDelegate>
|
||||
@property(nonatomic, retain) UIWebView *uiWebView;
|
||||
@interface UIWebViewWrapper () <WKUIDelegate, WKNavigationDelegate>
|
||||
@property(nonatomic, retain) WKWebView *uiWebView;
|
||||
@property(nonatomic, copy) NSString *jsScheme;
|
||||
@end
|
||||
|
||||
@@ -125,7 +105,7 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
self.uiWebView.delegate = nil;
|
||||
self.uiWebView.UIDelegate = nil;
|
||||
[self.uiWebView removeFromSuperview];
|
||||
self.uiWebView = nil;
|
||||
self.jsScheme = nil;
|
||||
@@ -134,10 +114,10 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||
|
||||
- (void)setupWebView {
|
||||
if (!self.uiWebView) {
|
||||
self.uiWebView = [[[UIWebView alloc] init] autorelease];
|
||||
self.uiWebView.delegate = self;
|
||||
self.uiWebView = [[[WKWebView alloc] init] autorelease];
|
||||
self.uiWebView.UIDelegate = self;
|
||||
self.uiWebView.navigationDelegate = self;
|
||||
}
|
||||
self.uiWebView.allowsInlineMediaPlayback = YES;
|
||||
if (!self.uiWebView.superview) {
|
||||
auto eaglview = (CCEAGLView*)cocos2d::Application::getInstance()->getView();
|
||||
[eaglview addSubview:self.uiWebView];
|
||||
@@ -165,15 +145,22 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||
}
|
||||
|
||||
- (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL {
|
||||
auto path = [[NSBundle mainBundle] resourcePath];
|
||||
path = [path stringByAppendingPathComponent:@(baseURL.c_str() )];
|
||||
auto url = [NSURL fileURLWithPath:path];
|
||||
|
||||
[self.uiWebView loadData:[NSData dataWithBytes:data.c_str() length:data.length()]
|
||||
MIMEType:@(MIMEType.c_str())
|
||||
textEncodingName:@(encodingName.c_str())
|
||||
baseURL:[NSURL URLWithString:@(getFixedBaseUrl(baseURL).c_str())]];
|
||||
characterEncodingName:@(encodingName.c_str())
|
||||
baseURL:url];
|
||||
}
|
||||
|
||||
- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL {
|
||||
if (!self.uiWebView) {[self setupWebView];}
|
||||
[self.uiWebView loadHTMLString:@(string.c_str()) baseURL:[NSURL URLWithString:@(getFixedBaseUrl(baseURL).c_str())]];
|
||||
auto path = [[NSBundle mainBundle] resourcePath];
|
||||
path = [path stringByAppendingPathComponent:@(baseURL.c_str() )];
|
||||
auto url = [NSURL fileURLWithPath:path];
|
||||
[self.uiWebView loadHTMLString:@(string.c_str()) baseURL:url];
|
||||
}
|
||||
|
||||
- (void)loadUrl:(const std::string &)urlString {
|
||||
@@ -216,12 +203,13 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||
|
||||
- (void)evaluateJS:(const std::string &)js {
|
||||
if (!self.uiWebView) {[self setupWebView];}
|
||||
[self.uiWebView stringByEvaluatingJavaScriptFromString:@(js.c_str())];
|
||||
[self.uiWebView evaluateJavaScript:@(js.c_str()) completionHandler:nil];
|
||||
}
|
||||
|
||||
- (void)setScalesPageToFit:(const bool)scalesPageToFit {
|
||||
if (!self.uiWebView) {[self setupWebView];}
|
||||
self.uiWebView.scalesPageToFit = scalesPageToFit;
|
||||
// TODO: there is not corresponding API in WK.
|
||||
// https://stackoverflow.com/questions/26295277/wkwebview-equivalent-for-uiwebviews-scalespagetofit/43048514 seems has a solution,
|
||||
// but it doesn't support setting it dynamically. If we want to set this feature dynamically, then it will be too complex.
|
||||
}
|
||||
|
||||
- (void)setBackgroundTransparent:(const bool)isTransparent {
|
||||
@@ -230,35 +218,60 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||
[self.uiWebView setBackgroundColor:isTransparent ? [UIColor clearColor] : [UIColor whiteColor]];
|
||||
}
|
||||
|
||||
#pragma mark - UIWebViewDelegate
|
||||
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
|
||||
NSString *url = [[request URL] absoluteString];
|
||||
if ([[[request URL] scheme] isEqualToString:self.jsScheme]) {
|
||||
#pragma mark - WKNavigationDelegate
|
||||
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
|
||||
NSString *url = [webView.URL absoluteString];
|
||||
if ([[webView.URL scheme] isEqualToString:self.jsScheme]) {
|
||||
self.onJsCallback([url UTF8String]);
|
||||
return NO;
|
||||
decisionHandler(WKNavigationActionPolicyCancel);
|
||||
return;
|
||||
}
|
||||
if (self.shouldStartLoading && url) {
|
||||
return self.shouldStartLoading([url UTF8String]);
|
||||
if (self.shouldStartLoading([url UTF8String]) )
|
||||
decisionHandler(WKNavigationActionPolicyAllow);
|
||||
else
|
||||
decisionHandler(WKNavigationActionPolicyCancel);
|
||||
|
||||
return;
|
||||
}
|
||||
return YES;
|
||||
|
||||
decisionHandler(WKNavigationActionPolicyAllow);
|
||||
}
|
||||
|
||||
- (void)webViewDidFinishLoad:(UIWebView *)webView {
|
||||
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
|
||||
if (self.didFinishLoading) {
|
||||
NSString *url = [[webView.request URL] absoluteString];
|
||||
NSString *url = [webView.URL absoluteString];
|
||||
self.didFinishLoading([url UTF8String]);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
|
||||
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
|
||||
if (self.didFailLoading) {
|
||||
NSString *url = error.userInfo[NSURLErrorFailingURLStringErrorKey];
|
||||
if (url) {
|
||||
self.didFailLoading([url UTF8String]);
|
||||
NSString *errorInfo = error.userInfo[NSURLErrorFailingURLStringErrorKey];
|
||||
if (errorInfo) {
|
||||
self.didFailLoading([errorInfo UTF8String]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma WKUIDelegate
|
||||
|
||||
// Implement js alert function.
|
||||
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)())completionHandler
|
||||
{
|
||||
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message
|
||||
message:nil
|
||||
preferredStyle:UIAlertControllerStyleAlert];
|
||||
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok"
|
||||
style:UIAlertActionStyleCancel
|
||||
handler:^(UIAlertAction *action) {
|
||||
completionHandler();
|
||||
}]];
|
||||
|
||||
auto rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
|
||||
[rootViewController presentViewController:alertController animated:YES completion:^{}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -372,7 +385,7 @@ namespace cocos2d {
|
||||
width:width/scaleFactor
|
||||
height:height/scaleFactor];
|
||||
}
|
||||
|
||||
|
||||
void WebViewImpl::setBackgroundTransparent(bool isTransparent){
|
||||
[_uiWebViewWrapper setBackgroundTransparent:isTransparent];
|
||||
}
|
||||
|
||||
+4
@@ -30,6 +30,7 @@
|
||||
4033CDC120E493CF00DAA37B /* jsb-adapter in Resources */ = {isa = PBXBuildFile; fileRef = 4033CDBF20E493CF00DAA37B /* jsb-adapter */; };
|
||||
40CEC422212BDAB0004BCE66 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 40CEC424212BDAB0004BCE66 /* LaunchScreen.storyboard */; };
|
||||
40CEC429212BDB03004BCE66 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 40CEC42B212BDB03004BCE66 /* Localizable.strings */; };
|
||||
467AF6052321FBD400770112 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 467AF6042321FBD400770112 /* WebKit.framework */; };
|
||||
4D3EB2581F8F4A6C007DA644 /* jsb_module_register.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4D3EB2571F8F4A6C007DA644 /* jsb_module_register.cpp */; };
|
||||
4D3EB26B1F8F77C9007DA644 /* jsb_module_register.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4D3EB2571F8F4A6C007DA644 /* jsb_module_register.cpp */; };
|
||||
4D697BDE1CBD303100A5AF29 /* libjs_static.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D697BDC1CBD303100A5AF29 /* libjs_static.a */; };
|
||||
@@ -137,6 +138,7 @@
|
||||
40CEC425212BDAC1004BCE66 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
|
||||
40CEC42C212BDB19004BCE66 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
|
||||
40CEC42D212BDB36004BCE66 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = "<group>"; };
|
||||
467AF6042321FBD400770112 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.2.sdk/System/Library/Frameworks/WebKit.framework; sourceTree = DEVELOPER_DIR; };
|
||||
4D3EB2571F8F4A6C007DA644 /* jsb_module_register.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_module_register.cpp; sourceTree = "<group>"; };
|
||||
4D697BD91CBD301C00A5AF29 /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjs_static.a; path = "../../cocos2d-x/external/ios/libs/libjs_static.a"; sourceTree = "<group>"; };
|
||||
4D697BDC1CBD303100A5AF29 /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjs_static.a; path = "../../cocos2d-x/external/mac/libs/libjs_static.a"; sourceTree = "<group>"; };
|
||||
@@ -216,6 +218,7 @@
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
467AF6052321FBD400770112 /* WebKit.framework in Frameworks */,
|
||||
1A37E9D4200DEEF40078AF72 /* SystemConfiguration.framework in Frameworks */,
|
||||
1AC62C111F4D1E7400F43CB1 /* JavaScriptCore.framework in Frameworks */,
|
||||
1A499EA91F0CD97F004E6AEC /* libicucore.A.tbd in Frameworks */,
|
||||
@@ -316,6 +319,7 @@
|
||||
A92275401517C094001B78AA /* Frameworks */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
467AF6042321FBD400770112 /* WebKit.framework */,
|
||||
1A37E9D3200DEEF40078AF72 /* SystemConfiguration.framework */,
|
||||
1A37E9D5200DEEFB0078AF72 /* SystemConfiguration.framework */,
|
||||
1AC62C121F4D1E7F00F43CB1 /* JavaScriptCore.framework */,
|
||||
|
||||
+4
@@ -27,6 +27,7 @@
|
||||
4011F9D5212BD7050091CC5B /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4011F9D7212BD7050091CC5B /* Localizable.strings */; };
|
||||
4033CDBC20E4935100DAA37B /* jsb-adapter in Resources */ = {isa = PBXBuildFile; fileRef = 4033CDBB20E4935000DAA37B /* jsb-adapter */; };
|
||||
4033CDBD20E4935100DAA37B /* jsb-adapter in Resources */ = {isa = PBXBuildFile; fileRef = 4033CDBB20E4935000DAA37B /* jsb-adapter */; };
|
||||
467AF6082321FBED00770112 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 467AF6072321FBED00770112 /* WebKit.framework */; };
|
||||
4DD5E6AB1CDAF6430046171E /* libcocos2d Mac.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DD0DB561CDAF5F40099AD86 /* libcocos2d Mac.a */; };
|
||||
4DD5E6B11CDAF65C0046171E /* libcocos2d iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DD0DB581CDAF5F40099AD86 /* libcocos2d iOS.a */; };
|
||||
502380DC17EBB88200990C9B /* libcurl.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 502380DB17EBB88200990C9B /* libcurl.dylib */; };
|
||||
@@ -132,6 +133,7 @@
|
||||
4011F9D8212BD7150091CC5B /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = "<group>"; };
|
||||
4011F9D9212BD73A0091CC5B /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
|
||||
4033CDBB20E4935000DAA37B /* jsb-adapter */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "jsb-adapter"; path = "../../../jsb-adapter"; sourceTree = "<group>"; };
|
||||
467AF6072321FBED00770112 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.2.sdk/System/Library/Frameworks/WebKit.framework; sourceTree = DEVELOPER_DIR; };
|
||||
4D697BD91CBD301C00A5AF29 /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjs_static.a; path = "/Applications/CocosCreator.app/Contents/Resources/cocos2d-x/external/ios/libs/libjs_static.a"; sourceTree = "<absolute>"; };
|
||||
4D697BDC1CBD303100A5AF29 /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjs_static.a; path = "/Applications/CocosCreator.app/Contents/Resources/cocos2d-x/external/mac/libs/libjs_static.a"; sourceTree = "<absolute>"; };
|
||||
502380DB17EBB88200990C9B /* libcurl.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libcurl.dylib; path = usr/lib/libcurl.dylib; sourceTree = SDKROOT; };
|
||||
@@ -208,6 +210,7 @@
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
467AF6082321FBED00770112 /* WebKit.framework in Frameworks */,
|
||||
1A37E9DA200DEF120078AF72 /* SystemConfiguration.framework in Frameworks */,
|
||||
1ACA99251F550A28002DE225 /* JavaScriptCore.framework in Frameworks */,
|
||||
1A499EBD1F0CDDD9004E6AEC /* libicucore.A.tbd in Frameworks */,
|
||||
@@ -308,6 +311,7 @@
|
||||
A92275401517C094001B78AA /* Frameworks */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
467AF6072321FBED00770112 /* WebKit.framework */,
|
||||
1A37E9D9200DEF110078AF72 /* SystemConfiguration.framework */,
|
||||
1A37E9DB200DEF190078AF72 /* SystemConfiguration.framework */,
|
||||
1ACA99241F550A28002DE225 /* JavaScriptCore.framework */,
|
||||
|
||||
Reference in New Issue
Block a user