I have a project from college, make a mobile app, I made it for the ios version. I tried to run it in the simulator, it shows the text. but if I try it in real it’s not a simulator. the text doesn’t work, there is an error like that on the webkit, I don’t know why. can anyone help?
I want the text to appear on a real iPhone, not a simulator 🙁
here this code:
class ThirdViewController: UIViewController {
var webView: WKWebView!
var descriptionLabel: UILabel!
var scrollView: UIScrollView!
var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupScrollView()
setupWebView()
setupLabel()
setupDescription()
loadVideo()
}
func setupScrollView() {
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
// Set up constraints for the scroll view
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
// Set up the content view inside the scroll view
contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(contentView)
// Set up constraints for the content view
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)
])
}
func setupWebView() {
webView = WKWebView()
webView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(webView)
// Set up constraints for the mini-player in the top-left corner
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20),
webView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
webView.widthAnchor.constraint(equalToConstant: 200),
webView.heightAnchor.constraint(equalToConstant: 150)
])
// Set corner radius
webView.layer.cornerRadius = 30
webView.clipsToBounds = true
}
func setupLabel() {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "How To Make Nasi Goreng At Home"
label.font = UIFont.boldSystemFont(ofSize: 18)
label.numberOfLines = 5
contentView.addSubview(label)
// Set up constraints for the label
NSLayoutConstraint.activate([
label.centerYAnchor.constraint(equalTo: webView.centerYAnchor),
label.leadingAnchor.constraint(equalTo: webView.trailingAnchor, constant: 20),
label.trailingAnchor.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -20)
])
}
func setupDescription() {
descriptionLabel = UILabel()
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.text = """
Description
Ingredients:
Sweet Soy Sauce
¼ cup soy sauce
4 tbsp brown sugar
Nasi Goreng
2 tbsp cooking oil
2 tbsp chopped onion
2 garlic cloves
2-3 red chili
100-gm boneless chicken
1 tbsp sweet soy sauce
1 egg
1 ½ tsp balachaw paste
1 ½ cup cooked rice
¼ tsp turmeric
1 tsp soy sauce
½ tsp pepper
Garnish
1 tsp fried onions
1 egg
Instructions:
1. Simmer soy sauce with brown sugar on low heat until it thickens
2. Set aside the sweet soy sauce to use for later
3. Add cooking oil along with chopped onion, garlic cloves, and red chili in pan
4. Stir for 1-2 minutes
5. Add bite size chicken pieces and sweet soy sauce
6. Stir in whisked egg
7. Add balachaw paste and cooked rice
8. Add turmeric, soy sauce, salt and pepper to taste
9. Garnish with a fried onion and a sunny side up before serving
10. Ready to Enjoy!
"""
descriptionLabel.font = UIFont.systemFont(ofSize: 16)
descriptionLabel.numberOfLines = 0 // Allows the label to have multiple lines
contentView.addSubview(descriptionLabel)
// Set up constraints for the description label
NSLayoutConstraint.activate([
descriptionLabel.topAnchor.constraint(equalTo: webView.bottomAnchor, constant: 20),
descriptionLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
descriptionLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
descriptionLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20)
])
}
func loadVideo() {
// YouTube video URL with autoplay enabled
let videoURL = URL(string: "https://www.youtube.com/embed/zsNnqYN_a6Y?autoplay=1")!
let request = URLRequest(url: videoURL)
webView.load(request)
}
}
class FourViewController: UIViewController {
var webView: WKWebView!
var descriptionLabel: UILabel!
var scrollView: UIScrollView!
var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupScrollView()
setupWebView()
setupLabel()
setupDescription()
loadVideo()
}
func setupScrollView() {
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
// Set up constraints for the scroll view
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
// Set up the content view inside the scroll view
contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(contentView)
// Set up constraints for the content view
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)
])
}
func setupWebView() {
webView = WKWebView()
webView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(webView)
// Set up constraints for the mini-player in the top-left corner
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20),
webView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
webView.widthAnchor.constraint(equalToConstant: 200),
webView.heightAnchor.constraint(equalToConstant: 150)
])
// Set corner radius
webView.layer.cornerRadius = 30
webView.clipsToBounds = true
}
func setupLabel() {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Anak Anak Jadi Lahap Makan Setelah Di Buat Kan Lele Goreng Kremes, Garing Seharian"
label.font = UIFont.boldSystemFont(ofSize: 18)
label.numberOfLines = 5
contentView.addSubview(label)
// Set up constraints for the label
NSLayoutConstraint.activate([
label.centerYAnchor.constraint(equalTo: webView.centerYAnchor),
label.leadingAnchor.constraint(equalTo: webView.trailingAnchor, constant: 20),
label.trailingAnchor.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -20)
])
}
func setupDescription() {
descriptionLabel = UILabel()
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.text = """
Description
CARA MEMBUAT IKAN LELE GORENG KREMES SUPER CRUNCY
BAHAN BAHAN
500 Gram Ikan Lele
Untuk Kremesan nya
5 Sdm Tepung beras
2 Sdm Tepung maizena
1 butir telur
3 siung bawang putih
1 Sdm ketumbar bubuk
1 Sdt kunyit bubuk
1 Sdt Royco ayam
1/2 Sdt garam
1/2 Sdt soda kue
200 ml air
"""
descriptionLabel.font = UIFont.systemFont(ofSize: 16)
descriptionLabel.numberOfLines = 0 // Allows the label to have multiple lines
contentView.addSubview(descriptionLabel)
// Set up constraints for the description label
NSLayoutConstraint.activate([
descriptionLabel.topAnchor.constraint(equalTo: webView.bottomAnchor, constant: 20),
descriptionLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
descriptionLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
descriptionLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20)
])
}
func loadVideo() {
// YouTube video URL with autoplay enabled
let videoURL = URL(string: "https://www.youtube.com/watch?v=BS1ViAwnlJ0")!
let request = URLRequest(url: videoURL)
webView.load(request)
}
}
class FiveViewController: UIViewController {
var webView: WKWebView!
var descriptionLabel: UILabel!
var scrollView: UIScrollView!
var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupScrollView()
setupWebView()
setupLabel()
setupDescription()
loadVideo()
}
func setupScrollView() {
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
// Set up constraints for the scroll view
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
// Set up the content view inside the scroll view
contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(contentView)
// Set up constraints for the content view
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)
])
}
func setupWebView() {
webView = WKWebView()
webView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(webView)
// Set up constraints for the mini-player in the top-left corner
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20),
webView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
webView.widthAnchor.constraint(equalToConstant: 200),
webView.heightAnchor.constraint(equalToConstant: 150)
])
// Set corner radius
webView.layer.cornerRadius = 30
webView.clipsToBounds = true
}
func setupLabel() {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Beef And Onion Stir Fry |Tender And Juicy Beef"
label.font = UIFont.boldSystemFont(ofSize: 18)
label.numberOfLines = 5
contentView.addSubview(label)
// Set up constraints for the label
NSLayoutConstraint.activate([
label.centerYAnchor.constraint(equalTo: webView.centerYAnchor),
label.leadingAnchor.constraint(equalTo: webView.trailingAnchor, constant: 20),
label.trailingAnchor.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -20)
])
}
func setupDescription() {
descriptionLabel = UILabel()
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.text = """
Description
—-Ingredients(4 servings)—
6.7 oz/190 g onion
4 stalks of green onion
1/4 tsp salt
1/4 tsp sugar
4 cloves of garlic
16.9 oz/480 g beef
2 tsp soy sauce
2 1/4 tsp dark soy sauce
3/4 tsp oyster sauce
3/4 tsp sugar
1 1/2 tsp baking soda
4 1/2 tsp corn starch
3 tsp oil
1 1/2 tsp sesame oil
3 tbsp oil for cooking
3 tsp dark soy sauce
1 1/2 tsp oyster sauce
1 1/2 tbsp cooking wine
1/3 cup water
Black pepper
"""
descriptionLabel.font = UIFont.systemFont(ofSize: 16)
descriptionLabel.numberOfLines = 0 // Allows the label to have multiple lines
contentView.addSubview(descriptionLabel)
// Set up constraints for the description label
NSLayoutConstraint.activate([
descriptionLabel.topAnchor.constraint(equalTo: webView.bottomAnchor, constant: 20),
descriptionLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
descriptionLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -20),
descriptionLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -20)
])
}
func loadVideo() {
// YouTube video URL with autoplay enabled
let videoURL = URL(string: "https://www.youtube.com/watch?v=wJ_vNUSQMZg")!
let request = URLRequest(url: videoURL)
webView.load(request)
}
}
Yizhreel Novanto Tampubolon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.