Xcode Button Going to the Wrong Link - ios

my app currently has two buttons. One leads to a Twitter page, and the other links to a page on my Website. They are both on the same view controller and same view. However, when I click the Feedback button, it goes to my Twitter. Any advice?
Here's my code.
import UIKit
class AboutScreenViewController: UIViewController {
#IBAction func twitterButton(_ sender: Any) {
if let url2 = URL(string: "https://twitter.com/SunnyParks4u") {
UIApplication.shared.open(url2, options: [:], completionHandler: nil)
}
}
#IBAction func FeedbackButton(_ sender: Any) {
if let Contactlink = URL(string: "https://sunnyparks4u.wixsite.com/home-page/contact-8") {
UIApplication.shared.open(Contactlink, options: [:], completionHandler: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}

Maybe You copy/pasted the button in the interface builder and now both of the buttons executes the same IBAction.
Right-click on both buttons and check

Related

How to create a Cancel button on the screen that selects the file on Swift5?

I am using UIDocumentBrowser to retrieve files. But I am not able to place a back or cancel button in the navigation bar.
I want to make a cancellation button for this but I can't make a cancellation button. How can I solve this problem?
current code
import Foundation
import UIKit
#available(iOS 11.0, *)
class DocumentBrowserViewController : UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
browserUserInterfaceStyle = .dark
view.tintColor = .white
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: #escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
let newDocumentURL: URL? = nil
// Set the URL for the new document here. Optionally, you can present a template chooser before calling the importHandler.
// Make sure the importHandler is always called, even if the user cancels the creation request.
if newDocumentURL != nil {
importHandler(newDocumentURL, .move)
} else {
importHandler(nil, .none)
}
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
guard let sourceURL = documentURLs.first else { return }
do{
try presentDocument(at: sourceURL)
} catch {
Log.Debug("\(error)")
}
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
// Present the Document View Controller for the new newly created document
do{
try presentDocument(at: sourceURL)
} catch {
Log.Debug("\(error)")
}
}
func documentBrowser(_ controller: UIDocumentBrowserViewController, failedToImportDocumentAt documentURL: URL, error: Error?) {
// Make sure to handle the failed import appropriately, e.g., by presenting an error message to the user.
}
func presentDocument(at documentURL: URL) throws {
guard documentURL.startAccessingSecurityScopedResource() else {
throw IXError.fileAcessFailed
}
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let documentViewController = storyBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
documentViewController.document = Document(fileURL: documentURL)
}
}
picture of cancellation button that I want
Help me a lot
Thanks in advance.
Do I understand correctly that you want to push a viewController (documentViewController) on the navigation stack and have a back button on the navigationBar that leads you back to your main viewController (DocumentBrowserViewController)? If so first you need to push documentViewController on the current navigation stack.
First of all, does the documentViewController appears?
What I see is that you instantiate a documentViewController, set it's document to Document(...) and end of story. I don't use storyboard but does instantiate presents the viewController?
If you provide more details I will update the answer. But general conclusion is in your presentDocument(...), you need:
self.navigationController?.pushViewController(documentViewController, animated: true)
I learned about the UIDocumentBrowserViewController class and succeeded in adding buttons. But the position of the button is not where I want it to be.
But this has solved my fundamental problem, so I'll end the question.
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = false
allowsPickingMultipleItems = false
browserUserInterfaceStyle = .dark
view.tintColor = .white
let cancelbutton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelButton(sender:)))
additionalTrailingNavigationBarButtonItems = [cancelbutton]
}
#objc func cancelButton(sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}

Reloading a WebView with a new url from another ViewController

I'm having trouble reloading a UIWebiView from another view controller. When I try to send the new url and reload it, the app crashes due to the webview being nil.
I made a simple test app to narrow down my problem, right now its a simple page with a webivew showing google and a button on the view. When the button is pressed, it modally presents another page. This page only has one button that when pressed, it dismissed itself and calls a function in the first view asking to refresh it with a new url. Heres my code:
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let urlRequest = NSURLRequest(URL: NSURL(string: "https://www.google.com")!)
webView.loadRequest(urlRequest)
}
#IBAction func showViewPressed(sender: AnyObject) {
let showView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("showView")
self.presentViewController(showView, animated: true, completion: nil)
}
func reloadWebView(newURL: String) {
let urlRequest = NSURLRequest(URL: NSURL(string: newURL)!)
webView.loadRequest(urlRequest)
}
}
and for the second view:
class ReloadViewController: UIViewController {
#IBAction func buttonPressed(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: {
ViewController().reloadWebView("https://www.yahoo.com")
})
}
}
it crashes with fatal error: unexpectedly found nil while unwrapping an Optional value
Alright, so it seems before I ask a question I spend hours trying to find a solution to no avail. Then 5 minutes after I post a question thinking i'll never find the solution... I find it. So I found a solution that was for a tableview here this also happened to apply to webviews. So for anyone in the future looking for the same thing, heres what I did:
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let urlRequest = NSURLRequest(URL: NSURL(string: "https://www.google.com")!)
webView.loadRequest(urlRequest)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(reloadWebView), name:"reload", object: nil)
}
#IBAction func showViewPressed(sender: AnyObject) {
let showView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("showView")
self.presentViewController(showView, animated: true, completion: nil)
}
func reloadWebView() {
let urlRequest = NSURLRequest(URL: NSURL(string: "https://yahoo.com")!)
webView.loadRequest(urlRequest)
}
}
and for the second view:
class ReloadViewController: UIViewController {
#IBAction func buttonPressed(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: {
NSNotificationCenter.defaultCenter().postNotificationName("reload", object: nil)
})
}
}
and it worked beautifully!

How to navigate to particular view controller of contained application from TodayViewController button click

I have set up app group, I am able to go to application but unable to call its delegate functions or view controller delegate function which make me in trouble to navigate it to a particular page of my application.how can i solve this issue? please help me out.
#IBAction func btnseconbutton3(_ sender: Any) {
var responder: UIResponder? = self as UIResponder
let selector = #selector(self.openURL(_:))
while responder != nil {
if responder!.responds(to: selector) && responder != self {
responder!.perform(selector, with: URL(string: "SecurityPPSwiftFinal://")!)
return
}
responder = responder?.next
}
}
func openURL(_ url: URL) {
return
}
//In appdelegate method
let defaults = UserDefaults(suiteName: "group.com.example.SecurityPPSwiftFinal") defaults?.synchronize()|
// In landing page i.e main view contoller will appear code
override func viewWillAppear(_ animated: Bool)
{
let defaults = UserDefaults(suiteName: "group.com.example.SecurityPPSwiftFinal") defaults?.synchronize()
}
UIApplication object is not available in App Extensions.
For APIs not available in App Extensions, refer to: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html#//apple_ref/doc/uid/TP40014214-CH2-SW2
To open the App from Today Extension use:
#IBAction func btnseconbutton3(_ sender: Any)
{
self.extensionContext?.open(URL(string: "SecurityPPSwiftFinal://")!, completionHandler: nil)
}
To handle button tap event in AppDelegate after the app is opened, use:
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
{
if url.scheme == "SecurityPPSwiftFinal"
{
//TODO: Write your code here
}
return true
}
For more on interaction between Today Extension and Host App, refer to: https://github.com/pgpt10/Today-Widget

Safari ViewController with enabled address bar

I'm using Safari ViewController and address bar is disabled. How can I make it enabled so user can enter other url?
Here is the code that I'm using
class ViewController: UIViewController, SFSafariViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func showSafariVC(_ sender: AnyObject) {
let svc = SFSafariViewController(url: NSURL(string: "http://www.google.com") as! URL)
svc.delegate = self
present(svc, animated: true, completion: nil)
}
}
It is unable to edit SFSafariViewController 's URL as described here.
And in Apple developer site they mentioned that too:
A read-only address field with a security indicator and a Reader
button
Hope this help!

How to add a hyperlink button on iOS Swift?

I'm trying to make a clickable link over a button.
I made this using some Internet help but it didn't work :
#IBAction func linkClicked(sender: AnyObject) {
openUrl("http://fr.envisite.net/t5exce")
}
func openUrl(url:String!) {
let targetURL=NSURL(fileURLWithPath: url)
let application=UIApplication.sharedApplication()
application.openURL(targetURL);
}
It doesn't do anything, no error, just the button doesn't get me on Safari (I use the iOS simulator)
You're creating a file URL with a web url string. Use the NSURL String constructor instead.
#IBAction func linkClicked(sender: AnyObject) {
openUrl("http://fr.envisite.net/t5exce")
}
func openUrl(urlStr:String!) {
if let url = NSURL(string:urlStr) {
UIApplication.sharedApplication().openURL(url)
}
}
Swift3
#IBAction func linkClicked(sender: Any) {
openUrl(urlStr: "http://fr.envisite.net/t5exce")
}
func openUrl(urlStr: String!) {
if let url = URL(string:urlStr), !url.absoluteString.isEmpty {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}

Resources