How to detect user successfully shared to Facebook with SLComposeViewController - ios

I created a button in the app to share a status on Facebook with SLComposeViewController. How do I check whether the user really do click to share the status?
My code as below:
#IBAction func fbBtn(_ sender: Any) {
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {
let fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
self.present(fbShare, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}

problem solved TQ
fbShare.completionHandler = { (result:SLComposeViewControllerResult) -> Void in
switch result {
case SLComposeViewControllerResult.cancelled:
print("Cancelled")
break
case SLComposeViewControllerResult.done:
print("Done")
break
}
}

Related

Check of Location permissions set

My iOS app has been rejected by Apple as the app crashes if a user selected "Dont allow" location access. And the proceeds to tap on my Map button.
How can I wrap this button in check to see if the user has given permission, And if not how can I ask for permission again?
//Map Button Action - Opens Maps - Gives choice of Google or Apple maps
#IBAction func googleMapBtn(_ sender: UIButton) {
UIDevice.current.isBatteryMonitoringEnabled = true
let state = UIDevice.current.batteryState
//If user is in Loop - Cant open maps
if state == .charging {
print("In Loop - Cant open maps")
}
//Present Map Options
else {
let alertController = UIAlertController.init(title: "Open Map", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Google Maps", style: .default, handler: { (action) in
self.googleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Apple Maps", style: .default, handler: { (action) in
self.appleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Back", style: .default, handler: { (action) in
self.dismiss(animated: true, completion: nil)
}))
self.present(alertController, animated: true) {
}
}
}
The code crashes whenever a user selects a map type Google/Apple and the self.googleMapsOpen() or self.appleMapsOpen() are executed. Specifically is crashed on the let scheme=
func googleMapsOpen(){
print("Google Maps Pressed")
let scheme = "comgooglemaps://?center=\(LocationManager.sharedInstance.location.coordinate.latitude),\(LocationManager.sharedInstance.location.coordinate.longitude)&zoom=15"
self.open(scheme: scheme)
}
func appleMapsOpen(){
print("Apple Maps Pressed")
let scheme = "http://maps.apple.com/?ll=\(LocationManager.sharedInstance.location.coordinate.latitude),\(LocationManager.sharedInstance.location.coordinate.longitude)&zoom=15"
self.open(scheme: scheme)
}
You can do something like this :
func checkLocationPermissionEnabled()
{
if CLLocationManager.locationServicesEnabled()
{
switch(CLLocationManager.authorizationStatus())
{
case .notDetermined, .restricted, .denied:
self.openDeviceLocationSetting()
return
case .authorizedAlways, .authorizedWhenInUse:
//do whatever you want to do with location
return
}
}
else
{
self.openDeviceLocationSetting()
return
}
}
func openDeviceLocationSetting()
{
let alertController = UIAlertController(title: "", message:"For best results, let your device turn on location using Google's location service.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
self.isAlertShowing = false
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.shared.openURL(url as URL)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
UIAlertAction in
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}

Share Best Score on Facebook

Dears i got an error "Value of type 'GameScene' has no member 'present' "
I want to add a label to share the best-score in facebook the code below works fine in normal ViewController
for touch in touches {
let touchLocation = touch.location(in: self)
if atPoint(touchLocation).name == "shareButtoon" {
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {
let facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText(bestScore.text)
self.present(facebookSheet, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to your Facebook account", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
To be able to present such alert you might use the rootViewController, doing so:
UIApplication.shared.keyWindow?.rootViewController?.present(facebookSheet, animated: true, completion: nil)

Segue after new user creation using firebase not being performed -Swift

I am having issues with two segues however, since I used the same logic in the implementation of each of these segues I'm going to go ahead and condense everything into one question. Basically, I am trying to initialize a segue after performing createUser function with firebase and after performing login function also handled by Firebase, however, when the SignUp and Login buttons are clicked there seems to be no response to the segue being called. Below is the code for each of the #IBActions
#IBAction func signUpComplete(_ sender: Any) {
FIRAuth.auth()?.createUser(withEmail: signUpEmailField.text!, password: signUpPasswordField.text!, completion: { (user: FIRUser?, error) in
if self.signUpEmailField.text == "" || self.signUpPasswordField.text == "" {
let alert = UIAlertController(title: "Oops!", message: "A valid E-mail and Password are Required", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
if FIRAuthErrorCode.errorCodeEmailAlreadyInUse != nil {
let emailExists = UIAlertController(title: "Oops!", message: "A user with this E-Mail already exists!", preferredStyle: UIAlertControllerStyle.alert)
emailExists.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(emailExists, animated: true, completion: nil)
}
else {
DispatchQueue.main.async () {
self.performSegue(withIdentifier: "signUpSucceededSegue", sender: self)
}
}
}
})
}
and
#IBAction func loginButton(_ sender: Any) {
FIRAuth.auth()?.signIn(withEmail: emailLoginField.text!, password: passwordLoginField.text!, completion: { (user: FIRUser?, error) in
if self.emailLoginField.text == "" || self.passwordLoginField.text == ""{
let alert = UIAlertController(title: "Alert", message: "E-Mail is Required", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
if FIRAuthErrorCode.errorCodeUserNotFound != nil {
let invalidLogin = UIAlertController(title: "Alert", message: "E-Mail/Password Incorrect", preferredStyle: UIAlertControllerStyle.alert)
invalidLogin.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(invalidLogin, animated: true, completion: nil)
}
else{
DispatchQueue.main.async () {
self.performSegue(withIdentifier: "loginSucceededSegue", sender: self)
}
}
}
})
are there any glaring logical errors at first glance or is there a better way to implement a segue into these functions?
Embed a Navigation Controller to the very first view of your storyboard
Then Try using this code:-
#IBAction func signUpComplete(_ sender: Any) {
if self.signUpEmailField.text == "" || self.signUpPasswordField.text == "" {
let alert = UIAlertController(title: "Oops!", message: "A valid E-mail and Password are Required", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}else{
FIRAuth.auth()?.createUser(withEmail: self.signUpEmailField!.text, password: self.signUpPasswordField!.text, completion: { (user, err) in
if let ErrorCode = FIRAuthErrorCode(rawValue: err!._code){
switch ErrorCode {
case .errorCodeEmailAlreadyInUse : let emailExists = UIAlertController(title: "Oops!", message: "A user with this E-Mail already exists!", preferredStyle: UIAlertControllerStyle.alert)
emailExists.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(emailExists, animated: true, completion: nil)
break
default : break
}
}else{
let nextView = self.navigationController!.storyboard!.instantiateViewController(withIdentifier: "signUpSucceededSegue")
self.navigationController!.pushViewController(nextView, animated: true)
}
})
}
}
Then alter your other code-block in the similar fashion.

How to access the view controller which is evoked by the stopRecording() method of ReplayKit framework. And how to save the video in my camera roll

How to access the view controller which is evoked by the stopRecording() method of ReplayKit framework. And how to save the video in the camera roll?
Try this if you haven't gotten to work yet:
func stopRecording() {
let sharedRecorder = RPScreenRecorder.sharedRecorder()
sharedRecorder.stopRecordingWithHandler { (previewViewController: RPPreviewViewController?, error: NSError?) in
if previewViewController != nil {
print("stopped recording")
previewViewController!.previewControllerDelegate = self
let alertController = UIAlertController(title: "Recording", message: "Tap view to watch, edit, share, or save your screen recording!", preferredStyle: .Alert)
let viewAction = UIAlertAction(title: "View", style: .Default, handler: { (action: UIAlertAction) -> Void in
self.view?.window?.rootViewController?.presentViewController(previewViewController!, animated: true, completion: nil)
})
alertController.addAction(viewAction)
self.previewViewController = previewViewController!
self.previewViewController.modalPresentationStyle = UIModalPresentationStyle.FullScreen
self.view?.window?.rootViewController!.presentViewController(alertController, animated: true, completion: nil)
} else {
print("recording stopped working")
//create the alert
let alert = UIAlertController(title: "Alert", message: "Sorry, there was an error recording your screen. Please Try Again!", preferredStyle: UIAlertControllerStyle.Alert)
// show the alert
self.view!.window?.rootViewController!.presentViewController(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Try Again!", style: UIAlertActionStyle.Destructive, handler: { action in
// add action
}))
}
}
}
internal func previewControllerDidFinish(previewController: RPPreviewViewController) {
self.previewViewController.dismissViewControllerAnimated(true, completion: nil)
print("cancel and save button pressed")
}

Sharing on FB not working

When I try to share stuff on Facebook from my app, the "Post" button is not working. The cancel button on the left side is working, but the post button on the right is not.
This is my code:
#IBAction func saveToFB(sender: AnyObject) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("Share on Facebook")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
So I tried this out...
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
let fbcomposer = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
fbcomposer.setInitalText(text)
fbcomposer.addImage(UIImage(named: myimg.png))
fbcomposer.addURL(NSURL(string: iosLink))
presentViewController(fbcomposer, animated: true, completion: {})
}
The problem is in your code you are only setting the InitialText. There is some issue with fb that does not allow third party apps to set text when posting. In my case, it was adding the image and the link as well. But the text was not added.
Try adding links or images and see if the problem still persists.
Hope this helps. :)

Resources