Swift: How to Open a New App When Uibutton Is Tapped

Swift: How to open a new app when UIButton is tapped

Try this. For example you want to open an Instagram app:

let instagramHooks = "instagram://user?username=johndoe"
let instagramUrl = URL(string: instagramHooks)!
if UIApplication.shared.canOpenURL(instagramUrl)
{
UIApplication.shared.open(instagramUrl)
} else {
//redirect to safari because the user doesn't have Instagram
UIApplication.shared.open(URL(string: "http://instagram.com/")!)
}

How to have UILabel tap act like a UIButton to open URL

First you should better use buttons to handle things like that (UIButton) not labels (UILabel) , to solve your problem Try this

 // This is the key
let tap = UITapGestureRecognizer(target: self, action: #selector(self.onClicLabel(sender:)))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)

let tap2 = UITapGestureRecognizer(target: self, action: #selector(self.onClicLabel(sender:)))
label2.isUserInteractionEnabled = true
label2.addGestureRecognizer(tap2)

@objc func onClicLabel(sender:UITapGestureRecognizer) {
if(sender.view == self.label)
{
openUrl("http://www.google.com")
}
else if(sender.view == self.label2)
{
openUrl("http://www.twitter.com")
}
}

Open AppStore through button

Here. But I highly suggest you learn the basics of Swift!

UIApplication.sharedApplication().openURL(NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")!)

If you wanna open the AppStore in Swift 5:

if let url = URL(string: "itms-apps://itunes.apple.com/app/id1629135515") {
UIApplication.shared.open(url)
}

How to integrate two application in one application in iOS

On click of UIButton, you take the other App URL and can open it using:

 var newApp = "yourApp://"
var newAppUrl = NSURL(string: newApp)
if UIApplication.sharedApplication().canOpenURL(newAppUrl!)
{
UIApplication.sharedApplication().openURL(newAppUrl!)
}

Swift | UIButton causes crash on tap

The answer is in the error message. Somewhere, my guess is in LoginViewController, there is a view of type LoginView. That view is calling addTarget(_:action:for:). LoginView is not subclassed from UIControl and does not have addTarget(_:action:for:). It's causing the crash.

Let me break down the parts of -[SparkGPS.LoginView addTarget:action:forControlEvents:].

  • The - at the beginning means it's an instance method and not a static or class method.

  • SparkGPS.LoginView is the module and class. A module is another word for a framework or app. In this case, it looks like you have an app named SparkGPS and a class named LoginView.

  • addTarget:action:forControlEvents: is Objective-C's name for addTarget(_:action:for:).

Finally, "selector sent to instance" means the variable call a method. Selector is a way to identify a method, and an instance is stored in a variable. For example, in your code you have loginButton.setTitle("Login", for: .normal). This could be worded as setTitle(_:for:) was sent to the instance loginButton.

Launching App Store from App in Swift

You have too many protocols in your URL. Get rid of https: so the URL reads

itms-apps://itunes.apple.com/app/bars/id706081574

Open Apple Watch app from another watch app

The documentation of the openSystemURL(_:) method clearly states that the URL supplied to it has to support the tel: or sms: scheme and can only be used for starting a phone call or writing messages. It cannot be used to open any other applications, especially not 3rd party ones.

watchOS doesn't support URL schemes at the moment (as of watchOS 5), unlike iOS, so you won't be able to open other apps from your app's WatchKit Extension.

How to tap button to open URL with the UIButton in the Table View cells?

Create outlet for the button inside the cell then in cellForRowAt

cell.btn.addTarget(self,#selector(clicked),for:touchUpInside)
cell.btn.tag = indexPath.row

Add this inside the vc

@objc func clicked (_ btn:UIButton) {
UIApplication.shared.open(URL(string:buyLink[btn.tag])!, options: [:], completionHandler: nil)
}


Related Topics



Leave a reply



Submit