iPhone call from app in Swift Xcode 6 [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I checked and tried previous answers on how to make call directly from the app but I keep getting errors. I just made a button in the storryboard and hooked it up to the view controller. It keeps giving me the ERROR Excpected ',' separator but that doesn't solve anything. Do I need to add code to the AppDelegate or anything else? I really would like to know the way to do this in Swift.
import UIKit
class ViewController: UIViewController {
#IBAction func theCallMeButtonMethod(sender: AnyObject) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"telprompt:0123456789"]]
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Swift 3
This code will help you :
let phone = "tel://982374234"
let url = URL(string: phone)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
OR
let url = URL(string: "tel://9809088798")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)

This is how you access the application object and open a URL in swift.
Replace number below:
UIApplication.sharedApplication().openURL(NSURL(string:"telprompt:0123456789"))

Related

Reloading UITableViewController from outside a function called in viewDidLoad [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am using a UITableViewController and need to reload the table within a function. When I run the app the table view is not reloading. I am receiving no errors.
CODE:
func loadTheaters() {
let api = ""
let theaterId = id
let date = date
let url = URL(string: "http://data.tmsapi.com/v1.1/theatres/\(theaterId)/showings?startDate=\(date)&api_key=\(api)")
print(url!)
let request = URLRequest(
url: url! as URL,
cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData,
timeoutInterval: 10 )
let session = URLSession (
configuration: URLSessionConfiguration.default,
delegate: nil,
delegateQueue: OperationQueue.main
)
let task = session.dataTask(with: request, completionHandler: { (dataOrNil, response, error) in
if let data = dataOrNil {
do { let filmList = try! JSONDecoder().decode([Films].self, from: data)
self.films = filmList
self.tableView.reloadData()
}
}
})
task.resume()
}
Why is the tableview not reloading?
I had numberOfSections returned to 0.
Solved:
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
You should reload tableView in the main queue,
DispatchQueue.main.async {
self.tableView.reloadData()
}
post this please check your numberOfSections and numberOfRowsInSection dataSource methods if they are handled properly

How to detect when media player is done playing? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
This may me a simple question, but how do I detect when MPMediaPlayer is done playing in Swift 4?
You can track this using NSNotificationCenter.
func play(url: NSURL) {
let item = AVPlayerItem(URL: url)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)
let player = AVPlayer(playerItem: item)
player.play()
}
func playerDidFinishPlaying(note: NSNotification) {
// Your code here
}

How to activate a function after a given time? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would like to activate a function after a sound is played and complete. I assume that NSTimer needs to be included, but I am not sure of how to implement in order to make it. It is helpful if you could give me some code that works.
You should post code that you tried, and then someone then can help you make the code work. In this case, you don't need a timer for what you're doing. The AVAudioPlayerDelegate protocol on the player was designed for what you are trying to do:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer:AVAudioPlayer?
func playSound(fileName:NSString)
{
let path = NSBundle.mainBundle().pathForResource(fileName, ofType:"wav")
let url = NSURL.fileURLWithPath(path!)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
audioPlayer?.delegate = self
audioPlayer?.play()
} catch {
print("Player not available")
}
}
//MARK : AVAudioPlayerDelegate
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
print("finished playing, do something else here")
}
}

How to get someones current location in Swift iOS Programming? [duplicate]

This question already has answers here:
CLLocation Manager in Swift to get Location of User
(13 answers)
Closed 6 years ago.
I'm trying to figure out how to get someones current location using SWIFT for a iOS app in XCODE.
I was following this tutorial here, but for some reason my code isn't working. I think it's because I am using a more updated Xcode versioning. For some reason my delegate has different parameters and I'm not sure why, but when I build my program it isn't even asking me if I want to allow location services at all.
Here is my code
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization();
self.locationManager.startUpdatingLocation();
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// GPS STUFF
// UPDATE LOCATION
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!) { (placemarks, ErrorType) -> Void in
if(ErrorType != nil)
{
print("Error: " + ErrorType!.localizedDescription);
return;
}
if(placemarks?.count > 0)
{
let pm = placemarks![0] ;
self.displayLocationInfo(pm);
}
}
}
// STOP UPDATING LOCATION
func displayLocationInfo(placemark: CLPlacemark)
{
self.locationManager.stopUpdatingLocation();
print(placemark.locality);
print(placemark.postalCode);
print(placemark.administrativeArea);
print(placemark.country);
}
Also, in info.plist I also added NSLocationWhenInUseUsageDescription.
Any advice would be great
In your Info.plist, add this:
or you can add like this in your Info.plist:
<key>NSLocationAlwaysUsageDescription</key>
<string>To get current location</string>
This will ask you to allow location services.
Hope this help you.

I want to make a sound play starting from the 15th second [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to make a sound play starting from the 15th second.
How can I do it programmatically?
Thanks!
As I can understand you are trying to start your audio not from the beginning. In this case:
You need to use seekToTime
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let audioUrl = URL(string: "https://www.ringtonemobi.com/storage/upload/user_id_1/iphone-5-alarm-2016-08-21-01-49-25.mp3")!
player = AVPlayer(url: audioUrl)
player.seek(to: .init(seconds: 15, preferredTimescale: 1))
player.play()
}
}

Resources