Sharing to Instagram using UIActivityView in SwiftUI - swift

Im trying to share a Instagram post from an app that I made in swiftUI using UIActivityView, but whenever I try to share it only the picture shows up and the caption doesn't. The same thing happens with Facebook.
func sharePost(){
isSheetShowing.toggle()
let caption = "Hello, world!"
if let image = UIImage(named: "Image") {
let vc = UIActivityViewController(activityItems: [image,caption], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(vc,animated : true)
}
}

UIActivityController needs to correctly recognize what exactly you are trying to share in order to display it corrrectly. This solution seems to answer your question directly.
Also, this is kind of an anti-pattern in SwiftUI (presenting it directly on the window). You should check this question out.

Related

Black Screen while sharing on Facebook with UIActivityViewController?

Hi I am using UIActivityViewController to share content on social networks but facing one issue. The issue is when i select the share button the UIActivityViewController gets appear and I select the Facebook option.
I select the Facebook option in the sharing sheet -> Facebook sharing view appears with two options (Cancel and Next). When I select the Cancel button giving me another action sheet to discard or keep the post. When i select to discard the Action Sheet get disappear but the Facebook sharing view still keep showing and there is no way to go back.
Second when I press the next button in the Facebook sharing view -> another Facebook view gets appear with two buttons (Cancel and Share).... When I click the share button content get share on Facebook and black screen appears with a check in circle and text "Shared to Facebook!" .. And the application get stuck on this Screen.
Below is my code, Any help would be appreciated, I am also using the activityViewController!.completionWithItemsHandler but this closure not getting called in case of Facebook.
let url = "\(UserDefaults.baseURL)/pl/\(resource)/\(sharingId)"
// set up activity view controller
let imageToShare = [ url ]
activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController!.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// exclude some activity types from the list (optional)
activityViewController!.excludedActivityTypes = [UIActivity.ActivityType.airDrop]
activityViewController!.completionWithItemsHandler = { (activityType: UIActivity.ActivityType?, completed:
Bool, arrayReturnedItems: [Any]?, error: Error?) in
if completed {
print("share completed")
self.dismiss(animated: true)
return
} else {
print("cancel")
self.dismiss(animated: true)
}
if let shareError = error {
print("error while sharing: \(shareError.localizedDescription)")
}
}
// present the view controller
self.present(activityViewController!, animated: true, completion: nil)

Localization not working in presenting UIActivityViewController in swift 5

My app supports both LTR and RTL. It works fine for all cases. If i change app language from app settings then in every screen language is updated and works fine.
When i press share button in app. Below code is executed.Here i want to show share picker in my app's selected language. But it doesn't show it in my app's language.
let text = NSLocalizedString("I liked this view in app", comment: "")
// set up activity view controller
let activityViewController = UIActivityViewController(activityItems: [text, urls], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// exclude some activity types from the list (optional)
// activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]
// present the view controller
self.present(activityViewController, animated: true, completion: nil)`
I have tried below code to update share view language change but it didn't work.
if ( Language.currentLanguage == .arabic) {
activityViewController.accessibilityLanguage = "ar-SA"
}
else{
activityViewController.accessibilityLanguage = "en-US"
}
I have also tried below code but it is also not working.
if ( Language.currentLanguage == .arabic) {
activityViewController.view.semanticContentAttribute = .forceRightToLeft
}
else{
activityViewController.view.semanticContentAttribute = .forceLeftToRight
}
Anyone could help me with this issue ?
PS
For share picker you can refer below screenshot :
A solution which worked for me. Go to your Info.plist and add Localized resources can be mixed with Boolean YES or NO to it, and check the result.
As mentioned in Core Foundation Keys:
CFBundleAllowMixedLocalizations
CFBundleAllowMixedLocalizations (Boolean - iOS, macOS) specifies
whether the bundle supports the retrieval of localized strings from
frameworks. This key is used primarily by Foundation tools that link
to other system frameworks and want to retrieve localized resources
from those frameworks.

PHPickerViewController hide Search Bar and Navigation Bar

I've been trying to implement a photo selection feature in a new app. My current approach is to use a PHPickerViewController embedded in a UIViewControllerRepresentable to display in a swiftUI view.
This is my makeUIViewController function.
func makeUIViewController(context: Context) -> PHPickerViewController {
var configuration = PHPickerConfiguration()
configuration.filter = filter
configuration.selectionLimit = limit
let controller = PHPickerViewController(configuration: configuration)
controller.delegate = context.coordinator
return controller
}
It is inside a struct named PhotoPicker :
struct PhotoPicker: UIViewControllerRepresentable {
What I want to hide is this part :
Yes, all of that.
Let me explain myself, the PickerView is always presented, it is not a pop-up, so there is no need for a cancel button. As you can see there is no done button either. That's because only one image needs to be selected so what happens is when the user taps on an image, the event that a new image was selected is called immediately. Removing the need for user confirmation. Then concerning the search bar, I don't really want it, I just want the user to select a photo and finally the little switch between photos and albums isn't really necessary in my case either.
I've tried a lot of different ways, including trying to set options for the controller when it is created in makeUIViewController. These options were for example :
controller.navigationController?.setToolbarHidden(true, animated: false)
controller.navigationController?.setNavigationBarHidden(true, animated: false)
And I also tried invoking view modifier in my SwiftUI body :
PhotoPicker(filter: .images, limit: 1) { (results) in
}
.navigationBarTitle("")
.navigationBarHidden(true)
.statusBar(hidden: true)
.navigationBarBackButtonHidden(true)
But again, none of them seems to work. So that's why I'm asking here, because it seems I tried everything and nothing is working...

(UIApplication.shared.keyWindow?.rootViewController as? BaseSlidingController)?.openMenu() return nil Swift 4.2

I have a problem with the above stated. I can not find the exact information on the forums. Most of them are outdated and I have written the code programmatically. I have a controller that contains a view to edit the profile. I can not access that after changing the function listed below. I have a rootviewcontroller set to something else, but I tried the UiApplication calls anyway and it return nil and I can not open the profile controller. This is the function listed below.
#objc func handleOpen2() {
(UIApplication.shared.keyWindow?.rootViewController as? BaseSlidingController)?.openMenu()
}
Xcode does not give me an error but I can not get my menu to open. My rootviewcontroller is set to something else in app delegate. I have a controller that is used to control the sliding menu when I press the edit profile button.
func openMenu() {
isMenuOpened = true
redViewLeadingConstraint.constant = menuWidth
redViewTrailingConstraint.constant = menuWidth
performAnimations()
setNeedsStatusBarAppearanceUpdate()
}
This code is used to open my side bar menu with the information I need and also to perform animations as well. I was wondering if someone had any idea what I can do different instead in my handleOpen2 function. If you need more code, please let me know. Thanks
On swift 5 version:
(UIApplication.shared.windows.first?.rootViewController as? BaseSlidingController)?.openMenu()

google placepicker for ios and BackgroundMapViewController.swift

I'm trying to implement google's placepicker for ios.
There is some demo code at:
https://github.com/googlemaps/maps-sdk-for-ios-samples/tree/master/GooglePlacePicker/GooglePlacePickerDemos
And i get most of it except the line in the file PickAPlaceViewController.swift
var mapViewController: BackgroundMapViewController?
In the github repo Google have provided their own class BackgroundMapViewController.
Do I need to create an instance of this class to use the place picker?
Or can I just use a GMSMapView like I ordinarily would with google maps.
Thanks.
For Google Places Controller you just need below code to show or present Places Controller in your App.
Put this in ur Button Action method :
// Create a place picker. Attempt to display it as a popover if we are on a device which
// supports popovers.
let config = GMSPlacePickerConfig(viewport: nil)
let placePicker = GMSPlacePickerViewController(config: config)
placePicker.delegate = self
placePicker.modalPresentationStyle = .popover
placePicker.popoverPresentationController?.sourceView = pickAPlaceButton
placePicker.popoverPresentationController?.sourceRect = pickAPlaceButton.bounds
// Display the place picker. This will call the delegate methods defined below when the user
// has made a selection.
self.present(placePicker, animated: true, completion: nil)
Hope This will helps to show Place Picker in your app.