The following code does not work:
override func loadView() {
self.view = webView
let url = NSURL(string: "https://itunes.apple.com/gb/app/economics-a-level/id1300094663?mt=8")!
webView.load(URLRequest(url: url as URL))
}
But if instead I put https://apple.com it does work.
I am using a test device.
So how can I direct a user to my app on the App Store?
You don't need a webview to open the app store, simply do this:
let url = "itms-apps://itunes.apple.com/app/id1300094663"
UIApplication.shared.open(URL(string: url)!)
Related
I'm trying to load local html5 files to Xcode to create an ios App. I tried using a template and it uses both local and web based paths to create a very nice ios app in Xcode.
I need to learn about how to create a a webview app that has normal features and can load my local html5 webapps into Xcode for compiling an ios app.
I tried the following code, and it works alright when loading a url based html5 page.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
https://developer.apple.com/documentation/webkit/wkwebview
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
I need to change the URL to local file path. The above code page from apple documentation suggests that I use load(_: ) to load local html files. I'm not sure how to use this function. So, I tried code samples from alternate sources, and here's some part of the code I'm trying with for the Webview app.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet var webView: WKWebView!
//var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
guard let fileUrl = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "www-WebsiteContent") else { return; }
//let urlPath = Bundle.main.url(forResource: "input", withExtension: "txt")
if fileUrl != "" {
let url = URL(fileURLWithPath: fileUrl)
//let url = init(_: fileURLWithPath, path: "www-WebsiteContent/index.html")
webView.loadFileURL(url, allowingReadAccessTo : url)
let request = URLRequest(url: url)
webView.load(request)
}
//let myURL = URL(string:"file:///www-WebsiteContent/index.html")
//let myRequest = URLRequest(url: myURL!)
//webView.load(myRequest)
//webView.loadFileURL(URL: , webView.allowingReadAccessTo readAccessURL: URL)
}}
I'm using latest Xcode version on Macbook Air with latest Swift 5 version. There's a main storyboard with the webview object, and a viewcontroller.swift file.
The error is that when I run the project, it doesn't display the index.html file in the www-WebsiteContent folder. Maybe I wasn't able to connect the webview overlay to the viewcontroller.swift file properly.
Things I need to understand:
Can I run the above code and compile it as a complete ios app? Or would I need to edit AppDelegate or SceneDelegate files too? I've only edited the viewcontroller.swift file as a new project.
How can I resize the webview overlay to automatic width and height of the ios device? I have an html5 webapp files that I need to convert to an ios app. How can I create this webview so that it fits all the device types screen sizes? My html5 app fits perfectly when viewed in the device browser.
So I have this link
https://api1341.mr009.com/game/ptLaunchGame?agent=mobile&gameCode=art&gameType=8&sig=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEyMTc4NDQsImxvZ2luTmFtZSI6ImRldnRlc3Q0NyIsImlhdCI6MTUzMTM5MTQ2OH0.0Ao0O9dPMWvWAHeYE1j4w5f2uLN7C32oMmVXbvTI1qQ
Which I try to run on a webview, but it doesn't work.
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: urlString)
let request = URLRequest(url: url!)
webView.loadRequest(request)
}
It only works when I launch the link outside the app, such as safari browser.
Oh by the way, I can only use Webview as a requirement. No WebKitView.
It's caused by this line
webView.loadHTMLString(urlString, baseURL: nil)
That function is supposed to be used to load a local .html file, not a website from an URL. What you're doing is beginning to load a request using
webView.loadRequest(request)
and then immediately cancel it, by attempting to load a local .html file using
webView.loadHTMLString(urlString, baseURL: nil)
which obviously fails, because that's an URL, not a .html file.
Just remove
webView.loadHTMLString(urlString, baseURL: nil)
Call delegate of webview equal to self also
webView.delegate = self
self.webView.loadRequest(URLRequest(url: URL(string: self.link)!))
I am facing this strange issue. When I use url1, webView works fine. But when I use url2, webView sometimes doesn't load anything & only blank screen is displayed.
What should I do to solve this issue?
let url1 = "https://www.apple.com"
let url2 = "https://itunes.apple.com/us/music-video/despacito-feat-daddy-yankee/id1194807248"
override func viewDidLoad()
{
super.viewDidLoad()
let url = URL(string: url2)
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
If you are testing on simulator, & trying to open an iTunes URL in WebView, then the webpage is not loaded in webview.
If you testing on device, then the webpage for iTunes URL gets loaded in webview.
The below url
"https://itunes.apple.com/us/music-video/despacito-feat-daddy-yankee/id1194807248"
not handled by the UIWebView to load in simulator so try on device or open using UIApplication.shared.open(url!, options: [:], completionHandler: nil) mathod.
I am working on the simulator any there are no apps like adobe to open the pdf files....i am using UIDocumentInteractionController to open the pdf file like below.
let url=URL(fileURLWithPath: "/Users/sai/Library/Developer/CoreSimulator/Devices/425D8615-ADEC-4334-A639-C30B1E675EFA/data/Containers/Data/Application/AB3787BF-F6AC-4111-9847-A0FDC4E899F8/tmp/samplepdf.pdf")
print("--------------------------------- str is \(url)")
if (url != nil) {
// Initialize Document Interaction Controller
self.documentInteractionController = UIDocumentInteractionController(url: url)
// Configure Document Interaction Controller
self.documentInteractionController?.delegate = self
// Preview PDF
self.documentInteractionController?.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
}
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
My screen is getting displayed like below
No pdf is being displayed here...is it because there is no app in my simulator which could open pdf files?
Not sure if any Simulator apps will handle pdf documents - maybe News? But you can just add a webView to a view controller and use that to view your pdf.
#IBOutlet weak var webView: UIWebView!
Do the usual safety checks on your url and then:
webView.loadRequest(NSURLRequest(url: url as URL) as URLRequest)
I think there is a path issue.
Replace
let url=URL(fileURLWithPath: "/Users/sai/Library/Developer/CoreSimulator/Devices/425D8615-ADEC-4334-A639-C30B1E675EFA/data/Containers/Data/Application/AB3787BF-F6AC-4111-9847-A0FDC4E899F8/tmp/samplepdf.pdf")
With
let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("samplepdf.pdf")
for Swift 3/Xcode 8
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("samplepdf.pdf") as NSURL
Hope that will help.
I'm trying to create a simple webview app of my website, I load the url, make some actions (saving cookies) without problems but, when I close and re-open my app, all cookies are destroyed.
Where is the problem?
Thanks.
super.viewDidLoad()
let urlString = "http://my.url"
let url = NSURL(string: urlString)
let request = NSMutableURLRequest(URL: url!)
webView.loadRequest(request)