Social Framework doesn't work properly Swift - ios

I want to have a share button in my app for sharing to Facebook.
I decided to do it with the social framework as i believed it was easier.
I was wrong...
Here is my code:
#IBAction func facebookButton(sender: AnyObject) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
let fbSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
fbSheet.setInitialText("Some text")
fbSheet.addImage(UIImage(named: "loading"))
fbSheet.addURL(NSURL(string: "http://www.something.com"))
presentViewController(fbSheet, animated: true, completion: nil)
} else {
self.showAlert("Facebook", msg: "Please login to your Facebook account in Settings")
}
}
This code is working, but it returns only the url and if I comment it; it returns only the image.
I have changed the order and I put image third but again it returns only the url.
It's like it has priorities and it returns only one of them.

Since last year Facebook doesn't allow to prefill the text if you want to share something. You can set only hashtags as setInitialText. something like this
fbSheet.setInitialText("#\(appname)")
https://developers.facebook.com/docs/apps/review/prefill

Related

migrating project to comply with google sign in requirements for iOS

I'm trying to get a google sign in button to work with my iOS app, but I'm getting this error here:
Cannot find 'presentingViewController' in scope
This is the code segment:
func handleSignInButton() {
GIDSignIn.sharedInstance.signIn(
with: signInConfig,
presenting: presentingViewController // this is the line with the error,
callback: GIDSignInCallback? = nil) { user, error in
guard let signInUser = user else {
// Inspect error
return
}
// If sign in succeeded, display the app's main content View.
}
}
I've been told I need to migrate the way I am signing in and I found this here:
https://developers.google.com/identity/sign-in/ios/quick-migration-guide
but I'm confused about this part here:
Manually connect GIDSignInButton to a method that calls signInWithConfiguration:presentingViewController:callback: using an IBAction or similar.
Can someone show me how to do this properly? I'm a iOS novice :)
Thanks!

How to know if user shared my app or not?

My app is all about getting points and winning money, and what I need to do is let the user share my app then give him some points.
The problem is that I don't know how to detect if the user truly shared the app or not
I'm using the following code:
func shareTapped(){
let text = "example"
let url = URL(string: "example.com")
let image = UIImage(named: "example_image")
let shareViewController = UIActivityViewController(activityItems: [text, image!, url!] ,applicationActivities: nil)
self.present(shareViewController, animated: true, completion: {() in
print("done")
})
}
The sharing method is working perfectly, but I was wondering if there is any delegate we can call in this situation.
Thanks.
So there is 2 scenarios where user can cancel share.
One is when UIActivityViewController present then there is a cancel button on UIActivityViewController from where user can cancel it and yes you can detect it with method
shareViewController.completionWithItemsHandler = { activity, completed, items, error in
}
In above method completed will be false if user canceled from UIActivityViewController cancel button. and it will return true if user share successfully but here comes second case with it.
So for second case suppose user want to share via watsapp and click on watsapp icon from UIActivityViewController and watsapp user list appear.
But there is a cancel button on that screen from where user can cancel sharing but still you will get completed flag true so there is no way you can detect if user click on cancel button from watsapp user list.

IOS swift how to know when activityViewController has been successfully used

I have a tableView that utilizes the UIActivityViewController so users can share content on other 3rd party networks Facebook, twitter, text message etc. I have a function called IncreaseShareByOne() and it's purpose is to count the number of times that something has been shared . My problem is that right now that function fires of every time the UIActivityViewController is clicked . Is there someway that I can find out if a user really shared something so that I can use that function correctly ? becomes sometimes you open the UIActivityViewController and decide not to share anything so I want to catch and not use that function. I am new to swift and am using version 3 .
func sharedShareAction(controller: UIViewController, sender: UIButton) {
controller.present(activityViewController,animated: true,completion: nil)
IncreaseShareByOne()
}
You can add a completion handler to your UIActivityViewController. In the completion handler, check whether the user submitted using the completed value. You'll probably want to do something like this:
func sharedShareAction(controller: UIViewController, sender: UIButton) {
controller.present(activityViewController,animated: true, completion: nil)
activityViewController.completionWithItemsHandler = { activity, completed, items, error in
if !completed {
// handle task not completed
return
}
IncreaseShareByOne()
}
}
Check the API docs for more info.

Social Framework For Facebook - Placeholder Text in Swift

As stated by facebook, prefill is not allowed anymore with the social framework. But as the statement below;
Your app’s composer can include a call-to-action that disappears when people start to write a post. For example, Facebook's composer uses grey scale text to ask “What's on your mind?” that disappears when people start to write.
Can we put a placeholder text that dissapears when a user starts to write text via the social framework facebook share?
Currently my initial text code which is below is not working.
func facebookShareFunc() {
let shareToFacebook : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
shareToFacebook.setInitialText(“Initial text here”)
shareToFacebook.completionHandler = {
(result:SLComposeViewControllerResult) in
let getResult = result as SLComposeViewControllerResult;
switch(getResult.rawValue) {
case SLComposeViewControllerResult.Cancelled.rawValue: print("User cancelled post")
case SLComposeViewControllerResult.Done.rawValue: self.workingFunc()
default: print("Shouldn't happen")
}
}
self.presentViewController(shareToFacebook, animated: true, completion: nil)
}

How to add google places autocomplete to xcode with swift (tutorial)

I want to add google places autocomplete to xcode with swift, so users can search on a city and press enter, so should the app show that city on the map.
I´m using google maps, so it has to be connected to that map and the search bar would i like to have in the navi-bar (just above the map)
Does anyone know a good tutorial to do that??
For Swift 3:
1- Select your podfile and type : pod 'GooglePlaces'
2- In the appDelegate, add your API Key :
GMSPlacesClient.provideAPIKey("YOUR KEY") (Import the GooglePlaces)
3- Use this code in your viewController that contains the googleMap:
// This code snippet demonstrates adding a
// full-screen Autocomplete UI control
import UIKit
import GooglePlaces
class ViewController: UIViewController {
// TODO: Add a button to Main.storyboard to invoke onLaunchClicked.
// Present the Autocomplete view controller when the button is pressed.
#IBAction func onLaunchClicked(sender: UIButton) {
let acController = GMSAutocompleteViewController()
acController.delegate = self
present(acController, animated: true, completion: nil)
}
}
extension ViewController: GMSAutocompleteViewControllerDelegate {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: \(error)")
dismiss(animated: true, completion: nil)
}
// User cancelled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
print("Autocomplete was cancelled.")
dismiss(animated: true, completion: nil)
}
}
Not exactly tutorials, but there are a few resources on Github, with people providing libraries and code samples to achieve what you're asking for.
SPGooglePlacesAutocomplete (https://github.com/spoletto/SPGooglePlacesAutocomplete) is very popular and has many features, but it's written in Objective-C
iOS Google Places Autocomplete (https://github.com/watsonbox/ios_google_places_autocomplete) is more recent, written in Swift, and also contains some examples.
lots of others...
Even without proper tutorials, you can still check out these libraries and understand how their code works to get some inspiration if you want to write your own components.
As a sidetone, Google also has this page, but it's not a dedicated tutorial for iOS apps, just some useful explanations of how their API works: https://developers.google.com/places/documentation/autocomplete#examples
You can try one for wrapper to implement Autocompletion for Place API with Google:
https://github.com/mrugrajsinh/MVAutocompletePlaceSearchTextField
Its very simple drop-in control and subclass of UITextField so using by binding Class with simple UITextField you can achive Auto completion dropdown similar as Uber and many popular app does.
Here's a tutorial I came across which is based on my ios_google_places_autocomplete code.
#Frederik: If you've had a problem with the library, please create an issue describing it.
please check this one 110% work.
http://sweettutos.com/2015/09/30/how-to-use-the-google-places-autocomplete-api-with-google-maps-sdk-on-ios/
Use this latest repo. with swift 2.0
Just download zip file of project and open pod file and write pod 'GoogleMaps' and save it open terminal and install pod.
Enjoy!!!
RKAutoCompletePlaceSearch
Since the question was asked, Google has added UI elements to the iOS SDK for this sort of workflow. Check out the documentation.
Simple and lightweight solution for Swift users !
I also created a library to make these simple request without including any framework of third party library. You can also maintain cache for Autocomplete with the library.
To use to library follow these steps : -
step-1 Import GoogleApiHelper into your project.
step-2 Initialise GoogleApiHelper
GoogleApi.shared.initialiseWithKey("API_KEY")
step-3 Call the methods
var input = GInput()
input.keyword = "San francisco"
GoogleApi.shared.callApi(input: input) { (response) in
if let results = response.data as? [GApiResponse.Autocomplete], response.isValidFor(.autocomplete) {
//Enjoy the Autocomplete Api
} else { print(response.error ?? "ERROR") }
}
You can find the library here

Resources