salud
salud

Reputation: 103

I want to perform automatic playback when loading with WKWebView

In Objective-C, we developed an application using UIWebView. Among them, the automatic reproduction of media was realized with the following sources,

[self.webview setMediaPlaybackRequiresUserAction:NO];

An error will now appear when changing from UIWebView to WKWebView.

No visible @interface for 'WKWebView' declares the selector 'setMediaPlaybackRequiresUserAction:'

How can I fix it? Please tell me if you understand.

Upvotes: 0

Views: 988

Answers (1)

Denis
Denis

Reputation: 492

WKWebView setMediaPlaybackRequiresUserAction is deprecated so you need to replace it with WKWebViewConfiguration mediaTypesRequiringUserActionForPlayback like below.

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
theConfiguration.mediaTypesRequiringUserActionForPlayback = false;

Yo can't add WKWebView directly to in your ViewController, you need to add it programatically like below.

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
theConfiguration.mediaTypesRequiringUserActionForPlayback = true;
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:@"http://www.apple.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];

Use Above code or Below Code

If you want to change frame than use below, Here is full example code.

ViewController.h File

#import <WebKit/WebKit.h>

@interface ViewController : UIViewController

@property(strong,nonatomic) WKWebView *webView;
@property (strong, nonatomic) NSString *productURL;

@end

ViewController.m File

#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.productURL = @"http://www.URL YOU WANT TO VIEW GOES HERE";

    NSURL *url = [NSURL URLWithString:self.productURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    theConfiguration.mediaTypesRequiringUserActionForPlayback = false;
    _webView = [[WKWebView alloc] initWithFrame:self.view.frame];  
    [_webView loadRequest:request];
    _webView.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:_webView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Upvotes: 4

Related Questions