Share video to Instagram on iOS - ios

Is it possible to share a video on Instagram without saving it to the user's Camera Roll?
this is what i tried so far:
let instagramURL = URL(string: "instagram://app")!
if (UIApplication.shared.canOpenURL(instagramURL)) {
self.documentController = UIDocumentInteractionController(url: videoURL)
self.documentController.delegate = self
self.documentController.uti = "com.instagram.exlusivegram"
self.documentController.presentOpenInMenu(from: self.view.frame, in: self.view, animated: true)
} else {
print(" Instagram isn't installed ")
}
videoURL is the URL of the video i saves in the Documents folder.
if I save the URL with Instagram.igo at the end, then when i choose Instagram to share it opens like this:
if I save the video with .mov at the end, it seems that Instagram share opens with a photo (like video thumbnail) and not a video.

What exactly you did wrong is difficult to determine, but I'll try to address where you might have been wrong.
First of all make sure that the videoURL is defined locally. When I wrote an app posting to Instagram, I first defined the path with file manager, like:
let tempDirectory = FileManager().temporaryDirectory
var postingPath = tempDirectory.appendingPathComponent("postingVideo")
postingPath = postingPath.appendingPathExtension("igo")
I then wrote the movie into this directory and almost have the same code as you.
Do take into consideration that you need to create the igo folder and then save the movie into this folder. If you try to refer to the folder you have the movie in, your app will not read the movie file and nothing will be posted. When it is a movie file from the photo library, use an AVExportSession and export it to the postingPath.
self.instagramController = UIDocumentInteractionController.init(url: postingPath)
self.instagramController.uti = "com.instagram.exclusivegram"
self.instagramController.delegate = self
self.instagramController.presentOpenInMenu(from:
self.navigationItem.rightBarButtonItem!, animated: true)
However, you do need to make your class posting to instagram adhere to the UIDocumentInteractionControllerDelegate, otherwise you need cast self. You also need to include Social. I don't think you have done anything wrong in the second part of this post, because you would have gotten xcode errors and warnings.
I hope the first part can help you further, since I don't think the second part has given you problems.

Related

How to play audio files from Google Drive (Swift 5)

In my project I use a Google Drive API. I get a list of the user's files and can save them to the device memory.
I also want to be able to play audio files directly from Google Drive, but I just can't do it. The AVPlayer doesn't want to play the audio file using the URL link, which I transfer to him. But if I paste this link into any browser, then the track is played.
Has anyone encountered this and knows how to fix the problem? I would be grateful for any help.
My code looks like this (it's a simplified version):
import AVKit
class MyVC: UIViewController {
...
var player: AVPlayer!
...
private func playTrack() {
let url = URL(string: "https://drive.google.com/uc?export=open&id=XXXXX" //where XXXXX is an item id
let playerItem: AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
player!.play()
}
}
For example URL link which I transfer to the AVPlayer looks like this:
https://drive.google.com/uc?export=open&id=1JzuAhQ-7R01wLhtD8zEN_sC5nvFlt7wU
But if I paste it into the browser it automatically changes and looks like this:
https://doc-0g-1s-docs.googleusercontent.com/docs/securesc/anu2dlv0j1ugsoh1pb6b8qpkkt7k1j6k/hp3dnmru336i1cpkk4f5p4bstc83b30f/1602827400000/02426681720429398713/02426681720429398713/1JzuAhQ-7R01wLhtD8zEN_sC5nvFlt7wU?e=open&authuser=0&nonce=4dd9dhoo6he8g&user=02426681720429398713&hash=o87f0k3t4iv19ugae899urmcjd14ogiq
Maybe it's important.
You files should be shared by a direct link in Google Drive to play it so you can make it manually or using API e.g.:
And then use direct links in format: https://drive.google.com/uc?export=open&id=XXXXX.

How to load native album video using swift

I'm an undergraduate student and I'm witring an iPhone HumanSeg app. But now I have a problem, that I have a native video in album, and I need to load that video into my code and do some processing. My codes are below:
let filePath = Bundle.main.path(forResource: "1", ofType: "mp4")
let videoURL = NSURL(fileURLWithPath: filePath!)
let avAsset = AVAsset(url: videoURL as URL)
But when I run this code, Xcode just tells me that filePath is nil. I assert that 1.mp4 is in both Assets.xcaassets and iPhone album. Is there anyone who'd like to offer some help?
By the way, How can I get the images(in UIImage format) in the video at the fastest speed? For each image at given time, I really have to read it in no more than 5ms so I may output the preserved video at a good fps.
Check target's build phases, whether the file is being copied to the bundle.Also the check the box to include the file. This code is correct for fetching that file.

How to open a document URL in default ios APP using Swft 3?

I have a url document url list in a TableViewController` like this one
https://lists.w3.org/Archives/Public/uri/2004Nov/att-0015/App-Note-UseOfTheFileURLInJDF-031111.doc
and on TableViewCell selection this doc file should open in any default viewer that is not part my app, So I can do achieve this ? Is this possible or any suggestion.
Working with Quick​Look Framework should satisfied your requirement.
As mentioned at "Using the Quick Look Framework":
A Quick Look preview controller can display previews for the following
items:
iWork documents
Microsoft Office documents (Office ‘97 and newer)
Rich Text Format (RTF) documents
PDF files
Images
Text files whose uniform type identifier (UTI) conforms to the public.text type
Comma-separated value (csv) files
You can find many of articles about working with QuickLook Framework; You might want to check the following one:
Using Quick Look Framework for Previewing Documents.
Also, checking this repo (GitHub) might be useful.
Hope this helped.
Use UIWebView class to open the given URL.
Method 1:
let webView = UIWebView(frame: self.view.frame)
webView.scalesPageToFit = true
view.addSubview(webView)
let urlS = "https://lists.w3.org/Archives/Public/uri/2004Nov/att-0015/App-Note-UseOfTheFileURLInJDF-031111.doc"
let url = URL(string: urlS)
let request = URLRequest(url: url!)
webView.loadRequest(request)
Method 2:
With this method, you'll get nice ToolBar items, which you can customize based on your requirement.
Using UIWebView library for Swift, SwiftWebView:
If you're using UINavigationController:
let urlS = "https://lists.w3.org/Archives/Public/uri/2004Nov/att-0015/App-Note-UseOfTheFileURLInJDF-031111.doc"
let webVC = SwiftWebVC(urlString: urlS)
self.navigationController?.pushViewController(webVC, animated: true)
OR
If you want to present Modally:
let urlS = "https://lists.w3.org/Archives/Public/uri/2004Nov/att-0015/App-Note-UseOfTheFileURLInJDF-031111.doc"
let webVC = SwiftModalWebVC(urlString: urlS)
self.present(webVC, animated: true, completion: nil)

Playing Video from Online URL works but from Local Path doesn't

I am having trouble playing a local video on my computer. I used a library called Player which is quite straight forward, and in the example project, it's using a Vine video with supplying a link at the top, and it's working:
let videoUrl = NSURL(string: "https://v.cdn.vine.co/r/videos/AA3C120C521177175800441692160_38f2cbd1ffb.1.5.13763579289575020226.mp4")!
Thus, in ViewDidLoad, self.player.setUrl(videoUrl) works.
I tried to download the Vine video to my local. I saved it as aaa.mp4.
I dragged the mp4 file in xCode project.
I added the file in Copy Bundle Resources
Then I used;
let path = NSBundle.mainBundle().pathForResource("aaa", ofType:"mp4")!
let videoUrl = NSURL(string: path)!
viewDidLoad() {
// print(path)
// print(videoUrl)
self.player.setUrl(videoUrl)
}
The video doesn't get played, but
print(path) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Application/5456FF89-904F-4AE6-A90C-747B6A371745/Player.app/aaa.mp4
and print(videoUrl) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Appl ... /aaa.mp4
Please note that, if I give the path a filename that doesn't exists, it throws an error, thus I know the file is there (and exists) in my case.
What may be the problem? What am I missing? I don't receive any warnings or errors
Use
NSURL(fileURLWithPath: path)!
instead.

What's the path to save a video in iPad?

I'm saving a video on my iPad with this code on swift:
let paths = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as String
var filePath:String? = nil
var fileNamePostfix = 0
do {
filePath =
"\(documentsDirectory)/\(dateTimePrefix)-\(fileNamePostfix++).mp4"
} while (NSFileManager.defaultManager().fileExistsAtPath(filePath))
let fileUrl = NSURL(fileURLWithPath: filePath)
self.fileOutput.startRecordingToOutputFileURL( fileUrl , recordingDelegate: delegate)
But I can't see if my video is saving because I can't open path var/mobile/media...
There are any form to save pictures on photos folder?
Thanks!!
You can't save assets to the Media Library — the stuff that appears in the Music, Videos, and Podcasts apps. The only way to get things into there is by syncing from iTunes on the desktop or downloading from the iTunes Store.
If you want to save a video so that it appears in the Photos app, use the Photos framework in iOS 8:
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let request = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(url)
}, completionHandler: { success, error in
if !success { NSLog("Failed to create video: %#", error) }
})
(In iOS 7 and earlier, use the AssetsLibrary framework instead.)
If you just want to see if the files are getting into the documents folder on your device then you can see the container in the Devices window of Xcode 6.
Just Window > Devices then select your device. Then select your app, click the gear button and tell it if you want to view, download, or replace the container for the app.
Note that you can also save the videos into the Saved Photos album, assuming they are in the right format. Check out this method in the ALAssetsLibrary Class:
writeVideoAtPathToSavedPhotosAlbum:completionBlock:
You can't access the photo system folder directly. However if you just want to see the files you've saved, you don't have to. Just enable document sharing by adding UIFileSharingEnabled to your Info.plist.
When you plug your device in and open iTunes, it will show your documents folder like so:
More info on filesharing here.

Resources