I want to display a local pdf file using UIDocumentInteractionController
Here is my code :
pole = Pole.getWithMinor(minorBeacon)
var address = pole.pdf
if pole != nil {
var urlpath = NSBundle.mainBundle().pathForResource(pole.pdf, ofType: "pdf")
let url : NSURL! = NSURL(string: urlpath!)
//pdfView.loadRequest(NSURLRequest(URL: url))
println(url)
let docController = UIDocumentInteractionController(URL : url)
docController.UTI = "com.adobe.pdf"
docController.delegate = self
docController.presentPreviewAnimated(true)
the error is :
2015-06-02 15:57:08.390 LePetitPoucet[4232:2026014] *** Assertion failure in -[UIDocumentInteractionController setURL:], /SourceCache/UIKit/UIKit-3318.93/UIDocumentInteractionController.m:1024
2015-06-02 15:57:08.391 LePetitPoucet[4232:2026014] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController: invalid scheme (null). Only the file scheme is supported.'
when i create a webWiew, the PDF is displayed well but i want to use pdf native viewer instead. Thanks for your help !
This one always gets me too
let url : NSURL! = NSURL(string: urlpath!) is wrong
you want
let url : NSURL! = NSURL.fileURLWithPath(urlpath!)
Related
I am trying to access the access the video files which are present in the iOS Files App. Trying to play videos using AVPlayer
I am able to access the videos but after some time when I try to access the same video I am getting the error from the AVPlayer.
let filemgr = FileManager.default
let docsDirURL = try! filemgr.url(for: .documentDirectory,
in: .userDomainMask, appropriateFor: nil, create: true)
let lastComp = docsDirURL.lastPathComponent
let pathy = "\(docsDirURL)"
if let urls = lap.videoURLs {
self.lap = lap
for url in urls {
let urlComps = "\(url)".components(separatedBy: "/")
var urlpath = ""
for (index, element) in urlComps.enumerated() {
if index > 9{
urlpath += element + "/"
}
}
print(urlpath)
var pathz = pathy.replacingOccurrences(of: lastComp, with: urlpath)
pathz = String(pathz.dropLast(2))
print(pathz)
let finalURL = NSURL(string: pathz)
// let playerItem = AVPlayerItem(url: url as URL)
let playerItem = AVPlayerItem(url: finalURL! as URL)
playerItem.addObserver(self, forKeyPath: "status", options: .new,
context: &itemStatusContext)
self.playerItems.append(playerItem)
if self.queuePlayer == nil {
self.queuePlayer = AVPlayer(playerItem: playerItem)
}
}
}
I expect to play videos normally as before but I can only access it for some time like one minute and then after I received an error.
Optional(Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x283368f90 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}) and URLS: [<AVPlayerItem: 0x283c1d4a0, asset = <AVURLAsset: 0x2878f41c0, URL = file:///var/mobile/Containers/Data/Application/0CE62F51-77E0-4630-A3C4-1CE615ACCD26/tmp/uk.co.racelogic.Circuit-Tools-for-iOS-Inbox/VBOX0005_0001.mp4>>, <AVPlayerItem: 0x283c1d850, asset = <AVURLAsset: 0x2878f5520, URL = file:///var/mobile/Containers/Data/Application/0CE62F51-77E0-4630-A3C4-1CE615ACCD26/tmp/uk.co.racelogic.Circuit-Tools-for-iOS-Inbox/VBOX0005_0002.mp4>>]
So as stated in error "No such file or directory" a file is absent. Regarding full path it stored inside temporary folder. Probably you imported these files via UIDocumentPickerViewController or similar component. You must copy provided file immediately (synchronously) in same code block where you get the URL to some local storage to avoid this situation.
On application launch i am loading https://www.google.com in WKWebView.
App has one button, on click of that button app is loading local html page from document directory.
i used following code to load html page.
let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask,
true)[0]
let fileName = "Demo.html"
let fullDestPath = URL(fileURLWithPath: destPath)
.appendingPathComponent(fileName)
self.webView! .loadFileURL(fullDestPath, allowingReadAccessTo: fullDestPath)
The code was working till iOS 12.1.4 however in iOS 12.2 it is not loading html page and throwing error
ProvisionalPageProxy::didFailProvisionalLoadForFrame: pageID = 1, frameID = 1, navigationID = 2
Check if the file Demo.html is present in the document directory. If is not present, then it will not load html page.
Try to load from Bundle
if let path = Bundle.main.path(forResource: "Demo", ofType: "html") {
let htmlURL = URL(fileURLWithPath: path)
webView.loadFileURL(htmlURL, allowingReadAccessTo: htmlURL)
} else {
print("File not found")
}
Getting an "unexpected found nil" error, but when checking the value - its there:
override func viewDidLoad() {
super.viewDidLoad()
if whichLink == "official link" {
let urlStr = videoGame.offLink!
let url = NSURL(string: urlStr)!
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}
else if whichLink == "moby game link" {
print("yo yo yo, value is here! \(videoGame.mgLink) ")
let urlStr1 = videoGame.mgLink!
let url1 = NSURL(string: urlStr1)!
let request = NSURLRequest(URL: url1)
webView.loadRequest(request)
}
}
I'm suspecting an error in storyboard... but can't locate anything.
Did anyone has a clue what can be wrong?
The full project can be found # https://github.com/flostik2008/Favorite-Games
Your URL string is incorrectly formatted with the space at the end, so the NSURL initialization is returning nil.
You should URL encode all raw strings before trying to create an NSURL:
let urlStr1 = videoGame.mgLink!.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! should work
I'm new in swift and i would like to get data from a mp3 file located on a server, ( i used an URL, exemple : http://myserver.eu/file.mp3 ).
i used some tuto, like http://geekyviney.blogspot.com/2015/02/extracting-data-like-thumbnail-images.html ( it's working with a local file and i'm trying to make it work with a file from an url)
i got a exception :
Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I1386_INVOP, subcode=0x0
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
I suppose that pathForResource is only for local file ? thanks for your help
var filePath = NSBundle.mainBundle().pathForResource("http://servername.eu/filename", ofType: "mp3")
var fileUrl = NSURL(fileURLWithPath: filePath!)
var asset = AVAsset.assetWithURL(fileUrl) as AVAsset
You're right, pathForResource is used to access files locally in the application bundle.
To load data from URLs with http scheme use this syntax
let urlString = "http://servername/filename"
let serverURL = NSURL(string: urlString)!
if let asset = AVAsset.assetWithURL(serverURL) as? AVAsset {
// do something with the asset
}
Swift 3 version:
let urlString = "http://servername/filename"
if let serverURL = URL(string: urlString) {
let asset = AVAsset(url: serverURL)
// do something with the asset
}
Ow and don't forget to add the correct import
import AVFoundation
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