Swift YouTube video in Full Screen & Auto Play with SFSafariViewController - ios

Problem:
I would like SFSafariViewController to autoplay a YouTube video in full screen.
Code:
import SafariServices
#objc func handleYTTap(_ sender: UIGestureRecognizer) {
let youtubeVideoURL = "https://youtu.be/d9MyW72ELq0"
let bookmark = NSURL(string: youtubeVideoURL)
let safari = SFSafariViewController(url: bookmark as! URL)
if(UIDevice.current.userInterfaceIdiom == .pad) {
safari.modalPresentationStyle = .fullScreen
}else{
safari.modalPresentationStyle = .fullScreen
}
self.present(safari, animated: true, completion: nil)
}
Tried Solutions:
I have tried to implement
"?playsinline=1?autoplay=1".
to the url but still cant get the SFSafariViewController to autoplay in full screen. is this even possible to do without creating a custom view or using a library of some sort?

Related

Using Safariservices swift how to code around the small black box

Using SFSafariViewController, works well but for some reason when I go to this webpage [knowitall.ch], it starts up by opening a zoomed view? with a small black box that I need to press to get the full webpage.
My code couldn't be simpler.
if let url = URL(string: url2U) {
let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
vc.delegate = self
present(vc, animated: true)
}
If I press the box I get the correct view, how to code around this so I open with the second view here? black box not required :)
That "zoomed view" is the Reader Mode. It shows up because you asked for it. Set entersReaderIfAvailable to false if you don't want it. Also, init(url:entersReaderIfAvailable:) has been deprecated in iOS 11. You need to initialize it with a config object:
if let url = URL(string: url2U) {
let vc: SFSafariViewController
if #available(iOS 11.0, *) {
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = false
vc = SFSafariViewController(url: url, configuration: config)
} else {
vc = SFSafariViewController(url: url, entersReaderIfAvailable: false)
}
vc.delegate = self
present(vc, animated: true)
}

Xcode Swift play youtube video on Native video player?

i am looking for pod on swift to play Youtube video on ios native player just like XCDYouTubeKit unfortunately XCDYouTubeKit is only for objective-C i can't find any swift pod work like this , all i found they embed iframe video and the youtube logo will appear which will allow user to click on it and miss up the page when the full page open .
how i can play youtube video on ios native video player ? without html embed !
Exactly like :
Although XCDYouTubeKit is written in Objective-C, it integrates fine with Swift. Here is some sample code to use it with Swift 3:
struct YouTubeVideoQuality {
static let hd720 = NSNumber(value: XCDYouTubeVideoQuality.HD720.rawValue)
static let medium360 = NSNumber(value: XCDYouTubeVideoQuality.medium360.rawValue)
static let small240 = NSNumber(value: XCDYouTubeVideoQuality.small240.rawValue)
}
func playVideo(videoIdentifier: String?) {
let playerViewController = AVPlayerViewController()
self.present(playerViewController, animated: true, completion: nil)
XCDYouTubeClient.default().getVideoWithIdentifier(videoIdentifier) { [weak playerViewController] (video: XCDYouTubeVideo?, error: Error?) in
if let streamURLs = video?.streamURLs, let streamURL = (streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?? streamURLs[YouTubeVideoQuality.hd720] ?? streamURLs[YouTubeVideoQuality.medium360] ?? streamURLs[YouTubeVideoQuality.small240]) {
playerViewController?.player = AVPlayer(url: streamURL)
} else {
self.dismiss(animated: true, completion: nil)
}
}
}

Trying to Open SFSafariViewController in AppDelegate via Push Notification

I am trying to open a URL using Safari when the app gets a push notification. This works successfully when the app is in the first viewcontroller. But if the app is further into the program I get an error
<SFSafariViewController: 0x10b20ae60> on <AdViewController: 0x100b14530> whose view is not in the window hierarchy!
(AdViewController is my first view controller, so it is still focused on that one. )
I have searched this site and have tried all the suggestions for this error but have come up short. Currently, my code looks like this:
if MessageUrl != nil , let url = URL(string: MessageUrl!) {
let safari = SFSafariViewController(url: url)
self.window?.rootViewController?.presentedViewController?.present(safari, animated: true, completion: nil)
}
Get top most UIViewController I used extension UIApplication in Appdeleagte from the "swift 3" answer at bottom of that page and changed my code to:
if MessageUrl != nil , let url = URL(string: MessageUrl!) {
let safari = SFSafariViewController(url: url)
UIApplication.topViewController()?.present(safari, animated: true, completion: nil)
}

ios9 - Swift 2.1 - SFSafariViewController

I'm looking to use the SFSafariViewController and have it automatically load a website when the app is opened. Is this possible?
What I have so far (brand new single ios app):
// ViewController.swift
let urlString = "https://stackoverflow.com"
#IBAction func launchSFSafariViewController(sender: AnyObject) {
if let url = NSURL(string: urlString) {
let vc = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
vc.delegate = self
presentViewController(vc, animated: true, completion: nil)
}
}
How do I connect the action to the main view controller via the storyboard? All the articles I see only discuss connecting a button to an action.
Implement viewDidAppear to perform the presentation. (You will need to use a Bool flag, though, to make sure this doesn't happen when you dismiss the presented view controller and viewDidAppear is called again!)

Make button open link - Swift

This is the code I have now, taken from an answer to a similar question.
#IBAction func GoogleButton(sender: AnyObject) {
if let url = NSURL(string: "www.google.com"){
UIApplication.sharedApplication().openURL(url)
}
}
The button is called Google Button and its text is www.google.com
How do I make it open the link when I press it?
What your code shows is the action that would occur once the button is tapped, rather than the actual button. You need to connect your button to that action.
(I've renamed the action because GoogleButton is not a good name for an action)
In code:
override func viewDidLoad() {
super.viewDidLoad()
googleButton.addTarget(self, action: "didTapGoogle", forControlEvents: .TouchUpInside)
}
#IBAction func didTapGoogle(sender: AnyObject) {
UIApplication.sharedApplication().openURL(NSURL(string: "http://www.google.com")!)
}
In IB:
Edit: in Swift 3, the code for opening a link in safari has changed. Use UIApplication.shared().openURL(URL(string: "http://www.stackoverflow.com")!) instead.
Edit: in Swift 4
UIApplication.shared.openURL(URL(string: "http://www.stackoverflow.com")!)
The string you are supplying for the NSURL does not include the protocol information. openURL uses the protocol to decide which app to open the URL.
Adding "http://" to your string will allow iOS to open Safari.
#IBAction func GoogleButton(sender: AnyObject) {
if let url = NSURL(string: "http://www.google.com"){
UIApplication.sharedApplication().openURL(url)
}
}
if let url = URL(string: "your URL") {
if #available(iOS 10, *){
UIApplication.shared.open(url)
}else{
UIApplication.shared.openURL(url)
}
}
as openUrl method is deprecated in iOS 10, here is solution for iOS 10
let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString) as! URL
UIApplication.shared.open(settingsUrl, options: [:], completionHandler: nil)
In Swift 4
if let url = URL(string: "http://yourURL") {
UIApplication.shared.open(url, options: [:])
}
if iOS 9 or higher it's better to use SafariServices, so your user will not leave your app.
import SafariServices
let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)
For Swift 3.0:
if let url = URL(string: strURlToOpen) {
UIApplication.shared.openURL(url)
}
This code works with Xcode 11
if let url = URL(string: "http://www.google.com") {
UIApplication.shared.open(url, options: [:])
}
The code that you have should open the link just fine. I believe, that you probably just copy-pasted this code fragment into your code. The problem is that the UI component (button) in the interface (in storyboard, most likely) is not connected to the code. So the system doesn't know, that when you press the button, it should call this code.
In order to explain this fact to the system, open the storyboard file, where your Google Button is located, then in assistant editor open the file, where your func GoogleButton code fragment is located. Right-click on the button, and drag the line to the code fragment.
If you create this button programmatically, you should add target for some event, for instance, UITouchUpInside. There are plenty of examples on the web, so it shouldn't be a problem :)
UPDATE: As others noted already, you should also add a protocol to the link ("http://" or "https://"). It will do nothing otherwise.
For Swift3 , below code is working fine
#IBAction func Button(_ sender: Any) {
UIApplication.shared.open(urlStore1, options: [:], completionHandler: nil)
}
Actually You Can Use It Like This In Your Action Button Works For Swift 5 :
guard let settingsUrl = URL(string:"https://yourLink.com") else {
return
}
UIApplication.shared.open(settingsUrl, options: [:], completionHandler: nil)
}
// How to open a URL in Safari
import SafariServices \\ import
#IBAction func google(_ sender: Any)
{
if let url = URL(string: "https://www.google.com")
{
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)
}
}
I made this way:
I imported SafariServices
import SafariServices
First step: I defined a button just above viewDidLoad:
let myButton = UIButton()
Second step: I called a function inside viewDidLoad:
func setupMyButton() {
view.addSubview(myButton)
myButton.configuration = .plain()
myButton.configuration?.cornerStyle = .capsule
myButton.configuration?.title = "Go to Google"
myButton.addTarget(self, action: #selector(selector), for: .touchUpInside)
myButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
myButton.widthAnchor.constraint(equalToConstant: 200),
myButton.heightAnchor.constraint(equalToConstant: 50),
])
}
Third step: At the bottom of the scope, I called an #objc func to use as selector. (Outside viewDidLoad)
#objc func selector() {
if let url = URL(string: "https://www.google.com")
{
let safariVC = SFSafariViewController(url: url)
present(safariVC, animated: true, completion: nil)
}
}
And I did not forget to call my func at the beginning of the viewDidLoad:
setupMyButton()
A dude named PRAVEEN BHATI helped me at the third step.
Hope this helps.

Resources