blank screen appears when click on UILabel in swift - ios

i'm trying to open iPhone call screen with selected number when click on UIlabel which is hyperlink in swift.
a blank screen appears for 1 second and goes. No iPhone call screen appears. Don't know what is wrong in my code.
Code in viewDidLoad
let strPhone = "080 2854 2105"
let attributedPhoneStr = NSMutableAttributedString(string:strPhone, attributes:[NSLinkAttributeName: NSURL(string: "http://aerospace.honeywell.com")!])
lblPhoneContact.attributedText = attributedPhoneStr
lblPhoneContact.userInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed"))
lblPhoneContact.addGestureRecognizer(gestureRecognizer)
Function labelPressed
func labelPressed() {
let url:NSURL = NSURL(string: "tel://08028542105")!
UIApplication.sharedApplication().openURL(url)
}
what is wrong or missing in my code.
I tried running on my iPhone device.

for swift use
let url:NSURL = NSURL(string: "telprompt:08028542105")!
instead of
let url:NSURL = NSURL(string: "tel://08028542105")!

You can try to use the GCD's dispatch_async to invoke the "openURL" method:
dispatch_async(dispatch_get_main_queue()) { () -> Void in
UIApplication.sharedApplication().openURL(url)
}

You should omit the // from the uri - I.e. It should just be
let url:NSURL = NSURL(string: "tel:08028542105")!

Related

Facebook app invitation iOS SDK can't display the Invitation VC

So this's my code for app invite :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}
This code works fine but if I try to add the promotional code like this :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
content.promotionCode = "preview"
content.promotionText = "Use the *preview* code to unlock the app"
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}
The the invite VC is not shown any more (the function is called but nothing is showing). What did I missed here ?
The issue was that I've used special character like * so removing it make the app works fine my final code is like this :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
content.promotionCode = "preview"
content.promotionText = "Use the preview code to unlock the app"
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}

How to display PDF files from remote location on UIWebView using Swift?

I'm trying to display a pdf file, using an iOS app, that is located on our website. Here's how I'm doing this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print("preparing segue")
if segue.identifier == "PdfWebView" {
print("Found segue")
let detailViewController = segue.destinationViewController as! PdfViewController
let myIndexPath = self.tableView.indexPathForSelectedRow!
let row = myIndexPath.row
//Pass selected cell title to next View
detailViewController.website = pdfLinks[row]
print("Website: ", detailViewController.website)
}
}
// This is on pdfViewController
// The webiste = http://www.example.com/some-pdf-document-to-display.pdf
if let website = website {
print("Getting pdf: ",website)
if let pdf = NSBundle.mainBundle().URLForResource(website, withExtension: "pdf", subdirectory: nil, localization: nil){
print(pdf) // <-- this is always nil
let req = NSURLRequest(URL: pdf)
webView.loadRequest(req)
self.view.addSubview(webView)
}
}
What am I doing wrong? Needless to say, this is my very first app that I'm developing.
If the file is not in your app you should not use NSBundle.
let pdfUrl = NSURL(string: website)
let req = NSURLRequest(URL: pdfUrl)
I'm also not sure why you are adding the webView subview to the view. Shouldn't that be done somewhere else? Shouldn't you push a controller with the webview instead?
File is not in your Bundle. Instead you can directly load the URL.
let website = "http://www.sanface.com/pdf/test.pdf"
let reqURL = NSURL(string: website)
let request = NSURLRequest(URL: reqURL!)
webView.loadRequest(request)

Controlling UIWebView

In my IOS app i am using a webview to play audio from my website. My problem is that webview plays automatically when i don't even click on the webview. It says in the ios docs that mediaPlaybackRequiresUserAction is equal to true by default but it still plays. I have tried even setting mediaPlaybackRequiresUserAction to true in my code but that still doesn't solve the problem. I need to stop it from playing without user action because when the user goes to the next screen the webview automatically plays the audio. I have looked around on stack overflow and none of the solutions i found seem to help with this problem.
Here is my code:
//function to get NSURLrequest
func configureView() -> NSURLRequest {
let fileNameDownload: AnyObject = self.detailItem!
let fileNameURL = (fileNameDownload as NSString)
let spaces = fileNameURL.componentsSeparatedByString(" ")
if (spaces.count > 0) {
let fileNameURL = fileNameURL.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, fileNameURL.length))
println(fileNameURL)
let url = NSURL(string: fileNameURL)
let request = NSURLRequest(URL: url!)
return request
}
else {
let url = NSURL(string: fileNameURL)
let request = NSURLRequest(URL: url!)
return request
}
}
override func viewDidLoad() {
super.viewDidLoad()
previewWebView.loadRequest(configureView())
}

iOS 8.1 killed a part of my webview. Help wanted

I updated to iOS 8.1 and updated my xcode but now something is broken on with my webview.
I used this part of code:
let stream = "http://stream.hive365.co.uk:8088/live"
let url = NSURL(string: stream)
let request = NSURLRequest(URL: url)
webplayer.loadRequest(request)
xcode gives a error about the let request
value of optional type NSURL? not unwrapped; did you mean to use! or ?
I'm new with iOS swift development and can't get the error away...
any help would be appreciated!
NSURL returns an optional so you need to check if it is nil
let stream = "http://stream.hive365.co.uk:8088/live"
if let url = NSURL(string: stream) {
let request = NSURLRequest(URL: url)
webplayer.loadRequest(request)
}
change
let url = NSURL(string: stream)
to
let url = NSURL(string: stream)?
or
let url : NSURL = NSURL(string: stream) as NSURL

MPMoviePlayerController doesn't display video

I saw a lot of topics there and always the same problem: it's not displaying at all.
So because I am little stubborn, I want it more
This is my code :
let moviePath = NSBundle.mainBundle().pathForResource("back", ofType:"mp4");
println(moviePath!)
let movieURL = NSURL.fileURLWithPath(moviePath!)
let theMoviePlayer = MPMoviePlayerController(contentURL: movieURL!)
self.view.addSubview(theMoviePlayer.view)
theMoviePlayer.prepareToPlay()
theMoviePlayer.shouldAutoplay = true
theMoviePlayer.fullscreen = true
theMoviePlayer.play()
I checked and I have the movie file in the app's bundle resources.
The video dimension : 640 × 360 pixels
So where it can be from?
Did I forget something important ?
Edit
This new code works but always some problems :
let url = NSBundle.mainBundle().URLForResource("back", withExtension: ".mp4")
let player = MPMoviePlayerViewController(contentURL: url)
presentMoviePlayerViewControllerAnimated(player)
player.moviePlayer.prepareToPlay()
player.moviePlayer.repeatMode = MPMovieRepeatMode.One
player.moviePlayer.shouldAutoplay = true
player.moviePlayer.play()
It show me the video in landscape view and not full screen.
This is how I did it in one of my apps. You might need to call presentMoviePlayerViewControllerAnimated() and pass in the MPMoviePlayerViewController instead of doing addSubview.
let url = NSBundle.mainBundle().URLForResource("back", withExtension: ".mp4")
let player = MPMoviePlayerViewController(contentURL: url)
presentMoviePlayerViewControllerAnimated(player)
player.prepareToPlay()
player.shouldAutoplay = true
player.fullscreen = true
player.moviePlayer.play()
** Resolved **
I resolved addend the line :
player.moviePlayer.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI / 2))
Of course, it do a rotation of the video. So If it's a simple background animated like clouds or something simple, it's ok.
if there is no issue with the content type of video, call below function. It just works. :)
private func presentVideoVC(){
var url: NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
var moviePlayerVC: MPMoviePlayerViewController! = MPMoviePlayerViewController(contentURL: url)
self.presentMoviePlayerViewControllerAnimated(moviePlayerVC)
}
my two cents:
in case of ios9 use https.. (or add exceptions... not good.)
dont call directly on viewDidload, it does not work, call in async to wait some time, allowing controller to be set:
dispatch_async(dispatch_get_main_queue()){
self.playIt()
}
func playIt() {
let urlString = "https://www.ingconti.com/softysit/Spinelli_short.mp4"
let url: NSURL = NSURL(string: urlString)!
let moviePlayerVC: MPMoviePlayerViewController! = MPMoviePlayerViewController(contentURL: url)
moviePlayerVC.moviePlayer.shouldAutoplay = true
self.presentMoviePlayerViewControllerAnimated(moviePlayerVC)
}
note that now MPMoviePlayerViewController is deprecated.

Resources