Swift posting to Facebook not working? - ios

I'm trying to post a photo to Facebook, and it works fine using the following code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
self.selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
var photo = FBSDKSharePhoto()
photo.image = self.selectedImage
photo.userGenerated = true
var content = FBSDKSharePhotoContent()
content.photos = [photo]
self.dismissViewControllerAnimated(true, completion: nil)
shareDialog.fromViewController = self
shareDialog.shareContent = content
shareDialog.show()
}
However, this only works when the user has the facebook app downloaded. otherwise it does NOT open the web browser to share the photo, even though facebook state:
The Share Dialog switches to the native Facebook for iOS app, then
returns control to your app after a post is published. If someone
doesn't have Facebook app installed it will automatically falls back
to a web-based dialog.
Any suggestions please ? answers appreciated, thank you.

You can try this:
func postToFacebook(image: UIImage!) {
var SocialMedia :SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
SocialMedia.completionHandler = {
result -> Void in
var getResult = result as SLComposeViewControllerResult;
switch(getResult.rawValue) {
case SLComposeViewControllerResult.Cancelled.rawValue: println("Cancelled")
case SLComposeViewControllerResult.Done.rawValue: println("It's Work!")
default: println("Error!")
}
self.dismissViewControllerAnimated(true, completion: nil)
}
self.presentViewController(SocialMedia, animated: true, completion: nil)
SocialMedia.setInitialText("...")
SocialMedia.addImage(image)
}

Related

How can [UIImagePickerController.InfoKey.phAsset] as? PHAsset be nil

I sent a user a TestFlight version of my app. She has a video recorded that she made using her iPhone. When the imagePicker was presented and she selected a video it came back as nil. I have an error message with a number 120 that appears in an alert that tells me where the error occurred.
It can only be the asset [UIImagePickerController.InfoKey.phAsset] as? PHAsset is nil. I don't see how that's possible because it's a video that she made using her phone. The odd thing is when she selects a photo everything works fine and when I select videos using iOS 14 and iOS 13 everything works fine.
She's on iOS 15.1, and I'm wondering is that the issue? She's a fellow iOS dev and she said iOS 15 has been causing issues.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset
// ...
if let style = asset?.playbackStyle {
// ...
// if it entered here then the below alert would have never appeared
} else {
let errorMessage = "Error: 120"
let alert = UIAlertController(title: "Unknown Error", message: errorMessage, preferredStyle: .alert)
// ...
}
imagePicker?.dismiss(animated: true, completion: nil)
}
FYI afterwards I just tested on iOS 15.1 with some videos and it worked fine.
I tried the PHPickerController but it's very buggy so I'd rather stick with the ImagePicker for now.
I'm not exactly sure how the asset can be nil but according to this answer by #Sh_Kahn I need to handle all possibilities:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
let style = asset.playbackStyle
// ...
} else if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
// ...
} else if let imageUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL {
// ...
} else {
let errorMessage = "Error: 120"
let alert = UIAlertController(title: "Unknown Error", message: errorMessage, preferredStyle: .alert)
// ...
}
imagePicker?.dismiss(animated: true, completion: nil)
}

Issue setting default profile pic in Swift (iOS)

I am new to Swift/iOS development, so this might be a stupid question, but I can't seem to find how to do this correctly.
I am following a youtube guide on how to programmatically code (no storyboard) a login screen in Swift and register a user into a Firebase database. The basic outline of the design are as follows
Once the user clicks the icon of the cat with the crown, the image picker comes up and they can select a profile picture and then register like normal. If the user does not select an image from the picker, the cat with the crown icon gets loaded as the profile picture by default.
What I have been trying to do is make it so a different picture (not the cat, and not shown to the user on the login page) named "nedstark" is set as the default profile picture that is stored in the database.
The program uses LoginController.swift and LoginController + handlers.swift files to make this happen. I tried to add a conditional to the picker where if nothing is selected, the default profile pic is set to a specific default user pic (different than the cat), but it doesn't work.
This is the code that stores the photo and other User info into the Database
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).jpg")
if let profileImage = self.profileImageView.image, let uploadData = UIImageJPEGRepresentation(profileImage, 0.1) {
storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if let error = error {
print(error)
return
}
if let profileImageUrl = metadata?.downloadURL()?.absoluteString {
let values = ["name": name, "email": email, "profileImageUrl": profileImageUrl]
self.registerUserIntoDatabaseWithUID(uid, values: values as [String : AnyObject])
}
})
}
This is the code where I am trying to set the default profile pic
func handleSelectProfileImageView() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicker = originalImage
}
else { //Testing if default profile pic set
selectedImageFromPicker = UIImage(named: "nedstark")
}
if let selectedImage = selectedImageFromPicker {
profileImageView.image = selectedImage
}
else { //Testing if default profile pic set
profileImageView.image = UIImage(named: "nedstark")
}
dismiss(animated: true, completion: nil)
}
I
I know that this is a total noob question, but any help, tips, or pointers is greatly appreciated. Thanks!
You need to handle delegate method of cancel event. UIImagepickercontrolledelegate has another method
imagePickerControllerDidCancel()
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
imageView.image = UIImage(named: "nedstark")
dismiss(animated: true, completion: nil)
}
When you click on cancel button in pickerviewcontroller then didfinishPickingMedia function won't call. That's way your default image is not set when you tap on cancel button.

Selecting image from library doesn't call "didFinishPickingImage" function - Swift

I have a UIImagePickerController with the option to select from library.
When the accessLibrary button is called, it prompts another UIImagePickerController that has source type .PhotoLibrary.
func accessLibrary(sender: UIButton!) {
NSLog("access library")
choseFromLibrary = true
self.libraryPicker = UIImagePickerController()
self.libraryPicker!.allowsEditing = false
self.libraryPicker!.sourceType = .PhotoLibrary
self.presentViewController(self.libraryPicker!, animated: true, completion: nil)
}
This all works well, but when the user does select a photo, I want to pass that image to a view and then show that view on the screen.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary
var image = info[UIImagePickerControllerOriginalImage] as? UIImage
if (image != nil) {
if (choseFromLibrary) {
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
else {
if (frontFacing) {
image! = UIImage(CGImage: image!.CGImage!, scale: 1.0, orientation: .LeftMirrored)
}
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
}
}
The above function works if the image is taken using the camera, but not using the library.
When a user picks a photo from the library, the library picker controller just dismisses itself and the view is not display.
I ran an NSLog("here") to check if the didFinishPickingImageWithInfo function is called when choosing from the library and in fact it is not.
didFinishPickingImageWithInfo is a delegate message (UIImagePickerControllerDelegate). The problem, however, is that you have not given your image picker any delegate! Thus, it has no one to send delegate messages to.
if you want to implement a Instagram like image picker than try this
Image Picker like Instagram
The delegate function is updated in new swift versions from 4 to upwards.
Use this function instead
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { }

Where are the photos taken with my app stored?

Besides browsing the Photolibrary I've enabled the Camera to add a photo to my
#IBAction func startCameraButtonTapped(sender: AnyObject) {
let startCameraController = UIImagePickerController()
startCameraController.delegate = self
startCameraController.sourceType = UIImagePickerControllerSourceType.Camera
startCameraController.allowsEditing = true
self.presentViewController(startCameraController, animated: true, completion: nil)
//invoiceImageHolder.image!.scaleAndRotateImage2(280)
//self.invoiceImageHolder.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
}
Every user input can be deleted, but now I'd like to know where the corresponding photo is. When I browse the photo library I can't see the photos taken with my app? Where are those photos stored?
[edit]
This is my saveImage process
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
saveImage(selectedImage, path: fileInDocumentsDirectory("\(uniqueImageName)"))
// set the unique image reference in textfield
// Set photoImageView to display the selected image
self.invoiceImageHolder.image = selectedImage
// Dismiss the picker
dismissViewControllerAnimated(true, completion: nil)
}
func saveImage(image: UIImage, path: String) -> Bool{
let pngImageData = UIImagePNGRepresentation(image)
// let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
let result = pngImageData?.writeToFile(path, atomically: true)
//print("Save result \(result)")
return result!
}
EDIT hmm I think I see where the photos are stored, but now how do I browse to them for deletions?
I think it's here:
/var/mobile/Containers/Data/Application/CBFE9E9D-A657-4011-8B9F-EF0C9BB2C603/Documents/
BUT everytime i restart the app the part between Application and /Documents is different??
Still not a working solution, but for those newbies as me this is a valuable readup: http://www.techotopia.com/index.php/Working_with_Directories_in_Swift_on_iOS_8
[EDIT 2]
with this code i am able to see all whiles stored with my app. I was a bit surprise to see all the files
// Files stored in my app filemanager
let filemgr = NSFileManager.defaultManager()
do {
print("Start filemanager")
let filelist = try filemgr.contentsOfDirectoryAtPath(documentsDirectory())
for filename in filelist {
print(filename)
}
} catch {
print("No directory path \(error)")
}
// end filemanager
UIImagePickerController() doesn't automatically save your photos into the library. See its UIImagePickerControllerDelegate protocol and you'll see that it has a method returning your UIImage after making a photo. Then you can save it:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
you save them - not in the photo library - but as 'simply' files inside your app's documents folder. There is no standard UI for deletion then.
You need to write your own ViewController to show & handle deletions then.

Swift - Publish directly on facebook

I created a code swift, that use facebook SDK 4.x. The code use FBSDKShareButton and worked very well.
This is the code:
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "http://alexandreccarmo.com")
content.contentTitle = "Teste"
content.contentDescription = "teste teste"
content.imageURL = NSURL(string: "xxxx/2.jpg")
let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25)
self.view.addSubview(button)
But I need to share without use facebook dialog. When the user click on button of my app I need share directly on their facebook.
I tried this:
let contentx : FBSDKShareLinkContent = FBSDKShareLinkContent()
contentx.contentURL = NSURL(string: "http://alexandreccarmo.com")
contentx.contentTitle = "Testexxxxx"
contentx.contentDescription = "teste testexxx"
contentx.imageURL = NSURL(string: "xxxx/2.jpg")
FBSDKShareAPI.shareWithContent(contentx, delegate: nil)
But I receive this error:
FacebookLogin[6721:3464359] FBSDKLog: Warning: [FBSDKAccessToken currentAccessToken] is missing publish_actions permissions
I read on facebook developers that I need to request "publish_actions" when I need to send share. But I don't understand how can i do this. Somebody knows?
So here is some code that I used to publish an image on Facebook without the Facebook SDK:
func actionTapped(sender: AnyObject) {
let objectsToShare = //reference to image
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.completionWithItemsHandler = actionCompleted
presentViewController(activityVC, animated: true, completion: nil)
}
func actionCompleted(activityType: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) {
// Return if cancelled
if (!completed) {
return
}
close()
}
You can check out these links as well:
Can I use UIActivityViewController to share on Facebook ?
http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/
UIActivityViewController share only text
I hope this helps.

Resources