Original Post November 15, 2016: Xcode 7.1.1/Swift 2.1:
This example shows how to download an online text file from Google drive to an iOS app. This example may also be used to for any text content on the web, such as open data in JSON format.
1. Create a file called test with its content as:
abc
2. Upload file test to Google drive.
Right click on the file and select "Share...".
Select "Copy link":
The link should be in this format:
https://drive.google.com/open?id=ABCDE...123
https://drive.google.com/uc?export=download&id=ABCDE...123
4. Modify the code below in ViewController.swift:
Update August 13, 2017: Xcode 8.3.3/Swift 3.1.
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://drive.google.com/uc?export=download&id=ABCDE...123"
let url = URL(string: urlString)
var webString : String = ""
do {
webString = try String(contentsOf: url!)
} catch {
print("Error")
}
print(webString)
}
Original Post November 15, 2016: Xcode 7.1.1/Swift 2.1:
Original Post November 15, 2016: Xcode 7.1.1/Swift 2.1:
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://drive.google.com/uc?export=download&id=ABCDE...123"
let url = NSURL(string: urlString)
NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in
//Convert NSData to NSString.
let webString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(webString!)
}.resume()
}
Related Information
Print out JSON content in Swift playground
Open an internet image and create a button with underline title
No comments:
Post a Comment