Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Remove iOS9 deprecated interface usage. (#2643)
Browse files Browse the repository at this point in the history
  • Loading branch information
wqyfavor authored and jianhan-he committed Jul 3, 2019
1 parent 29a600a commit 3768c8f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 161 deletions.
146 changes: 35 additions & 111 deletions ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.m
Expand Up @@ -25,8 +25,6 @@

#import <AVFoundation/AVPlayer.h>
#import <AVKit/AVPlayerViewController.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>
#import <MediaPlayer/MPMoviePlayerController.h>
#import <AVFoundation/AVPlayerItem.h>

@interface WXPlayer : NSObject
Expand All @@ -53,40 +51,7 @@ @implementation WXVideoView
- (id)init
{
if (self = [super init]) {
if ([self greater8SysVer]) {
_playerViewController = [AVPlayerViewController new];

} else {
_playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:nil];
MPMoviePlayerViewController *MPVC = (MPMoviePlayerViewController*)_playerViewController;
MPVC.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
MPVC.moviePlayer.shouldAutoplay = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:MPVC.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackStateDidChangeNotification object:MPVC.moviePlayer queue:nil usingBlock:^(NSNotification *notification)
{
if (MPVC.moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {
if (_playbackStateChanged)
_playbackStateChanged(WXPlaybackStatePlaying);
}
if (MPVC.moviePlayer.playbackState == MPMoviePlaybackStateStopped) {
//stopped
} if (MPVC.moviePlayer.playbackState == MPMoviePlaybackStatePaused) {
//paused
if (_playbackStateChanged) {
_playbackStateChanged(WXPlaybackStatePaused);
}
} if (MPVC.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted) {
//interrupted
} if (MPVC.moviePlayer.playbackState == MPMoviePlaybackStateSeekingForward) {
//seeking forward
} if (MPVC.moviePlayer.playbackState == MPMoviePlaybackStateSeekingBackward) {
//seeking backward
}
}];
}
_playerViewController = [AVPlayerViewController new];

[self addSubview:_playerViewController.view];
}
Expand All @@ -96,23 +61,11 @@ - (id)init
- (void)dealloc
{
_weexSDKInstance = nil;
if ([self greater8SysVer]) {
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
[AVVC.player removeObserver:self forKeyPath:@"rate"];
[_playerItem removeObserver:self forKeyPath:@"status"];

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object: _playerItem];
}
else {
MPMoviePlayerViewController *MPVC = (MPMoviePlayerViewController*)_playerViewController;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:MPVC.moviePlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:MPVC.moviePlayer];
}
}

- (BOOL)greater8SysVer
{
return YES;
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
[AVVC.player removeObserver:self forKeyPath:@"rate"];
[_playerItem removeObserver:self forKeyPath:@"status"];

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object: _playerItem];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
Expand Down Expand Up @@ -159,34 +112,27 @@ - (void)setURL:(NSURL *)URL
return;
}
NSURL *videoNewURL = [NSURL URLWithString:newURL];
if ([self greater8SysVer]) {

AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
if (AVVC.player && _playerItem) {
[_playerItem removeObserver:self forKeyPath:@"status"];
[AVVC.player removeObserver:self forKeyPath:@"rate"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object: _playerItem];
}
_playerItem = [[AVPlayerItem alloc] initWithURL:videoNewURL];
AVPlayer *player = [AVPlayer playerWithPlayerItem: _playerItem];
AVVC.player = player;

[player addObserver:self
forKeyPath:@"rate"
options:NSKeyValueObservingOptionNew
context:NULL];

[_playerItem addObserver:self
forKeyPath:@"status"
options:NSKeyValueObservingOptionNew
context:NULL];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinish) name:AVPlayerItemDidPlayToEndTimeNotification object: _playerItem];
}
else {
MPMoviePlayerViewController *MPVC = (MPMoviePlayerViewController*)_playerViewController;
[MPVC moviePlayer].contentURL = videoNewURL;
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
if (AVVC.player && _playerItem) {
[_playerItem removeObserver:self forKeyPath:@"status"];
[AVVC.player removeObserver:self forKeyPath:@"rate"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object: _playerItem];
}
_playerItem = [[AVPlayerItem alloc] initWithURL:videoNewURL];
AVPlayer *player = [AVPlayer playerWithPlayerItem: _playerItem];
AVVC.player = player;

[player addObserver:self
forKeyPath:@"rate"
options:NSKeyValueObservingOptionNew
context:NULL];

[_playerItem addObserver:self
forKeyPath:@"status"
options:NSKeyValueObservingOptionNew
context:NULL];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinish) name:AVPlayerItemDidPlayToEndTimeNotification object: _playerItem];
}

- (void)setPosterURL:(NSURL *)posterURL
Expand All @@ -213,52 +159,30 @@ - (void)setPosterURL:(NSURL *)posterURL

- (void)setControlShow:(BOOL)showControl
{
if ([self greater8SysVer]) {
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
AVVC.showsPlaybackControls = showControl;
}
else
{
MPMoviePlayerViewController *MPVC = (MPMoviePlayerViewController*)_playerViewController;
MPVC.moviePlayer.controlStyle = showControl ? MPMovieControlStyleEmbedded : MPMovieControlStyleNone;
}
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
AVVC.showsPlaybackControls = showControl;
}

- (void)playFinish
{
if (_playbackStateChanged)
_playbackStateChanged(WXPlaybackStatePlayFinish);
if ([self greater8SysVer]) {
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
[[AVVC player] seekToTime:CMTimeMultiply([AVVC player].currentTime, 0)];
} else {
MPMoviePlayerViewController *MPVC = (MPMoviePlayerViewController*)_playerViewController;
[[MPVC moviePlayer] stop];
}

AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
[[AVVC player] seekToTime:CMTimeMultiply([AVVC player].currentTime, 0)];
}

- (void)play
{
_posterImageView.hidden = YES;
if ([self greater8SysVer]) {
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;

[[AVVC player] play];
} else {
MPMoviePlayerViewController *MPVC = (MPMoviePlayerViewController*)_playerViewController;
[[MPVC moviePlayer] play];
}
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
[[AVVC player] play];
}

- (void)pause
{
if ([self greater8SysVer]) {
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
[[AVVC player] pause];
} else {
MPMoviePlayerViewController *MPVC = (MPMoviePlayerViewController*)_playerViewController;
[[MPVC moviePlayer] pause];
}
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;
[[AVVC player] pause];
}

- (void)posterTapHandler {
Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
Expand Up @@ -930,7 +930,7 @@ - (NSURL *)completeURL:(NSString *)url
return result;
}
// if result is nil, try url-encode the 'url' string.
return [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] relativeToURL:_scriptURL];
return [NSURL URLWithString:[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] relativeToURL:_scriptURL];
}

- (BOOL)checkModuleEventRegistered:(NSString*)event moduleClassName:(NSString*)moduleClassName
Expand Down
114 changes: 65 additions & 49 deletions ios/sdk/WeexSDK/Sources/Module/WXModalUIModule.m
Expand Up @@ -22,8 +22,6 @@
#import "WXUtility.h"
#import "WXAssert.h"

static NSString *WXModalCallbackKey;

typedef enum : NSUInteger {
WXModalTypeToast = 1,
WXModalTypeAlert,
Expand Down Expand Up @@ -72,9 +70,6 @@ @interface WXModalUIModule () <UIAlertViewDelegate>
@end

@implementation WXModalUIModule
{
NSMutableSet *_alertViews;
}

@synthesize weexInstance;

Expand All @@ -86,15 +81,13 @@ @implementation WXModalUIModule
- (instancetype)init
{
if (self = [super init]) {
_alertViews = [NSMutableSet setWithCapacity:1];
}

return self;
}

- (void)dealloc
{
[_alertViews removeAllObjects];
}

#pragma mark - Toast
Expand Down Expand Up @@ -293,7 +286,6 @@ - (void)prompt:(NSDictionary *)param callback:(WXModuleKeepAliveCallback)callbac
[self alert:message okTitle:okTitle cancelTitle:cancelTitle defaultText:defaultValue type:WXModalTypePrompt callback:callback];
}


#pragma mark - Private

- (void)alert:(NSString *)message okTitle:(NSString *)okTitle cancelTitle:(NSString *)cancelTitle defaultText:(NSString *)defaultText type:(WXModalType)type callback:(WXModuleKeepAliveCallback)callback
Expand All @@ -305,50 +297,74 @@ - (void)alert:(NSString *)message okTitle:(NSString *)okTitle cancelTitle:(NSStr
return;
}

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:okTitle, nil];
alertView.tag = type;
if (type == WXModalTypePrompt) {
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alertView textFieldAtIndex:0];
textField.placeholder = defaultText;
}
objc_setAssociatedObject(alertView, &WXModalCallbackKey, [callback copy], OBJC_ASSOCIATION_COPY_NONATOMIC);
[_alertViews addObject:alertView];
__weak WXModalUIModule* wModuleSelf = self;

WXPerformBlockOnMainThread(^{
[alertView show];
});
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
WXModuleKeepAliveCallback callback = objc_getAssociatedObject(alertView, &WXModalCallbackKey);
if (!callback) return;

id result = @"";
switch (alertView.tag) {
case WXModalTypeAlert: {
result = @"";
break;
}
case WXModalTypeConfirm: {
NSString *clickTitle = [alertView buttonTitleAtIndex:buttonIndex];
result = clickTitle;
break;
}
case WXModalTypePrompt: {
NSString *clickTitle = [alertView buttonTitleAtIndex:buttonIndex];
NSString *text= [[alertView textFieldAtIndex:0] text] ?: @"";
result = @{ @"result": clickTitle, @"data": text };
break;
__strong WXModalUIModule* sModuleSelf = wModuleSelf;

if (sModuleSelf) {
void (^handleAction)(UIAlertController*, UIAlertAction*) = ^(UIAlertController* alertView, UIAlertAction* action) {
if (callback) {
id result = @"";
switch (type) {
case WXModalTypeAlert: {
break;
}
case WXModalTypeConfirm: {
result = action.title ?: @"";
break;
}
case WXModalTypePrompt: {
NSString *clickTitle = action.title ?: @"";
NSString *text = [alertView.textFields firstObject].text ?: @"";
result = @{ @"result": clickTitle, @"data": text };
break;
}
default:
break;
}

callback(result, NO);
}
};

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
__weak UIAlertController* weakAlertView = alertView;

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle ?: @"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
handleAction(weakAlertView, action);
}];

UIAlertAction *OKAction = nil;
if (okTitle) {
OKAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
handleAction(weakAlertView, action);
}];
}

if (type == WXModalTypePrompt) {
[alertView addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = defaultText ?: @"";
}];
}

[alertView addAction:cancelAction];

if (OKAction) {
[alertView addAction:OKAction];
}

UIViewController* selfVC = sModuleSelf.weexInstance.viewController;
if (selfVC == nil) {
selfVC = [UIApplication sharedApplication].delegate.window.rootViewController;
WXLogWarning(@"Unable to find view controller for instance. Use Application root vc to show AlertViewController.");
}

if (selfVC) {
[selfVC presentViewController:alertView animated:YES completion:nil];
}
}
default:
break;
}

callback(result,NO);

[_alertViews removeObject:alertView];
});
}

- (NSString*)stringValue:(id)value
Expand Down

0 comments on commit 3768c8f

Please sign in to comment.