ios - UiWebView 委托(delegate)方法未被调用

标签 ios objective-c iphone uiwebview

我正在尝试在 webView 中播放 youtube 嵌入式视频,当我没有设置委托(delegate)时它会播放,如果我设置委托(delegate)视频不会加载并且委托(delegate)方法也不会被调用。这是我的代码:

.m类

#import "EmbeddedVideoVC.h"

@interface EmbeddedVideoVC (){
    MBProgressHUD *hud;
}
//@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIView *viewSelf;

@property (strong, nonatomic) NSTimer *controllersTimer;
@property (assign, nonatomic) NSInteger controllersTimeoutPeriod;

@end

@implementation EmbeddedVideoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.transform = CGAffineTransformMakeRotation(M_PI/2);       
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    CGRect bounds = [[UIScreen mainScreen] bounds];
    if ([SharedAppManager sharedInstance].applicationFrame.size.height < 568) {
        bounds = CGRectMake(0, 0, 480, 320);
    }
    _videoWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,bounds.size.height, bounds.size.width)];
    [_videoWebView setAllowsInlineMediaPlayback:YES];
    [_videoWebView setMediaPlaybackRequiresUserAction:NO];

    [self.viewSelf addSubview:_videoWebView];

    hud = [MBProgressHUD showHUDAddedTo:_videoWebView animated:YES];
    hud.color = [UIColor clearColor];
    hud.activityIndicatorColor = [UIColor whiteColor];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMethod)];
    [tap setNumberOfTapsRequired:1]; // Set your own number here
    [tap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol
    [_videoWebView addGestureRecognizer:tap];
    _videoWebView.delegate= self;
    [_videoWebView loadHTMLString:self.embeddedCode baseURL:nil];
    [self hideControllers];

}
 -(void)didTapMethod{
     //Showing Controls
}
#pragma mark - WEBVIEW DELEGATES

- (void)webViewDidStartLoad:(UIWebView *)webView{
    [MBProgressHUD hideHUDForView:self.videoWebView animated:YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
   [MBProgressHUD hideHUDForView:self.videoWebView animated:YES];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
   [MBProgressHUD hideHUDForView:self.videoWebView animated:YES];
}
- (BOOL)prefersStatusBarHidden {

    return YES;
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }

-(void)hideControllers { 
    [UIView animateWithDuration:0.5f animations:^{
    dispatch_async(dispatch_get_main_queue(), ^{ 
       topView.hidden= YES; 
    }); 
    } completion:^(BOOL finished){ 
    }]; 
}


-(void) showControles {

}

@end

.h类

#import "MusicParentVC.h"

@interface EmbeddedVideoVC : MusicParentVC <UIGestureRecognizerDelegate, UIWebViewDelegate>

@property (strong, nonatomic)  NSString *embeddedCode;
@property (nonatomic, strong) UIWebView *videoWebView;
@end

谁能告诉我问题是什么以及为什么 webViewDidFinishLoad: 和其他委托(delegate)方法没有被调用,甚至嵌入代码没有加载到 webview 中?

最佳答案

只需将您的UIWebView相关代码放在viewDidAppear:方法中即可。

引用:https://stackoverflow.com/a/27647327/2284065

关于ios - UiWebView 委托(delegate)方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41219916/

相关文章:

iphone - 使用我的应用程序打开文件

iphone - 需要 iOS 数据库云设计技巧

iphone - 当 UIButton 被拖动到屏幕的某个点时触发事件? objective-c

iphone - 在核心数据中列出共同的 friend

ios - setDetailItem : Do? 是什么

iOS 在 dispatch_async 中调用 animateWithDuration

ios - 在iOS问题中打开SQLite文件

ios - 如何用 objective c 实现 UISearchController

ios - 如何确定 UIScreenEdgePanGestureRecognizer 的框架

iOS 错误 : FBSOpenApplicationErrorDomain error 5. 这是什么意思?