Eventually, I want to incorporate SKStoreProductViewController into a project I'm working on so to start I thought I'd recreate what was posted here. The only thing is, when I click on my button in the simulator (xcode 14.2) no modal is appearing. Could it be because I need to run this on a real device or maybe because storeViewController.loadProduct is 'silently failing'? Any and all help is much appreciated!
My dummy app:
//
// ViewController.swift
// SKStoreProductViewController Demo
//
import UIKit
import StoreKit
class ViewController: UIViewController {
let testFlightAppURL = URL(string: "https://apps.apple.com/us/app/testflight/id899247664")!
let testFlightProductID = 899247664
private let button: UIButton = {
let button = UIButton()
button.backgroundColor = .darkGray
button.setTitle("Open App Store", for: .normal)
return button
}()
override func viewDidLoad() {
view.addSubview(button)
button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
button.frame = CGRect(x: 30,
y: view.frame.height-150-view.safeAreaInsets.bottom,
width: view.frame.size.width-60,
height: 55)
}
#objc func didTapButton() {
openAppStore()
}
func openAppStore() {
// 2. Create an SKStoreProductViewController instance and set its delegate
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
// 3. Indicate a specific product by passing its iTunes item identifier
let parameters = [SKStoreProductParameterITunesItemIdentifier: testFlightProductID]
storeViewController.loadProduct(withParameters: parameters) { _, error in
if error != nil {
// In case there is an issue loading the product, open the URL directly
UIApplication.shared.open(self.testFlightAppURL)
} else {
self.present(storeViewController, animated: true)
}
}
}
}
// MARK: - SKStoreProductViewControllerDelegate
// 4. A simple implementation of SKStoreProductViewController.
// Delegate dismisses the view controller when the user completes the purchase.
extension ViewController: SKStoreProductViewControllerDelegate {
func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
viewController.dismiss(animated: true)
}
}
Testing on a real device resolved the issue for me.
Related
When I open a safari view controller and then return to my app (when "Done" is pressed), my app renders a blank/white screen instead of the content for that view.
The code below is the code I have tried using in an empty view - the issue happens no matter where I try in my app.
import UIKit
import SafariServices
class SavedViewController: UIViewController, SFSafariViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let link = URL(string: "https://google.com"){
let myrequest = SFSafariViewController(url: link)
myrequest.delegate = self
present(myrequest, animated:true)
}
}
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
dismiss(animated:true, completion: nil)
}
}
The website loads fine, its when I return to my app that the blank screen appears. Am I doing something wrong?
It works fine.
You just created new UIViewController this way, UIViewController by default have black background. So when you press Done you just come back from SafariViewController to you SavedViewController (UIViewController).
Probably You are looking for UIWebView solution https://developer.apple.com/documentation/uikit/uiwebview.
If u want only display SafariViewController do it as function from your ViewController, You don't need to create new File with UIViewController class to do it.
import UIKit
import SafariServices
class SavedViewController: UIViewController, SFSafariViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = .gray
setupViews()
}
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
dismiss(animated:true, completion: nil)
}
private func setupViews(){
view.addSubview(openSafariButton)
openSafariButton.addTarget(self, action: #selector(handleOpenSafariButtonTap), for: .touchUpInside)
openSafariButton.frame = CGRect(x: 0, y: 100, width: view.frame.width, height: 60)
}
#objc func handleOpenSafariButtonTap(){
if let link = URL(string: "https://google.com"){
let myrequest = SFSafariViewController(url: link)
myrequest.delegate = self
present(myrequest, animated:true)
}
}
let openSafariButton: UIButton = {
let button = UIButton()
button.setTitle("openSafari", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = .red
return button
}()
}
That is what i mean.
You can add function:
private func openSafariLink(link: String){
if let link = URL(string: link){
let myrequest = SFSafariViewController(url: link)
present(myrequest, animated:true)
}
}
And call it from any place like that:
openSafariLink(link: "https://google.com")
This way fits more for Your solution.
I have two super view controllers MasterCategoryListViewController and MasterCategoryItemViewController.
I want to use these in several apps.
I inherit from both of these
class CustomListController: MasterCategoryListViewController
class CustomItemController: MasterCategoryItemViewController
Now in the MasterCategoryListViewController
I have a button handler...
#objc open func btnAddTapped(sender: UIBarButtonItem) {
let itemViewController = MasterCategoryItemViewController()
itemViewController.title = "Type"
self.navigationController?.pushViewController(itemViewController, animated: true)
}
I know I can override the method to push to CustomItemController however, I'm just wondering if I can do this in my MasterCategoryListViewController, obviously without it knowing anything about what CustomItemController?
Create a method on the parent called something like detailVCClass(), answering the class that should be instantiated upon button tap. The parent can answer something generic, and the subclasses answer any class that's appropriate for each to know about.
Have the button tap method instantiate an instance of self.detailVCClass(), rather than a class name literal.
however, I'm just wondering if I can do this in my
MasterCategoryListViewController, obviously without it knowing
anything about what CustomItemController ?
Yes. Why not? However, the one that will be pushed is the MasterCategoryItemViewController, not any subclassing classes of it. So like what you've mentioned in your question, you know that btnAddTapped can be overriden, so do it like that.
OR, you could do something a bit more interesting:
In your MasterCategoryListViewController, have an object of MasterCategoryItemViewController. Then in your CustomListController, apply any subclassing MasterCategoryListViewController class. Next, push that MasterCategoryItemViewController object in your btnAddTapped()
Complete sample:
import UIKit
class ListVC: MasterListVC {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "ListVC"
self.itemVCToBePushed = ItemVC2()
}
}
class MasterListVC: UIViewController {
var itemVCToBePushed: MasterItemVC?
lazy var button: UIButton = {
let button = UIButton(type: .custom)
button.frame = CGRect(x: 100, y: 100, width: 250.0, height: 44.0)
button.setTitle("Test", for: .normal)
button.addTarget(self, action: #selector(self.pushMe), for: .touchUpInside)
button.backgroundColor = .gray
return button
}()
#objc func pushMe() {
guard let itemVCToBePushed = self.itemVCToBePushed else { return }
self.navigationController?.pushViewController(itemVCToBePushed, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.title = "MasterListVC"
self.view.backgroundColor = .white
self.view.addSubview(self.button)
}
}
/////
class ItemVC: MasterItemVC {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "ItemVC"
}
}
class ItemVC2: MasterItemVC {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "ItemVC2"
}
}
class MasterItemVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "MasterItemVC"
self.view.backgroundColor = .white
}
}
The code below compiles fine, but crashes with an unrecognized selector sent to instance error.
I have one class that inherits from UIViewController:
class Controller: UIViewController {
override func viewDidLoad() {
let toolbarWrapper = CustomToolbarWrapper(view: view, target: self)
let toolbar = toolbarWrapper.toolbarView
view.addSubview(toolbar)
... Other code ...
}
}
And another class that is just a wrapper for a UIView and contains buttons:
class CustomToolbarWrapper {
var toolbarView: UIView
init(view: UIView, target: Any) {
let height: CGFloat = 80
toolbarView = UIView(frame: CGRect(x: 0, y: view.frame.height - height, width: view.frame.width, height: height))
let button = UIButton()
... Some button layout code ...
button.addTarget(target, action: #selector(CustomToolbar.buttonTapped(_:)), for: .touchUpInside)
toolbarView.addSubview(button)
}
#objc static func buttonTapped(_ sender: Any) {
print("button tapped")
}
}
For the sake of clarity, I left out a large chunk of code and kept what I thought was necessary. I think that my code doesn't work because of my misunderstanding of the how the target works in the addTarget function. Normally, I would just use self as the target of my button's action, so I just tried to pass along self from the view controller to the CustomToolbarWrapper's init function.
What else I have tried:
Changing the button's target from target to self like this:
button.addTarget(self, action: #selector(CustomToolbar.buttonTapped(_:)), for: .touchUpInside)
results in the app not crashing anymore. Instead, however, I believe that line of code fails to do anything (which doesn't throw an error for some reason?) because attempting to print button.allTargets or even button.allTargets.count results in the app crashing at compile time, with an EXC_BREAKPOINT error and no error description in the console or the XCode UI (which just confuses me even more because there are no breakpoints in my code!).
Also, making buttonPressed(_:) non-static does not change any of the previously mentioned observations.
Also, to make sure the button could in fact be interacted with, I added this in the viewDidLoad() of Controller:
for subview in toolbar.subviews? {
if let button = subview as? UIButton {
button.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)
}
}
and added a simple testing method to Controller for the button:
#objc func buttonPressed(_ sender: UIButton) {
print("Button Pressed")
}
And running the code did result in "Button Pressed" being printed in the console log, so the button should be able to be interacted with by the user.
Feel free to let me know if you think this is not enough code to figure out the problem, and I will post more details.
Edit
I prefer to keep the implementation of the button's action in the CustomToolbarWrapper class to prevent repeating code in the future, since the action will be the same no matter where an instance of CustomToolbarWrapper is created.
The best option would be to add the target in your controller and then call a method in your toolbarWrapper on button press. But if you really need to keep this design, you should have a strong reference to your toolbarWrapper in your controller class, otherwise your toolbarWrapper is deallocated and nothing gets called. Also, the buttonTapped(_:) method does not need to be static. Thus, in your controller:
class Controller: UIViewController {
var toolbarWrapper: CustomToolbarWrapper?
override func viewDidLoad() {
toolbarWrapper = CustomToolbarWrapper(view: view, target: self)
let toolbar = toolbarWrapper.toolbarView
view.addSubview(toolbar)
... Other code ...
}
}
And in your wrapper:
class CustomToolbarWrapper {
var toolbarView: UIView
init(view: UIView, target: Any) {
let height: CGFloat = 80
toolbarView = UIView(frame: CGRect(x: 0, y: view.frame.height - height,width: view.frame.width, height: height))
let button = UIButton()
... Some button layout code ...
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
toolbarView.addSubview(button)
}
#objc func buttonTapped(_ sender: Any) {
print("button tapped")
}
}
There is another way I would use which is delegation. The target does not necessarily have to be a controller, it can be the CustomToolbarWrapper itself.
First, declare a protocol
protocol CTDelegate: AnyObject {
func didClickButton()
}
Then in CustomToolbarWrapper add a property, weak var delegate: CTDelegate? and a button action:
#objc func buttonTapped(_ sender: UIButton) {
delegate?.didClickButton()
}
So in your case, it becomes:
button.addTarget(self, action: #selector(CustomToolbarWrapper.buttonTapped(_:)), for: .touchUpInside)
Then when you go to any ViewController, conform to CTDelegate and initialize the CustomToolbarWrapper, you can set its delegate to the controller.
e.g
let toolbarWrapper = CustomToolbarWrapper(view: view, target: self)
toolbarWrapper.delegate = self
and implement your action inside the method you are conforming to in your controller i.e.
func didClickButton()
Your problem is right here:
let toolbarWrapper = CustomToolbarWrapper(view: view, target: self)
You're passing an instance of Controller class which doesn't implement the buttonTapped(_:) selector. It is implemented by your CustomToolbarWrapper class. This is a bad design in general. You should either follow a delegate pattern, or a callback pattern.
Updated Answer:
Delegate pattern solution:
class Controller: UIViewController, CustomToolbarWrapperDelegate {
override func viewDidLoad() {
let toolbarWrapper = CustomToolbarWrapper(view: view, buttonDelegate: self)
let toolbar = toolbarWrapper.toolbarView
view.addSubview(toolbar)
}
// MARK: - CustomToolbarWrapperDelegate
func buttonTapped(inToolbar toolbar: CustomToolbarWrapper) {
print("button tapped")
}
}
protocol CustomToolbarWrapperDelegate: AnyObject {
func buttonTapped(inToolbar toolbar: CustomToolbarWrapper) -> Void
}
class CustomToolbarWrapper {
var toolbarView: UIView
weak var buttonDelegate: CustomToolbarWrapperDelegate?
init(view: UIView, buttonDelegate: CustomToolbarWrapperDelegate?) {
let height: CGFloat = 80
toolbarView = UIView(frame: CGRect(x: 0, y: view.frame.height - height, width: view.frame.width, height: height))
self.buttonDelegate = buttonDelegate
let button = UIButton()
button.addTarget(self, action: #selector(self.buttonTapped(_:)), for: .touchUpInside)
toolbarView.addSubview(button)
}
#objc private func buttonTapped(_ sender: Any) {
// Your button's logic here. Then call the delegate:
self.buttonDelegate?.buttonTapped(inToolbar: self)
}
}
If you'd rather stick to your current design then just implement the following changes:
class Controller: UIViewController {
override func viewDidLoad() {
let toolbarWrapper = CustomToolbarWrapper(view: view, target: self, selector: #selector(self.buttonTapped(_:)), events: .touchUpInside)
let toolbar = toolbarWrapper.toolbarView
view.addSubview(toolbar)
}
#objc private func buttonTapped(_ sender: Any) {
print("button tapped")
}
}
class CustomToolbarWrapper {
var toolbarView: UIView
init(view: UIView, target: Any?, selector: Selector, events: UIControlEvents) {
let height: CGFloat = 80
toolbarView = UIView(frame: CGRect(x: 0, y: view.frame.height - height, width: view.frame.width, height: height))
let button = UIButton()
button.addTarget(target, action: selector, for: events)
toolbarView.addSubview(button)
}
}
Note: I created a Test program so I can better understand how ARC works - I am having trouble implementing it in my real project.
I created a Test program to determine how ARC works - it works great! Here it is.
ViewController1:
import UIKit
class ViewController: UIViewController {
weak var vc2:ViewController2?
override func viewDidLoad() {
super.viewDidLoad()
print("VC1 Initialized")
addButton()
}
func addButton() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
button.backgroundColor = .red
button.setTitle("Go to VC2", for: .normal)
self.view.addSubview(button)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
#objc func buttonAction(sender: UIButton!) {
let vx = ViewController2()
vx.VC1 = self
vc2 = vx
self.present(vc2!, animated: false, completion: nil)
}
}
ViewController2:
import UIKit
class ViewController2: UIViewController {
weak var VC1:ViewController?
weak var VC3:ViewController3?
override func viewDidLoad() {
super.viewDidLoad()
print("VC2 Initialized")
addButton()
}
deinit {
print("VC2 Deinitialized")
}
func addButton() {
for i in 0..<2 {
let button = UIButton(frame: CGRect(x: 0, y: self.view.frame.height*0.5*CGFloat(i), width: self.view.frame.width, height: self.view.frame.height*0.5))
button.backgroundColor = i == 0 ? .red : .blue
button.setTitle("Go to VC\(i*2+1)", for: .normal)
self.view.addSubview(button)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.tag = i
}
}
#objc func buttonAction(sender: UIButton!) {
let tag = sender.tag
if(tag == 0) {
self.dismiss(animated: true, completion: nil)
}
else {
let vc3 = ViewController3()
vc3.ViewController2 = self
VC3 = vc3
self.present(VC3!, animated: true, completion: nil)
}
}
}
ViewController3:
import UIKit
class ViewController3: UIViewController {
var ViewController2:ViewController2?
override func viewDidLoad() {
super.viewDidLoad()
print("VC3 Initialized")
addButton()
}
deinit {
print("VC3 Deinitialized")
}
func addButton() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
button.backgroundColor = .red
button.setTitle("Go to VC1", for: .normal)
self.view.addSubview(button)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
#objc func buttonAction(sender: UIButton!) {
self.ViewController2!.VC1!.dismiss(animated: true, completion: nil)
}
}
What I was looking at was how to remove multiple pages at a time (as indicated by the button push on VC3 and it goes back to the main page) and still remove ARC memory. All goes well. You can see the output below of me doing these operations.
Start program - push to page 2 - push to page. 1 - push to page 2 - push to page 3 - go to page 1
VC1 Initialized
VC2 Initialized
VC2 Deinitialized
VC2 Initialized
VC3 Initialized
VC3 Deinitialized
VC2 Deinitialized
The problem I am incurring is in my actual program - when I use these methods above, it automatically calls deinit on the last object I created when I CREATE my new object. But it doesn't actually delete anything - the memory graph will show x number of items even though the deinit method is called.
import UIKIT
class myClass: UIViewController {
weak var pageController:PageController?
override func viewDidLoad() {
super.viewDidLoad()
print("Initialized PC")
presentThemes()
}
func presentThemes() {
let pg = PageController(x: 1, controller: self)
pageController = pg
self.present(pageController!, animated: true, completion: nil)
}
deinit {
print("Deinit")
}
}
The output I am being provided is:
Initialized PC
On the first instance and is:
Deinit
Initialized PC
On the second instance. Any idea why Deinit is being called but the memory graph shows it there?
I am trying to display a button on the MainViewController and a UITextField in an ExternalViewController for when the device is connected via HDMI. When a click occurs in the MainViewController, I need to update the UITextField in the ExternalViewController. I can see the prints occur in the output window, but the text field does not update.
MainViewController.swift
import UIKit
import WebKit
class MainViewController: UIViewController {
fileprivate var externalWindow: UIWindow?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if UIScreen.screens.count > 1 {
setupExternalScreen(UIScreen.screens[1])
}
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = UIColor.blue
button.setTitle("Click Me", for: UIControlState.normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
fileprivate func setupExternalScreen(_ screen: UIScreen) {
guard externalWindow == nil,
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ExternalScreen") as? ExternalViewController else {
return
}
externalWindow = UIWindow(frame: screen.bounds)
externalWindow!.rootViewController = vc
externalWindow!.screen = screen
externalWindow!.isHidden = false
}
func buttonAction(sender: UIButton) {
print("Button tapped")
ExternalViewController().updateLabel()
}
}
ExternalViewController.swift
import UIKit
class ExternalViewController: UIViewController {
let output = UITextField(frame: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 300, height: 100)))
override func viewDidLoad() {
super.viewDidLoad()
self.addTextField()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addTextField() {
output.textColor = UIColor.black
output.text = "This is the other text field"
view.addSubview(output)
}
func updateLabel() {
print("inside updateLabel")
output.text = "button was clicked"
}
}
This is how it looks like.
This is my first project with Swift, so I apologize if it is a bad question.
Try using NotificationCentre .
In ExternalVC
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(receivedDataFromNotification(notification:)), name: NSNotification.Name(rawValue: "passdata"), object: nil)
}
func receivedDataFromNotification(notification : NSNotification) -> Void {
print(notification.object);
output.text = "button was clicked"
}
In MainViewController
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "passdata"), object: "your string pass here")
While you can use a notification to transfer data between I prefer creating a delegate to transfer the data.
first create a protocol
protocol ExternalViewControllerDelegate: class{
func shouldUpdateLabel(withText text: String)
}
Then update the ExternalViewController appropriately to contain the delegate which a weak reference of course
class ExternalViewController: UIViewController {
weak var delegate: ExternalViewControllerDelegate?
let output = UITextField(frame: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 300, height: 100)))
override func viewDidLoad() {
super.viewDidLoad()
self.addTextField()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addTextField() {
output.textColor = UIColor.black
output.text = "This is the other text field"
view.addSubview(output)
}
func updateLabel() {
print("inside updateLabel")
output.text = "button was clicked"
delegate?.shouldUpdateLabel(withText: "Your text")
}
}
Remember to call the method in the delegate. I used the updateLabel method in the class to call the method. Which I assume you also want to use
Finally implement the protocol in the MainViewController and remember to set the delegate.
extension MainViewController: ExternalViewControllerDelegate{
func shouldUpdateLabel(withText text: String) {
//Do what you want with the text
}
}
Then update the setupExternalScreen method to set the delegate
func setupExternalScreen(_ screen: UIScreen) {
guard externalWindow == nil,
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ExternalScreen") as? ExternalViewController else {
return
}
vc.delegate = self
externalWindow = UIWindow(frame: screen.bounds)
externalWindow!.rootViewController = vc
externalWindow!.screen = screen
externalWindow!.isHidden = false
}