Make UIButton Click Display UIWebview - ios

I'm trying to load a specific URL after clicking a button. I have the webView code inside of a UIButton action, but I'm not sure how to pull that into the display/viewDidLoad method it will work. But I need to be able to change the URL with different buttons.
import UIKit
class ViewController: UIViewController {
var radioURL = "http://www.ladiescanwetalk.org/category/podcasts/"
#IBOutlet weak var mainWebView: UIWebView!
#IBAction func loadRadioView(sender: UIButton) {
let getCurrentURL = NSURL (string: radioURL)
let requestCurrentURL = NSURLRequest(URL: getCurrentURL!)
self.mainWebView?.loadRequest(requestCurrentURL)
}
override func viewDidLoad() {
super.viewDidLoad()
}

In my opinion the best to achieve what you're asking is to set a different tag value for each value (you can do this either using Storyboard or directly via code).
Your code will be something like this:
#IBAction func loadRadioView(sender: UIButton) {
if sender.tag == 1 {
radioURL = "http://anotherUrl"
} else if sender.tag == 2 {
radioURL = "http://yetanotherurl"
} else if sender.tag == 3 {
radioURL = "http://....."
}
let getCurrentURL = NSURL (string: radioURL)
let requestCurrentURL = NSURLRequest(URL: getCurrentURL!)
self.mainWebView?.loadRequest(requestCurrentURL)
}

Related

Hide a website button in my tableViewCell

This is code for my custom tableViewCell
#IBOutlet weak var webButton: UIButton!
func update(place:EClass) {
self.place = place
myLabel.text = place.name
myImage.image = nil
myLabel2.text = place.getDescription()
// IF (didClickWebsite) THE SITE DOESN'T EXIST, MAKE INVISIBLE THE BUTTON webButton
if let url = place.photos?.first?.getPhotoURL(maxWidth: maxWidht) {
myImage.af_setImage(withURL: url)
}
}
#IBAction func goToWebSite(_ sender: Any) {
if let place = place, let delegate = delegate {
delegate.didClickWebsite(place: place)
}
}
where I recently added this button #IBOutlet weak var webButton: UIButton!
and this extension is in the VC of my tableView
extension CourseClass2: PlaceCellDelegate {
func didClickWebsite(place: EClass) {
NearbyZone.getZoneDetails(place: place) { (place) in
if let website = place.details?["website"] as? String, let url = URL(string: website) {
let svc = SFSafariViewController.init(url: url)
self.navigationController?.pushViewController(svc, animated: true)
}
}
}
}
what I would like to do is to tell the func update in my custom tableViewCell that if the function didClickWebsite does not produce any results (in this case when the site does not exist) it has to hide the website button, how caI i do this?

load qr code link in UIwebview swift iOS

hello everybody i have a webview VViewController.swift and
QRCode Scanner QRRViewController.swift how to load the QR Code if its url to a web view direct and not to safari ?
VVCiewcontroller.swift
import UIKit
class VViewController: UIViewController {
#IBOutlet weak var WWebView: UIWebView!
var QRLink:String!
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string: QRLink)
var request = NSURLRequest(URL: url!)
WWebView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func goBack(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
QRRViewController.swift lok like this
this code load the Qr code URL to safari and not to web view
.......
if metadataObj.type == AVMetadataObjectTypeQRCode {
let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as! AVMetadataMachineReadableCodeObject
qrCodeFrameView?.frame = barCodeObject.bounds;
if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue
//if the result is url...
UIApplication.sharedApplication().openURL(NSURL(string: metadataObj.stringValue)!)
}
}
Assuming that you move from the QR view controller to the web views view controller, use the prepare for segue method to pass the link to the next view.
Add this to the QR view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
// Create a variable that you want to send
var QRLink = self.QRLink
// Create a new variable to store the instance of PlayerTableViewController
let destinationVC = segue.destinationViewController as VViewController
destinationVC.QRLink = QRLink
}
Dont forget, you need to also add the variable in the webviews controller to store the QR link.
VVCiewcontroller.swift
import UIKit
class VViewController: UIViewController {
private var QRLink: String
#IBOutlet weak var WWebView: UIWebView!
var QRLink:String!
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string: QRLink)
var request = NSURLRequest(URL: url!)
WWebView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func goBack(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
I assume that you are able to retrieve the string from your qr code reader function,
, also that you have a webview, so try this:
let request = NSURLRequest(URL: "THE URL FROM QRCode")
wvScreen.loadRequest(request)

how change a piece of Url with Textbox & Button?(Swift 2)

this is my code but its give me nil in output.
import AVFoundation
import AVKit
import UIKit
var userName :String?
class ViewController: UIViewController {
var movieUrl:NSURL? = NSURL(string: "http://streamapp.ir:1935/lahzenegar/\(userName)/playlist.m3u8")
#IBOutlet var inpt: UITextField!
#IBOutlet var scView: UIScrollView!
let avPlayerViewController = AVPlayerViewController()
var avPlayer: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
}
and my button code:
#IBAction func playButtonTapped3(sender: UIButton) {
userName = "\(self.inpt.text)"
self.presentViewController(self.avPlayerViewController, animated: true) { () -> Void in
self.avPlayerViewController.player?.play()
}
print(movieUrl)
}
so i want when press button (username) change value to whatever users entered in textBox
already output:
thanks for help
When you first create the movieURL the userName is not set. That is fine, however, when you press the button you need to update the movieURL as well as the username. Something like this:
userName = "\(self.inpt.text)"
movieURL = NSURL(string: "http://streamapp.ir:1935/lahzenegar/\(userName)/playlist.m3u8")

How to call performSegueWithIdentifier in Swift

I have created a prepareForSegue method and I am trying to call it from a button that I created by using the performSegueWithIdentifier method. The app is crashing when I load the simulator and it's not getting me a complete error message. Can someone please lead me in the right direction?
import Foundation
import UIKit
import Alamofire
import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit
class PageContentViewController: UIViewController {
#IBOutlet weak var logoImageView: UIImageView!
#IBOutlet weak var contentLabel: UILabel!
#IBOutlet weak var backgroundImageView: UIImageView!
#IBOutlet weak var pageControl: UIPageControl!
#IBOutlet weak var facebookButton: UIButton!
var index : Int = 0
var logoFile: String = ""
var content: String = ""
var backgroundFile: String = ""
let facebookReadPermissions = ["public_profile", "email", "user_friends"]
override func viewDidLoad() {
super.viewDidLoad()
pageControl.currentPage = index
facebookButton.hidden = (index == 3 ) ? false : true
pageControl.hidden = (index == 3) ? true: false
logoImageView.image = UIImage(named: logoFile)
contentLabel.text = content
backgroundImageView.image = UIImage(named: backgroundFile)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let destinationController = segue.destinationViewController as? PaymentSubViewController
where segue.identifier == "payment" {
// Do something with `destinationController`
}
}
#IBAction func test(sender: AnyObject) {
self.performSegueWithIdentifier("payment", sender: self)
}
#IBAction func fbTouched(sender: AnyObject) {
FBSDKLoginManager().logInWithReadPermissions(self.facebookReadPermissions, handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
if error != nil {
//According to Facebook:
//Errors will rarely occur in the typical login flow because the login dialog
//presented by Facebook via single sign on will guide the users to resolve any errors.
// Process error
FBSDKLoginManager().logOut()
} else if result.isCancelled {
// Handle cancellations
FBSDKLoginManager().logOut()
} else {
let fbToken = result.token.tokenString
Alamofire.request(Router.FacebookAuth(fbToken)).validate(statusCode: 200 ..< 300).responseJSON(completionHandler: { (request, response, JSON, error) in
if let json = JSON as? Dictionary<String, AnyObject> {
if let token = json["token"] as? String {
Router.OAuthToken = token
self.performSegueWithIdentifier("showHomeFeed", sender: self)
}
}
})
}
})
}
}
Because you are force unwrapping the destinationViewController using as!, if that value is nil or not a PaymentSubViewController, the app will crash.
The better way to implement this is with an optional binding (if let) and a conditional downcast (as?):
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let destinationController = segue.destinationViewController as? PaymentSubViewController
where segue.identifier == "payment" {
// Do something with `destinationController`
}
}
Of course, that'll stop the crash but won't answer the question of why segue.destinationViewController is nil or of another type. Make sure that you segue is configured properly in interface builder and that the destination view controller actually has segue.destinationViewController for its Class value in the identity inspector tab.
I had a map object on the storyboard and I did not add an outlet for the object which was creating an error message.

Is there any code like "If the storyboard ID is XXX, then..." in Swift?

I am now building an app which needs to load two different URLs inside two UIView Controllers a UIWebView. Though, they need to be in same classes. So is there any way that I can simply check "If the storyboard ID is XXX, then load URL_1, else URL_2"?
Here is my code:
let day_url = NSURL(string: "http://www.url1.com/")
let day_url_request = NSURLRequest(URL: day_url!)
day_webView.loadRequest(day_url_request)
let week_url = NSURL(string: "http://www.url2.com/")
let week_url_request = NSURLRequest(URL: week_url!)
week_webView.loadRequest(week_url_request)
Thanks!
PS: Sorry for my bad English. :(
Each of your UIViewControllers has an unique ID and a custom class. The first UIViewController should look something like this:
class StoryboardID1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let day_url = NSURL(string: "http://www.url1.com/")
let day_url_request = NSURLRequest(URL: day_url!)
day_webView.loadRequest(day_url_request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
And for the second UIViewController we have:
class StoryboardID2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let week_url = NSURL(string: "http://www.url2.com/")
let week_url_request = NSURLRequest(URL: week_url!)
week_webView.loadRequest(week_url_request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
The other way
First, declare your UIViewController:
let myViewControllerID = self.storyboard?.instantiateViewControllerWithIdentifier("PutIDHere") as UIViewController
Then check if myViewControllerID is actually the controller the user navigating at:
if segue.identifier == "myViewControllerID" {
...
}
You can use restorationIdentifier instead of Storyboard identifier and check it from
if self.restorationIdentifier == "View1" {
// load URL1
} else {
// Load URL2
}
Good Luck

Resources