First of all I know there are some similar topics as this one but because of my reputation I couldn't comment on those for help and stack overflow warned me not to ask for help from the answers section.. none of the similar posts have answered my question so here I go.
As can be understood from the topic, I want make a phone call on click,
I'm making an app for my business and I want to put in a call button so that people can call me over the app.
here are the attempts I've tried as read from the similar topics:
let phoneNumber = "1234567890"
if let phoneCallURL = NSURL(string: "tel:\(phoneNumber)") {
let application = UIApplication.sharedApplication()
if application.canOpenURL(phoneCallURL) {
application.openURL(phoneCallURL)
}
else{
println("failed")
}
}
so when I run the above code with a phone number it prints out the failed message on the console seems like i fails opening the URL
The other code I've tried is a very similar one
var url:NSURL = NSURL(string: "tel://phoneNumber")!
UIApplication.sharedApplication().openURL(url)
one other question is that: What is the correct syntax for the NSURL?
this
NSURL(string: "tel://\(phoneNumber)")
or this
NSURL(string: "tel//:\(phoneNumber)")
My last question is: If the app manages to make a call, does it appear on the simulator like a calling screen? I'm very new to swift programming and I apologise if the questions seem stupid..
The simple format for a tel: URL is tel:######. / is not a number. You probably mean this to be:
NSURL(string: "tel:\(phoneNumber)")
assuming phoneNumber is a string containing the phone number.
The tel: scheme is defined in RFC2806. You can look there for details on all its expected features.
Note that phone calls are not possible in the simulator, so you'll receive an error if you try to open a tel: URL there (unless you handle the URL yourself by registering an NSURLProtocol).
If you want to return to your app after your call has been ended (which you should do) then you need to use telprompt:// instead of tel://. The tel:// will take you to the home screen after the call.
Better use this:
var url:NSURL = NSURL(string: "telprompt://1234567891")!
UIApplication.sharedApplication().openURL(url)
let phoneNumber = "0507712323"
if let callNumber = phoneNumber {
let aURL = NSURL(string: "telprompt://\(callNumber)")
if UIApplication.sharedApplication().canOpenURL(aURL) {
UIApplication.sharedApplication().openURL(aURL)
} else {
print("error")
}
}
else {
print("error")}
I have this issue for different reasons and I would like share with you .
First Dont try in simulator must try on real device
Second make sure the passed number dont contain space
here is example
private func callNumber(phoneNumber:String) {
// I add this line to make sure passed number wihthout space
let CleanphoneNumber = phoneNumber.stringByReplacingOccurrencesOfString(" ", withString: "")
if let phoneCallURL:NSURL = NSURL(string: "tel://\(CleanphoneNumber)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL(phoneCallURL)) {
application.openURL(phoneCallURL);
}
}
}
Your code looks correct.It seems that it would always fail if you test it in simulator.
Try to use your iPhone to run it,and it would go to dialer interface as you want.
i added some additional validation in the code
func makeCall(constactNumber : NSString)
{
if(constactNumber.length == 0)
{
print("Contact number in not valid")
}
else
{
let CleanconstactNumber = constactNumber.stringByReplacingOccurrencesOfString(" ", withString: "")
if let phoneCallURL:NSURL = NSURL(string: "tel://\(CleanconstactNumber)")
{
if (UIDevice.currentDevice().model.rangeOfString("iPad") != nil)
{
print("Your device doesn't support this feature.")
} else
{
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL(phoneCallURL))
{
let mobileNetworkCode = CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode
if( mobileNetworkCode == nil)
{
print(" No sim present Or No cellular coverage or phone is on airplane mode.")
}
else
{
application.openURL(phoneCallURL);
}
}
}
}
}
}
Related
I am building my first ever IOS app with the help of online resources. I was able to build the signup and login screens of the app and connect it to firebase and store user's information on there. However, I want to get (retrieve) user's first name from the database when they log in and display that, and I found a piece of code that successfully does that but it is very slow, everything else in the page loads before it, so it is not really an ideal solution. I was wondering if there was another way to achieve what I want to do.
Code that checks if user's is signed in and gets its first name:
func CheckIfUserIsSignedIn() {
let db = Firestore.firestore()
if let userId = Auth.auth().currentUser?.uid {
let userName = db.collection("users").getDocuments() { (snapshot, error) in
if let error = error {
self.errorLabel.textColor = UIColor.red
self.errorLabel.text = "Error getting documents: \(error)"
}
else {
for document in snapshot!.documents {
if let firstUserDoc = snapshot?.documents.first {
let welcomeName = firstUserDoc["first_name"] as! String
self.errorLabel.text = "Hey, \(welcomeName) welcome!"
}
}
} //end else
}
} //end if
else {
//User is not logged in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "login")
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true)
}
} //end CheckIfUserIsSignedIn method`
There are a few ways you can do this but just one thing I want to point out is that when you check if the user has logged in with if let userId = Auth.auth().currentUser?.uid you then proceed to retrieve every user document with let userName = db.collection("users").getDocuments() which is probably making it much more slower than it needs to be. And this will get slower the more popular your app becomes because downloading more users takes more time. This is an easy fix by just adding one small thing:
let userName = db.collection("users").document(userId).getDocument()
This only gets 1 document instead of all.
Also right after that you loop through each document you've retrieved and perform
if let firstUserDoc = snapshot?.documents.first {
let welcomeName = firstUserDoc["first_name"] as! String
self.errorLabel.text = "Hey, \(welcomeName) welcome!"
}
This block is run snapshot!.documents.count (Number of users you have in your app) amount of times which again seems unnecessary as it does the same thing each iteration. Remove the loop and doing it 1 time will be so much faster.
This is how your code should look after:
if let userId = Auth.auth().currentUser?.uid {
db.collection("users").document(userId).getDocument { docSnapshot, error in
if let error = error {
errorLabel.textColor = UIColor.red
errorLabel.text = "Error getting documents: \(error)"
} else {
let welcomeName = docSnapshot!.get("first_name") as! String
errorLabel.text = "Hey, \(welcomeName) welcome!"
}
}
} //end if
// ...Other code
This should work but if you want an even faster way to do this and don't use the Auth.auth().currentUser!.displayName property then you can store their first name in that and simply reduce your code to:
if let userFirstName = Auth.auth().currentUser?.displayName! {
errorLabel.text = "Hey, \(userFirstName) welcome!"
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "login")
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true)
}
^ This would be ideal if your app refers to all users by their first names instead of their usernames (plus it'll show up in verification emails as well)
One last thing I'd like to mention is in your original post I don't understand how you were guaranteeing the first document of the snapshot to be the user you want. If it's an old user using the app then someone else's name would probably come up as newer users will be at the top of the list. As this is your first app I want to stress the importance of writing tests for your functions. Be sure to read up on Unit Tests and UI Tests (mainly Unit Tests for your purposes), they really make a big difference. It's not too hard to learn either. I remember when I first started I avoided them like the plague because I thought they took too much time. But in the long run they save you thousands of hours by making your code as bug free as possible and even help structuring your code better by making it more modular!
Hope this helps and best of luck with your first app!
In my App the Pink heart want to go to the red one! But as you see the way is blocked.
The code I use is:
let solution = GridGraph.findPath(from: GridGraph.node(atGridPosition:int2(Int32(pinkHeart.positon.x),Int32(pinkHeart.positon.y)))!,to:GridGraph.node(a[enter image description here][1]tGridPosition:int2(Int32(redHeart.positon.x),Int32(redHeart.positon.y)))!) as! [GKGridGraphNode]
But the Application always crashes because there is no way!
If I want to find a path from the pink heart to the yellow/green one the code works.
I tried many different ways to solve the problem but nothing seems to work...
e.g.:
do {
let solution = try GridGraph.findPath(from:GridGraph.node(atGridPosition:int2(Int32(pinkHeart.positon.x),Int32(pinkHeart.positon.y)))!, to:GridGraph.node(atGridPosition: int2(Int32(redHeart.positon.x),Int32(redHeart.positon.y)))!) as! [GKGridGraphNode]
} catch {
print("no path found")
}
or
if let solution = GridGraph.findPath(from:GridGraph.node(atGridPosition:int2(Int32(pinkHeart.positon.x),Int32(pinkHeart.positon.y)))!, to:GridGraph.node(atGridPosition: int2(Int32(redHeart.positon.x),Int32(redHeart.positon.y)))!) as! [GKGridGraphNode] {
//Do something!
}
I tried many more possible solutions, but it always crashed...
Thank you for your help!
Thats the image:
You can do it this Way:
var solutionPath: [GKGridGraphNode]? {
let solution = GridGraph.findPath(from: OneNode, to: anotherNode) as! [GKGridGraphNode]
if solution.isEmpty {
print("There is no possible path!")
return nil
}
else {
return solution
}
}
No idea what is happening here since I've followed other answers that use this including Swift: synchronously perform code in background; queue.sync does not work as I would expect
I have an XMLParser that I need to load 2 RSS feeds. To prevent reentrant parsing, I need to load these URLs one after the other is done. Problem is I need to do this in the background/async so it doesn't freeze up the UI.
I have this:
var myParser: XMLParser = XMLParser()
let parseQueue = DispatchQueue(label: "xmlQueue", attributes: [], target: nil)
var feeds = [RSS_FEED_URL_COT, RSS_FEED_URL] //this order
self.parseQueue.async {
for f in feeds
{
self.loadRSSData(rssFeed: f)
}
}
This does work meaning no reentrant error, but for a good 30 seconds whole UI is frozen up. What am I doing wrong here?
EDIT:
func loadRSSData(rssFeed: String){
if let rssURL = URL(string: rssFeed) {
print("LOADING THE URL: ", rssURL)
// fetch rss content from url
if let contents = XMLParser(contentsOf: rssURL)
{
self.myParser = contents
}
// set parser delegate
self.myParser.delegate = self
self.myParser.shouldResolveExternalEntities = false
// start parsing
self.myParser.parse()
}
}
#Sjyguy,
Kindly use the loadRSSData like below.
func loadRSSData(rssFeed: String){
if let rssURL = URL(string: rssFeed) {
print("LOADING THE URL: ", rssURL)
// fetch rss content from url
if let contents = XMLParser(contentsOf: rssURL)
{
self.myParser = contents
}
// set parser delegate
self.myParser.delegate = self
self.myParser.shouldResolveExternalEntities = false
**DispatchQueue.main.async{
// start parsing
self.myParser.parse()
}**
}
}
Hope it worked!
I'm working on a macOS cocoa-app in Swift where I import several different file types into the app for the user to interact with.
I'm currently trying to determine if it's possible to implement the "Open file with" feature, so that the user could open those files in a different program if they wanted to:
I've found a few different SO questions that seem tangentially related to what I'm trying to do:
Swift: How to open file with associated application?
Launch OSX Finder window with specific files selected
...but so far nothing to indicate if it's possible to implement right-click Finder/file (?) access in the way I had in mind.
Apologies if this is too vague of a question; any help / guidance appreciated!
Without going into details, it's pretty straight forward:
Get the list of all known applications that can open a specific file type (see LSCopyApplicationURLsForURL, a Core Foundation C function).
Build the menu. You can use NSWorkspace (and probably URL) to get the application icons.
Use NSWorkspace.openFile(_:withApplication:) to tell the application to open the given document.
2022, Swift 5
Get app list associated with local file:
func getAppsAssociatedWith(_ url: URL?) {
guard let url = localFileURL,
let retainedArr = LSCopyApplicationURLsForURL( url as CFURL, .all)?.takeRetainedValue(),
let listOfRelatedApps = retainedArr as? Array<URL>
else {
return []
}
return listOfRelatedApps
}
Getting thumbnail for app:
let singleAppIcon = NSWorkspace.shared
.icon(forFile: appUrl.path)
.scaledCopy(sizeOfLargerSide: 17)
Open url with app:
#available(macOS 10.15, iOS 9.0, *)
public class func openUrlWithApp(_ urls: [URL], appUrl: URL) {
NSWorkspace.shared.open(urls, withApplicationAt: appUrl, configuration: NSWorkspace.OpenConfiguration())
}
In my app I'm cashing all apps icons in dictionary.
[someFile localURL : app icon]
If I have already got icon earlier - no need to get it once more
var relatedAppsThumbnails: [URL: Image] = [:]
func updateRelatedApps() {
guard let url = currImgUrl, // file url to get icons from related apps
let retainedArr = LSCopyApplicationURLsForURL( url as CFURL, .all)?.takeRetainedValue(),
let listOfRelatedApps = retainedArr as? Array<URL>
else {
relatedApps = []
return
}
self.relatedApps = listOfRelatedApps
// add app icon in case of it wasn't added yet
for appUrl in listOfRelatedApps {
if relatedAppsThumbnails[appUrl] == nil {
let nsImg = NSWorkspace.shared.icon(forFile: appUrl.path)
.scaledCopy(sizeOfLargerSide: 17)
relatedAppsThumbnails[appUrl] = Image(nsImage: nsImg)
}
}
}
LSCopyApplicationURLsForURL is deprecated. You can use this alternative:
func getListOfExternalApps(forURL url: URL) -> [(URL, Image)] {
let listOfExternalApps = NSWorkspace.shared.urlsForApplications(toOpen: url)
let icons = listOfExternalApps.map {
let nsimage = NSWorkspace.shared.icon(forFile: $0.path())
nsimage.size = CGSize(width: .s16, height: .s16)
return Image(nsImage: nsimage)
}
return Array(zip(listOfExternalApps, icons))
}
I have converted the code for making a phone call from Objective-C to Swift, but in Objective-C, we can set the type of the URL that we like to open (e.g. telephone, SMS, web) like this:
#"tel:xx"
#"mailto:info#example.es"
#"http://stackoverflow.com"
#"sms:768number"
The code in Swift is:
UIApplication.sharedApplication().openURL(NSURL(string : "9809088798")
I read that have not released any scheme parameter for tel:, but I don't know if Swift can detect if the string is for making a phone call, sending email, or opening a website. Or may I write:
(string : "tel//:9809088798")
?
I am pretty sure you want:
UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)
(note that in your question text, you put tel//:, not tel://).
NSURL's string init expects a well-formed URL. It will not turn a bunch of numbers into a telephone number. You sometimes see phone-number detection in UIWebView, but that's being done at a higher level than NSURL.
EDIT: Added ! per comment below
A self-contained solution in Swift:
private func callNumber(phoneNumber:String) {
if let phoneCallURL:NSURL = NSURL(string:"tel://\(phoneNumber)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL(phoneCallURL)) {
application.openURL(phoneCallURL);
}
}
}
Now, you should be able to use callNumber("7178881234") to make a call.
For Swift in iOS:
var url:NSURL? = NSURL(string: "tel://9809088798")
UIApplication.sharedApplication().openURL(url!)
You need to remember to remove the whitespaces or it won't work:
if let telephoneURL = NSURL(string: "telprompt://\(phoneNumber.stringByReplacingOccurrencesOfString(" ", withString: ""))") {
UIApplication.sharedApplication().openURL(telelphoneURL)
}
"telprompt://" will prompt the user to call or cancel while "tel://" will call directly.
# confile:
The problem is that your solution does not return to the app after the
phone call has been finished on iOS7. – Jun 19 at 13:50
&# Zorayr
Hm, curious if there is a solution that does do that.. might be a
restriction on iOS.
use
UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://9809088798")!)
You will get a prompt to Call/Cancel but it returns to your application. AFAIK there is no way to return (without prompting)
You must insert "+"\ is another way
private func callNumber(phoneNumber:String) {
if let phoneCallURL:NSURL = NSURL(string:"tel://"+"\(phoneNumber)") {
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL(phoneCallURL)) {
application.openURL(phoneCallURL);
}
}
}
Small update to Swift 3
UIApplication.shared.openURL(NSURL(string: "telprompt://9809088798")! as URL)
The following code snippet can tell if the SIM is there or not and if the device is capable of making the call and if ok then it'll make the call
var info = CTTelephonyNetworkInfo()
var carrier = info.subscriberCellularProvider
if carrier != nil && carrier.mobileNetworkCode == nil || carrier.mobileNetworkCode.isEqual("") {
//SIM is not there in the phone
}
else if UIApplication.sharedApplication().canopenURL(NSURL(string: "tel://9809088798")!)
{
UIApplication.sharedApplication().openURL(NSURL(string: "tel://9809088798")!)
}
else
{
//Device does not have call making capability
}
For making a call in swift 5.1, just use the following code: (I have tested it in Xcode 11)
let phone = "1234567890"
if let callUrl = URL(string: "tel://\(phone)"), UIApplication.shared.canOpenURL(callUrl) {
UIApplication.shared.open(callUrl)
}
Edit:
For Xcode 12.4, swift 5.3, just use the following:
UIApplication.shared.open(NSURL(string: "tel://555-123-1234")! as URL)
Make sure that you import UIKit, or it will say that it cannot find UIApplication in scope.
For swift 3
if let phoneCallURL:URL = URL(string:"tel://\(phoneNumber ?? "")") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
application.open(phoneCallURL, options: [:], completionHandler: nil);
}
}
For swift 4:
func call(phoneNumber: String) {
if let url = URL(string: phoneNumber) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(phoneNumber): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(phoneNumber): \(success)")
}
}
}
Then, use the function:
let phoneNumber = "tel://+132342424"
call(phoneNumber: phoneNumber)
Swift 4 and above
let dialer = URL(string: "tel://5028493750")
if let dialerURL = dialer {
UIApplication.shared.open(dialerURL)
}
I prefer deferring the URL creation to the built-ins like:
var components = URLComponents()
components.scheme = "tel"
components.path = "1234567890"
print(components.url!) //Prints out: "tel:1234567890"
Which can then be used in UIApplication.shared.openURL