How to download audio file? - ios

I want to download audio files (mp3, m4a, wma...) with Google Drive API. Trying following code, I got the error on simulator.
Error:
The operation could't be completed. (The user has not granted the app xxxx read access to the file alEnlflseiFNSEi.)
Code:
let url = "https://www.googleapis.com/drive/v3/files/\(identifier)?alt=media"
service.fetchObject(
with: URL(string: url)!,
delegate: self,
didFinish: #selector(GoogleDriveFileListTableViewController.downloadWithTicket(ticket:finishedWithObject:error:))
)
// Parse results and display
func downloadWithTicket(ticket : GTLServiceTicket, finishedWithObject response : GTLDriveFileList, error : NSError?) {
if let error = error {
showAlert(title: "Error", message: error.localizedDescription)
return
}
}

With regard to your "read access" problem here's what the Download files states:
Downloading the file requires the user to have at least read access.
Additionally, your app must be authorized with a scope that allows
reading of file content. For example, an app using the
drive.readonly.metadata scope would not be authorized to download the
file contents. Users with edit permission may restrict downloading by
read-only users by setting the viewersCanCopyContent field to true.
Try obtaining the webContentLink of the audio file using Files.get. When this link is clicked or opened in a new window, the audio file will be downloaded.
It looks something like this:
https://drive.google.com/uc?id=0Bzgk4zccNwI7TzBxZTFzbHFhdUU&export=download

Related

Unable to access Files, because there is no such file or directory

Good Day.
I receive a message with an image URL. Whenever I receive the image URL I have to show it through local notification. Like this
However, I use UNNotificationAttachment
convenience init(identifier: String,
url URL: URL,
options: [AnyHashable : Any]? = nil) throws
where it is mentioned that
The URL of the file you want to attach to the notification.
The URL must be a file URL and the file must be readable by the current process.
This parameter must not be nil.
However, when I receive the message, I download it first and then fire the local notification
func downloadImage(from remoteUrl: URL, completion: #escaping(URL?) -> Void) {
URLSession.shared.downloadTask(with: remoteUrl) { localURL, response, error
//move to the directory and return the URL
completion(document directory path I have saved)
}.resumeTask()
}
It successfully returns the local URL, and I am able to show the notification successfully.
I have saved the last path as I have already downloaded the image. I don't want to download it again.
But whenever I open the app and want to access the downloaded file, it appears with an error
Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. No such file or directory.
The following tests are done:
Both URL path is the same.
I downloaded the container after receiving the notification, Image is appeared on the notification but didn't find the image in the document directory.
So what is the best to handle the situation?
but didn't find the image in the document directory
Because you didn't save it to the document directory. downloadTask is volatile. The URL it downloads the file to is deleted as soon as you return from the completion handler. If you don't want to lose it you must copy it to a safer location.

How to handle a file sent with 'Open in...' from another app to my own iOS app?

I am developing an app that reads csv files and display its content in a tableViewController. When doing my testings using Xcode, I have a sample file within the projects directory and I am perfectly able to read that file using its location path. The problem that I have is I want to be able to take any csv file (sent to me via some method), click the 'Open in' button and have the file sent to my app.
My app is being displayed in the available applications to which I can send the file to. My question is what happens after? When I choose to send it to my app it then switches over to my app and from there, I don't know how to receive the file and read it to extract its content. Where does the file go and how do I interact with it?
This is handled in your AppDelegate, more precisely, you get passed an URL to the document and then you handle it from there in optional function, e.g.:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
do {
let data = try Data(contentsOf: url)
// Do something with the file
} catch {
print("Unable to load data: \(error)")
}
return true
}
More info: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application

Sharing to specific Facebook page using Swift SDK GraphSharer

In my iOS app I'm trying to use the Facebook Swift SDK so that a user can post a URL to a specific Facebook page that they manage.
I've managed to get the following code working - but it only shares the post to my home timeline:
let content = LinkShareContent(url: URL(string: urlString)!)
let sharer = GraphSharer(content: content)
sharer.failsOnInvalidData = true
sharer.completion = {
(result) in
print(result)
}
do {
try sharer.share()
}
catch (let error) {
print(error.localizedDescription)
}
I believe that I need to use the graphNode property to specify the destination page and that the correct format is as follows:
sharer.graphNode = "/\(pageId)"
However, when I set this property before posting, I am getting an error:
failed(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
I've tried a dozen different formats to specify the node but they all return the same error and I can't find any working examples.
I managed to resolved this issue. It turns out that the graphNode path above is correct but I was looking in the wrong place due to the error message I was getting. Instead I just needed to specify the accessToken parameter.
graphSharer.accesstoken = myPageAccessToken
Note: The access token is not the user's current access token, but an access token you can retrieve when calling the /me/accounts API.
var graphPath = "/me/accounts"
I also then had an issue posting to the page because I had only requested the publish_actions permission. From the documentation, this appears to be suitable but it only worked where I requested further permissions as follows:
loginManager.logIn(withPublishPermissions: ["publish_actions",
"manage_pages", "publish_pages"], ...
Hopefully this will be of use to someone in future. :-)

iOS Swift: Can't create pinterest pin with PinterestSDK

I am experiencing issue while trying to create pin with Pinterest SDK, I use this code:
PDKClient.sharedInstance().createPin(with: imageToShare, link: URL.init(string: "https://someUrl"), onBoard: "MyBoard", description: "",
progress: { (percent) in
}, withSuccess: { (response) in
print("Success")
}) { (error) in
if let error = error {
print(error)
}
}
And I receive this particular response:
Error Domain=com.alamofire.error.serialization.response Code=-1011
"Request failed: not found (404)" UserInfo
{NSLocalizedDescription=Request failed: not found (404),
NSErrorFailingURLKey=https://api.pinterest.com/v1/pins/
User is logged in into Pinterest application, just before calling this line of code, I am able to successfully create board with name MyBoard via the same API, but I am stuck within creation of the pin.
I tried to create pin with image url (because I thought that maybe it is because of my image), but it outputs the same response.
I am using twitter and facebook sdk within the same code, Info.plist seems allright.
Any clues?
Apparently the problem was, that onBoard actually requires not a board name, but it's identifier. So when you create pin board programatically -- (PDKClient.sharedInstance().createBoard) you should fetch and save it's identifier, which can be fetched in response (response.board().identifier).
I was stuck within this problem for the past 1.5 hours and figured it out just after posting the question :) I think it can be useful for someone else, because official SDK wiki is not very informative and git repository is quite dead too.

How do I use the YouTube Data API in my iOS app? I get "Access not configured" error on each request

I want to use the YouTube Data API to get the links to thumbnails for a specific YouTube video in my iOS app. I went to Google Developers Console and created a new project.
For that project I then went to "APIs" and removed the default APIs it enables for me and added the YouTube Data API. I then went to credentials, created a Public Key (as I don't need the user to log in or anything, just getting the thumbnail URLs) as iOS, which gave me an API key. I added the Bundle Identifier for my project under Allowed Applications.
Now in my app I tried the following code:
let test = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: "https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=*MYKEYHERE*&part=snippet,contentDetails,statistics,status")!, completionHandler: { (data, response, error) -> Void in
let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil)
println("JSON: \(json)")
})
But it prints out the error:
JSON: Optional({
error = {
code = 403;
errors = (
{
domain = usageLimits;
message = "Access Not Configured. Please use Google Developers Console to activate the API for your project.";
reason = accessNotConfigured;
}
);
message = "Access Not Configured. Please use Google Developers Console to activate the API for your project.";
};
})
What exactly am I doing wrong?
There is a known problem about Bundle id in the console. If you can just remove that restriction, it should just work fine.
You can track the original issue from here: https://code.google.com/p/gdata-issues/issues/detail?id=5770
Here's what you need, YouTube has a service API via web to consult this types of things. I'm using this URL to consult all videos from a user (getting every videos: URL, Thumbnail, views, duration, etc). I'm sure you'll find thumbnails for a particular video. Its a JSON response so you'll have to take a good look to the structure but can't get simpler than this.
http://gdata.youtube.com/feeds/api/users/USER_NAME/uploads?v=2&alt=json&#&start-index=1&max-results=10

Resources