How to present SFSafariViewController in UIView

Related searches

i have a custom UIView that has a button property, i want to add an action to it to open webview in-app. To do that i want to use SFSafariViewController. Here is how i am trying to achieve this.

import UIKit
import SafariServices

class CustomViewScreen: UIView, SFSafariViewControllerDelegate {

@IBAction func privacyNoteBtnAction(_ sender: UIButton) {
    if #available(iOS 9.0, *) {
        let urlString = "https://stackoverflow.com/"
        if let url = URL(string: urlString) {
            let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
            vc.delegate = self
           UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil)
        }
    } else {
        // Fallback on earlier versions
    }
}

func safariViewControllerDidFinish(_ controller: SFSafariViewController) { 
  controller .dismiss(animated: true, completion: nil) // also not able to trigger this method when i tap "Done"
}

I cant even propery use this way , since i am getting " whose view is not in the window hierarchy!" Also i am not able to call SFSafariViewControllerDelegate method to dismiss the view after tap "Done" button. Any idea to give action while i am in the UIView or any idea to connect this action to my main controller to use properly present method?


Do like this,

@IBAction func privacyNoteBtnAction(_ sender: UIButton) {
   if #available(iOS 9.0, *) {
    let urlString = "https://stackoverflow.com/"
    if let url = URL(string: urlString) {
        let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
        vc.delegate = self

       if var topController = UIApplication.shared.keyWindow?.rootViewController {
          while let presentedViewController = topController.presentedViewController {
              topController = presentedViewController
          }

          topController.present(vc, animated: true, completion: nil)
       }

    }
} else {
    // Fallback on earlier versions
}
}

How to use SFSafariViewController to show web pages in your app , How to use SFSafariViewController to show web pages in your app entersReaderIfAvailable: true) vc.delegate = self present(vc, animated:� class SFSafariViewController: UIView Controller Overview The view controller includes Safari features such as Reader, AutoFill, Fraudulent Website Detection, and content blocking.


I don't think so. UIView can't present a viewcontroller, because they are different. You only can present a viewcontroller in a viewcontroller.

So I suggest your view just get touch event, and delegate to viewcontroller , tell it to present a SFSafariViewController. Like this:

// view.h
@protocol AccountBindingDelegate <NSObject>
- (void)jumpWebVC:(UIButton*)sender;
@end
@interface CustomRegisterView : UIView
@property (nonatomic, weak) id<AccountBindingDelegate> delegate;
@end

// view.m
[agreementBtn addTarget:self action:@selector(agreementBtnClick:) forControlEvents:UIControlEventTouchUpInside];

- (void)agreementBtnClick:(UIButton*)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(jumpWebVC:)]) {
        [self.delegate jumpWebVC:sender];
    }
}

// code in controller
@interface uiviewContrller ()<AccountBindingDelegate> 

@end

- (void)viewDidLoad {
    _registerView.delegate = self;
}

//tell viewcontroller to present
- (void)jumpWebVC:(UIButton*)sender
{
    PSMFWebVC *webVC = [PSMFWebVC new];
    [self.navigationController pushViewController:webVC animated:YES];
}

UIView don't need know the url, he just tell the viewcontroller : some button in uiview had been click. In case, your uivew has lots of buttons , your CustomViewScreen will be heavy which is not good.

SFSafariViewController poor replac…, The SfSafariViewController is a poor replacemet for UIWebview, IOS 12 If I present of of sf (safariviewcontroller) presenting view controller , the avpllayer does let overlay = UIView(frame: CGRect(x: 0, y: y, width: self.view.frame.width, � If a user clicks a web link in your app, you used to have two options before iOS 9.0 came along: exit your app and launch the web page in Safari, or bring up a new web view controller that you've designed, along with various user interface controls.


import UIKit
import SafariServices

class ViewController: UIViewController {

    var safariVC = SFSafariViewController(url: URL(string: "https://apple.com")!)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addViewControllerAsChildViewController()
    }
    
    //firstVCIdentifier
    
    func addViewControllerAsChildViewController() {
        addChild(safariVC)
        self.view.addSubview(safariVC.view)
        safariVC.didMove(toParent: self)
        self.setUpConstraints()
    }
    
    func setUpConstraints() {
        self.safariVC.view.translatesAutoresizingMaskIntoConstraints = false
        self.safariVC.view.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 100).isActive = true
        self.safariVC.view.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -100).isActive = true
        self.safariVC.view.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 100).isActive = true
        self.safariVC.view.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -100).isActive = true
    }

}

iOS, SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL: URL]; present(safariVC, animated: true, completion: nil) //Objective-C [self� SFSafariViewController(NSCoder) A constructor that initializes the object from the data stored in the unarchiver object. SFSafariViewController(NSObjectFlag) Constructor to call on derived classes to skip initialization and merely allocate the object. SFSafariViewController(NSUrl) Creates a new browsing interface with the provided URL.


ios - 如何在UIView中显示SFSafariViewController, ios - 如何在UIView中显示SFSafariViewController rootViewController?.present( vc, animated: true, completion: nil) } } else { // Fallback on earlier versions }� A better way to present a SFSafariViewController in SwiftUI A better way to present a SFSafariViewController or start a ASWebAuthenticationSession in SwiftUI. 13 August 2020


Push / Pop modal SFSafariViewController (Hacking , So really, only option is to present it modally. This has is I create a subclass of SFSafariViewController and add a 5 points wide UIView to it. i want to work with SFSafariViewController to view web application in ios app, but i am stuck in it here is my code PCL This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.


How to present SFSafariViewController in UIView i have a custom UIView that has a button property, i want to add an action to it to open webview in-app. To do that i want to use SFSafariViewController.