Monday, June 3, 2013

UIWebView allowsInlineMediaPlayback example in Objective C (iOS).


UIWebView allowsInlineMediaPlayback

A Boolean value that determines whether HTML5 videos play inline or use the native full-screen controller.

@property(nonatomic) BOOL allowsInlineMediaPlayback

Discussion of [UIWebView allowsInlineMediaPlayback]
The default value on iPhone is NO.

In order for video to play inline, not only does this property need to be set on the view, but the video element in the HTML document must also include the webkit-playsinline attribute.

UIWebView allowsInlineMediaPlayback example.
- (void)viewDidLoad {
    [super viewDidLoad];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"my url"]];
    [request setValue:[NSString stringWithFormat:@"%@ (iPhone/%@)", [request valueForHTTPHeaderField:@"User-Agent"],@"1.0"] forHTTPHeaderField:@"User-Agent"];

    request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

    [_webView loadRequest:request];
    _webView.allowsInlineMediaPlayback = YES;
    [_webView setDataDetectorTypes:UIDataDetectorTypeAll];
}

Example of [UIWebView allowsInlineMediaPlayback].
    BOOL ok = [super application:application didFinishLaunchingWithOptions:launchOptions];
    if (ok) {
        self.webView.allowsInlineMediaPlayback = YES;
        self.webView.mediaPlaybackRequiresUserAction = NO;
    }
    return ok;


UIWebView allowsInlineMediaPlayback example.
HTML
<video id="player" width="480" height="320" webkit-playsinline>


Obj-C
webview.allowsInlineMediaPlayback = YES;

End of UIWebView allowsInlineMediaPlayback example article.