I would like to make Facebook share button on my app that shares the image that is loaded from the UIImageview. I cannot figure out how to do it.. There is option to add the text on Facebook share window, but can't add the image. Please help.
import UIKit
import Social
class ViewController: UIViewController {
#IBOutlet weak var saveButtonVar: UIButton!
#IBOutlet weak var image: UIImageView!
#IBAction func facebookBtn(sender: AnyObject) {
var facebookBtn : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookBtn.setInitialText("I am sharing my motivation with you!")
facebookBtn.addImage(UIImage(named: "motImg")) //????
self.presentViewController(facebookBtn, animated: true, completion: nil)
}
#IBAction func twitterBtn(sender: AnyObject) {
}
#IBAction func saveButton(sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(image.image, nil, nil, nil)
let alertController = UIAlertController(title: "Image Saved!", message:
"", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
#IBAction func button(sender: AnyObject) {
load_image("https://dl.dropboxusercontent.com/u/26347781/Images/Image\(arc4random_uniform(17) + 1).jpg")
saveButtonVar.hidden = false
}
func load_image(urlString:String)
{
var imgURL: NSURL = NSURL(string: urlString)!
let request: NSURLRequest = NSURLRequest(URL: imgURL)
NSURLConnection.sendAsynchronousRequest(
request, queue: NSOperationQueue.mainQueue(),
completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
if error == nil {
self.image.image = UIImage(data: data)
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
saveButtonVar.hidden = true
image.image = UIImage(named: "motImg")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift 4
Integrate FacebookShare sdk.
People can share photos from your app to Facebook with the Share Dialog or with a custom interface:
-Photos must be less than 12MB in size.
-People need the native Facebook for iOS app installed, version 7.0 or higher
#IBAction func facebookBtn(sender: AnyObject) {
let shareImage = SharePhoto()
shareImage.image = imageView.image //Image from your imageview
shareImage.isUserGenerated = true
let content = SharePhotoContent()
content.photos = [shareImage]
let sharedDialoge = ShareDialog()
sharedDialoge.shareContent = content
sharedDialoge.fromViewController = self
sharedDialoge.mode = .automatic
if(sharedDialoge.canShow)
{
sharedDialoge.show()
}
else
{
print("Install Facebook client app to share image")
}
}
Hei there!
Check out this guy's answer:
Swift UIActivityViewController Image&Url Share not working with FB
For many people it seemed to work the example in the link, so maybe it does for you, too.
However, so far it did not work for me and I came across your question while still investigating my problem.
As far as adding text, I know this functionality is off for some time, but you should be able to add an url link.
Let me know how this works for you.
Related
I am creating an iOS app that has a custom UIMenuItem. This new custom UIMenuItem shows up when text is selected in WKWebView. How do I get the selected text.
I followed instructions in https://stackoverflow.com/a/49761522/6828076 to create a custom UIMenuItem. It works fine, but I need the selected text that was used when the custom UIMenuItem was tapped. There are many posts about using UIPasteboard but the custom item does not copy the selected text into the UIPasteboard, so I am unable to retrieve it.
func setupCustomMenu() {
let customMenuItem = UIMenuItem(title: "Foo", action:
#selector(ViewController.transelateMenuTapped))
UIMenuController.shared.menuItems = [customMenuItem]
UIMenuController.shared.update()
}
#objc func transelateMenuTapped() {
let yay = //Need to retrieve the selected text here
let alertView = UIAlertController(title: "Yay!!", message: yay, preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "cool", style: .default, handler: nil))
present(alertView, animated: true, completion: nil)
}
You can use Javascript for that.
Here's the code from the the answer you used, slightly altered to get the selected text by evaluating Javascript on the WKWebView:
import UIKit
import WebKit
class ViewController: UIViewController {
weak var webView: CustomMenuWebView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
prepareWebView()
}
#objc func translateMenuTapped(_ test: Any) {
webView.evaluateJavaScript("window.getSelection().toString()") { (test, error) in
guard let test = test as? String, error == nil else { return }
// ***** Here's the user's selected text *****
print(test)
}
}
}
private extension ViewController {
func prepareWebView() {
addWebViewToView()
loadWebViewContent()
setupCustomMenu()
}
func addWebViewToView() {
let webView = CustomMenuWebView(
frame: view.bounds, configuration: WKWebViewConfiguration())
view.addSubview(webView)
self.webView = webView
}
func loadWebViewContent() {
let url = URL(string: "https://www.google.com")
let request = URLRequest(url: url!)
webView.load(request)
}
func setupCustomMenu() {
let customMenuItem = UIMenuItem(
title: "Translate", action: #selector(ViewController.translateMenuTapped))
UIMenuController.shared.menuItems = [ customMenuItem ]
UIMenuController.shared.update()
}
}
class CustomMenuWebView: WKWebView {
// Turn off all other menu items
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
So here's my problem. I am trying to create a screen in which there is a UIImageView and a UIButton. When the user presses the button, the camera app opens, you take a photo and if you press "Use Photo" in the Camera app, you are returned to my app's screen and the photo is placed in the UIImageView I mentioned previously.
What happens so far is that when I press the "Use Photo" button, the image is correctly placed in my UIImageView but then the app crashes with the following error:
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.
What I've done so far is:
Placed the key "Privacy - Photo Library Usage Description" with the value "$(PRODUCT_NAME) uses Library in order to process the photos you captured." in the Info.plist file (also checked how it is written in Source form and it's correct according to the Apple Developer Documentation).
Also placed the key "Privacy - Camera Usage Description" with the value "$(PRODUCT_NAME) uses Cameras" in the Info.plist file.
Checked under "TARGETS->->Info->Custom iOS Target Properties" and the 2 key/value pairs that I mentioned in steps 1 and 2, exist.
I will provide you with my code so far:
import UIKit
import Vision
import MobileCoreServices
import AVFoundation
import Photos
class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
var newMedia: Bool?
#IBAction func captureImageButtonPressed(_ sender: Any) {
//let imageName : String = "dolphin"
//randomImageView.image = UIImage.init(named:imageName)
if UIImagePickerController.isSourceTypeAvailable(
UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType =
UIImagePickerControllerSourceType.camera
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true,
completion: nil)
newMedia = true
}
}
#IBAction func classifyButtonPressed(_ sender: UIButton) {
performVisionRequest()
}
#IBOutlet weak var randomImageView: UIImageView!
#IBOutlet weak var classificationLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
func performVisionRequest() {
let start = DispatchTime.now()
let model = Resnet50()
let request = VNImageRequestHandler(cgImage: randomImageView.image!.cgImage!, options: [:])
do {
let m = try VNCoreMLModel(for: model.model)
let coreMLRequest = VNCoreMLRequest(model: m) { (request, error) in
guard let observation = request.results?.first as? VNClassificationObservation else { return }
let stop = DispatchTime.now()
let nanoTime = stop.uptimeNanoseconds - start.uptimeNanoseconds
let timeInterval = Double(nanoTime)
self.classificationLabel.text = "\(observation.identifier) (\(observation.confidence * 100)%) in \(timeInterval) seconds."
}
try request.perform([coreMLRequest])
} catch {
print(error)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
self.dismiss(animated: true, completion: nil)
if mediaType.isEqual(to: kUTTypeImage as String) {
let image = info[UIImagePickerControllerOriginalImage]
as! UIImage
randomImageView.image = image
if (newMedia == true) {
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(ViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)
} else if mediaType.isEqual(to: kUTTypeMovie as String) {
// Code to support video here
}
}
}
#objc func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer) {
if error != nil {
let alert = UIAlertController(title: "Save Failed",
message: "Failed to save image",
preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK",
style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true,
completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
Any idea why I get the above error in bold? Thank you very much in advance for your time.
NSPhotoLibraryAddUsageDescription was added in iOS 11.
Please add "Privacy - Photo Library Additions Usage Description" in info.plist with a usage description (string), like you did for the other privacy permissions.
Ref: https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
Trying to follow the FoodTracker Apple tutorial. xCode 8.1. I get this crashlog:
2016-11-06 16:49:17.922832 FoodTracker[2307:660318] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.
(lldb)
I have read several posts on missing privacy keys in iOS 10, and so I added the required key...
Privacy - Photo Library Usage Description
...and a custom message under the value column. Yet I still get the same crash when I tap the image. Any help would be great. I would love to finish this tutorial.
Thanks!
INFO:
I cannot include a screenshot of my info.plist yet, so here is the link to a screenshot:
screenshot of info.plist
Here is my ViewController.swift just in case:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.delegate = self
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Hide the keyboard
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
mealNameLabel.text = textField.text
}
// MARK: UIImageControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: (nil))
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
photoImageView.image = selectedImage
dismiss(animated: true, completion: (nil))
}
// MARK: Actions
#IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
nameTextField.resignFirstResponder()
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
#IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
There are a total of 3 Info.plist files under the FoodTracker project. Make sure you're editing the one under the 'FoodTracker' folder. Also, make sure that the property is not cascaded under any existing array property.
You may need to make sure that your app has been granted permission. Though you may have added the info.plist string now, since you have run the app on your device already it may be holding on to the error. I would suggest doing a clean. Delete your derived data "Shortcut" Command + Option + Sift + k and delete the app form the device and build/run again. If that does not solve the problem you should look into if you actually have the ability to use that source.
func pickPhoto() {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
showPhotoMenu()
} else {
choosePhotoFromLibrary()
}
}
Because if you are on a simulator you don't actually have a camera like you do on a real device. This would cause a crash. If that does not solve it you can just check the permissions for that media type.
let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
if status == AVAuthorizationStatus.denied {
let changeYourSettingsAlert = UIAlertController(title: "You do not have permissions enabled for this.", message: "Would you like to change them in settings?", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: { (UIAlertAction) -> Void in
guard let url = URL(string: UIApplicationOpenSettingsURLString) else {return}
UIApplication.shared.openURL(url)
})
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
changeYourSettingsAlert.addAction(okAction)
changeYourSettingsAlert.addAction(cancelAction)
presentAlert(sender: changeYourSettingsAlert)
}
The other half is how to give them an option to open Settings for the user.
I hope that helps good luck🖖🏼
This question already has answers here:
Swift - Download a video from distant URL and save it in an photo album
(4 answers)
Swift - Downloading video with downloadTaskWithURL
(2 answers)
Closed 6 years ago.
I would like to save video from url to directory and show in gallery in application.
Below is my code:
import UIKit
import AVFoundation
import AVKit
class SettingsViewController: UIViewController {
#IBOutlet var urlLabel: UITextField!
let PlayerController = AVPlayerViewController()
var Player:AVPlayer?
//
// override func viewDidLoad() {
// super.viewDidLoad()
//
// }
// override func didReceiveMemoryWarning() {
// super.didReceiveMemoryWarning()
// // Dispose of any resources that can be recreated.
// }
//
#IBAction func downloadButton(sender: AnyObject) {
let urlLabel1 = urlLabel.text
let videoURL:NSURL? = NSURL(string: urlLabel1!)
if let url = videoURL {
self.Player = AVPlayer(URL: url)
self.PlayerController.player = self.Player
self.presentViewController(self.PlayerController, animated: true) { self.PlayerController.player?.play()}
print("work")
}
else {
var refreshAlert = UIAlertController(title: "Error", message: "Please correct your URL", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
print("Handle Ok logic here")
}))
presentViewController(refreshAlert, animated: true, completion: nil)
print("not work")
}
}
}
Any ideas how can I make it?
//Save Video to Home Directory
func saveVideoFromURL(videoURL: NSURL, name: String) {
let homeDirectory = NSURL.fileURLWithPath(NSHomeDirectory(), isDirectory: true)
let fileURL = homeDirectory.URLByAppendingPathComponent(name).URLByAppendingPathComponent("mov")
let urlData = NSData(contentsOfURL: videoURL)
urlData?.writeToURL(fileURL, atomically: true)
}
//Extract video from home directory
func getVideoFromDirectoryWithName(name: String) -> NSData {
let homeDirectory = NSURL.fileURLWithPath(NSHomeDirectory(), isDirectory: true)
let fileURL = homeDirectory.URLByAppendingPathComponent(name).URLByAppendingPathComponent("mov")
let fileData = NSData(contentsOfURL: fileURL)!
return fileData
}
Assuming the URL points directly to the video, you could simply create a NSURLSessionDownloadTask to download the video data onto the filesystem, and upon completing the download, open the video by creating an AVPlayerItem for the local video.
I have a UITabView in UIViewController, all tab items are linked to other UIViewControllers. I have written a swift code of downloading a file through internet. when I select second tabItem, this code runs well, it downloads and previews the downloaded file, Then when I click on first tabItem and then again click on second tabItem; file downloads well but it doesn't show any preview instead xCode gives me a warning message:
What I want is download file and preview file both should work when I again click on the second tabItem. whatever the code is.
warning: attempt to present QLPreviewController on KPIViewController whose view is not in the window hierarchy
I have found many solutions on the internet but it didn't work
first solution says to use
let viewer = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: path))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(viewer, animated: true, completion: nil)
but this function
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(viewer, animated: true, completion: nil)
do not accept
UIDocumentInteractionController
second solution says to override the existing presentViewController function to
override func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?) {
let APP_DELEGATE = UIApplication.sharedApplication().delegate
var presentedModalVC:UIViewController = (APP_DELEGATE!.window?!.rootViewController?.presentedViewController)!
if presentedModalVC == true {
while((presentedModalVC.presentedViewController) != nil){
presentedModalVC = presentedModalVC.presentedViewController!
}
presentedModalVC.presentViewController(viewControllerToPresent, animated: flag, completion: nil)
}
else{
APP_DELEGATE?.window!!.rootViewController?.presentViewController(viewControllerToPresent, animated: flag, completion: nil)
}
}
I tried this but it also needs a UIViewController in its parameters where I have UIDocumentInteractionController
I know these function cannot accept UIDocumentInteractionController type viewController.
here is my whole swift code:
// KPIViewController.swift
// download
//
// Created by me on 15/03/2016.
// Copyright © 2016 me. All rights reserved.
//
import UIKit
class KPIViewController: UIViewController,UITabBarDelegate, NSURLSessionDownloadDelegate, UIDocumentInteractionControllerDelegate{
#IBOutlet weak var tabBar1: UITabBar!
#IBOutlet weak var login_Item: UITabBarItem!
#IBOutlet weak var QAreport_Item: UITabBarItem!
#IBOutlet weak var KpiWebView: UIWebView!
#IBOutlet weak var progressView: UIProgressView!
var downloadTask: NSURLSessionDownloadTask!
var backgroundSession: NSURLSession!
var downloadReport:Bool!
var AuditCodeOfDashboardCell:String?
var AuditCodeForPDF:String?
let isDirectory: ObjCBool = false
override func viewDidLoad() {
super.viewDidLoad()
self.progressView.hidden = true
downloadReport = false
// Do any additional setup after loading the view.
self.tabBar1.delegate = self
}
override func viewDidAppear(animated: Bool) {
self.progressView.hidden = true
downloadReport = false
let backgroundSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("backgroundSession")
backgroundSession = NSURLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
progressView.setProgress(0.0, animated: false)
var requestURL = NSURL!()
var request = NSURLRequest!()
// loading data from web
if AuditCodeOfDashboardCell != nil{
print(self.AuditCodeOfDashboardCell)
requestURL = NSURL(string:“my URL string&\(AuditCodeOfDashboardCell)”)
request = NSURLRequest(URL: requestURL!)
AuditCodeForPDF = AuditCodeOfDashboardCell
AuditCodeOfDashboardCell = nil
}else{
requestURL = NSURL(string:“my URL string”)
request = NSURLRequest(URL: requestURL!)
}
KpiWebView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
print("selected tabItem: \(item.tag)")
switch (item.tag) {
case 1:
let loginVC = self.storyboard!.instantiateViewControllerWithIdentifier("loginViewController") as! LoginView
presentViewController(loginVC, animated: true, completion: nil)
break
case 2:
if AuditCodeForPDF != nil{
downloadReport = true
let url = NSURL(string: “my URL string&\(AuditCodeForPDF)”)!
urlToDownload = url
}
// if let resultController = storyboard!.instantiateViewControllerWithIdentifier(“2”) as? QAReportViewController {
// presentViewController(resultController, animated: true, completion: nil)
// }
break
default:
break
}
if downloadReport == true{
let url = NSURL(string: “my URL string&\(AuditCodeForPDF)”)!
downloadTask = backgroundSession.downloadTaskWithURL(url)
self.progressView.hidden = false
downloadTask.resume()
downloadReport = false
}
}
// - - Handling download file- - - - - - - - -
func URLSession(session: NSURLSession,
downloadTask: NSURLSessionDownloadTask,
didFinishDownloadingToURL location: NSURL){
let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentDirectoryPath:String = path.first!
let fileManager = NSFileManager()
var destinationURLForFile = NSURL(fileURLWithPath: documentDirectoryPath.stringByAppendingString("/Report.pdf"))
if fileManager.fileExistsAtPath(destinationURLForFile.path!){
// showFileWithPath(destinationURLForFile.path!)
do{
try fileManager.removeItemAtPath(destinationURLForFile.path!)
destinationURLForFile = NSURL(fileURLWithPath: documentDirectoryPath.stringByAppendingString("/Report.pdf"))
}catch{
print(error)
}
}
do {
try fileManager.moveItemAtURL(location, toURL: destinationURLForFile)
// show file
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.showFileWithPath(destinationURLForFile.path!)
})
}catch{
print("An error occurred while moving file to destination url")
}
}
func showFileWithPath(path: String){
let isFileFound:Bool? = NSFileManager.defaultManager().fileExistsAtPath(path)
if isFileFound == true{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewer = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: path))
viewer.delegate = self
viewer.presentPreviewAnimated(true)
})
}
}
func URLSession(session: NSURLSession,
downloadTask: NSURLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64){
progressView.setProgress(Float(totalBytesWritten)/Float(totalBytesExpectedToWrite), animated: true)
}
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController{
return self
}
func documentInteractionControllerDidEndPreview(controller: UIDocumentInteractionController) {
print("document preview ends")
}
}
I cannot find any proper solution that solve my problem. I am new with swift
please anyone on help me. Thanks in advance
UIDocumentInteractionController is not kind of UIViewController. So you cannot present an UIDocumentInteractionController with presentViewController: method.
Checkout https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/
You can presenting a document preview or options menus with UIDocumentInteractionController.