Choose what share actions get what content with UIActivityViewController - ios

I am currently using UIActivityViewController to share an url to other apps. This part works fine.
But when I'm sharing to an app like iMessage, Whatsapp or Mail. I would like to add a string.
Example:
Sharing -> airdrop: https://www.google.com
Sharing -> iMessage: "Hi there, checkout this cool website: https://www.google.com"
I tried to do it by adding both an Url object and a String into the UIActivityViewController like so:
let url = URL(string: "https://www.google.com)
let text = "Hi there, checkout this cool website: \(url)"
let items: [Any] = [url, text]
let controller = UIActivityViewController(
activityItems: items,
applicationActivities: nil)
DispatchQueue.main.async{self.present(controller, animated: true, completion: nil)}
^not the actual code, but enough to draw a picture
This does work with Airdrop, it opens the url.
It also works in Mail, it uses the string containing the url.
It doesn't work however in iMessage. iMessage takes both and combines them like so:
https://www.google.com Hi there, checkout this cool website: https://www.google.com
Does anyone have any suggestions for me to keep the functionality for Airdrop/Mail etc. But for it to also work on iMessage?

Use a UIActivityItemSource. The UIActivityItemSource is told the identifier of the requesting process, so you can refrain from handing the URL to Messages and hand it just the string, which is sufficient.

Related

Facebook SDK Share Image with Text

Prior to using Facebook SDK we used to share via UIActivityViewController since Facebook does not allow for us to pre-fill information on the user sharing, our solution was to use information the user description of the Image being share UIPasteboard.general.string. So the app would switch to the messenger and the user could paste. This worked just fine until we started using the Facebook SDK.
Now it seems that the UIPasteboard.general.string is reset when it opens up messenger and we no longer can get the image description copied to the clipboard.
This is how I'm sharing to messenger:
let sharePhoto = FBSDKSharePhoto()
sharePhoto.image = image
let content = FBSDKSharePhotoContent()
content.photos = [sharePhoto]
FBSDKMessageDialog.show(with: content, delegate: delegate)
Yes, I'm stacked on that staff too after FB lib update. As for me - I figured out with web link on image like it shown below. As a result - you do not need ask user to paste something. You can do it by yourself and paste it to title or description.
let content = LinkShareContent(url: url,
title: title,
description: description,
quote: nil,
imageURL: imageURL)
let dialog = ShareDialog(content: content)
dialog.presentingViewController = parentVC
dialog.mode = .automatic
dialog.completion = { result in
...
}
try? dialog.show()
Facebook is not support UI share both image and text. If you want to share an image and a text together, you can create a custom story from your app's settings, and share it as an open graph story. Here's the documentation.

iMessaged-based invitations for GameCenter for iOS 10

I'm trying to update my app to work correctly with the new features of GameCenter in iOS10.
I create a new GKGameSession on device1, get a share URL, and all that works fine. I send the share URL out via a share sheet to device 2.
Device2 clicks the link, the device briefly displays 'Retrieving...' and then launches my app. Great! But, now what? Is there context information available for this URL that I can somehow access? Otherwise I have no way how to respond when the app is launched.
Previously you'd get a callback to something adhering to the GKLocalPlayerListener protocol, to the method player:didAcceptInvite:, and you could join the match that way. But with these iCloud-based messages, the player might not be even logged into GameCenter, right? This part seems to have been glossed over in the WWDC presentation.
Also, as of today (12/28/2016) there is no Apple documentation on these new methods.
Since the GKGameSessionEventListener callback session:didAddPlayer: only fires if the game is already running, to be sure you can process this callback every time requires a work around. I've tested this and it works.
When you send out an iMessage or email invite to the game, don't include the Game Session Invite URL directly in the message. Instead use a registered URL that will open your app when opened on a device on which your app is installed. Check here to see how:
Complete Tutorial on iOS Custom URL Schemes
But add a percent escaped encoding of the game invite URL as a parameter to this URL thusly (I'm assuming the registration of a url e.g. newGameRequest but it will be best to make this quite unique, or even better - though it requires more setup, try Universal Link Support as this will allow you to direct users who don't have your app installed to a webpage with a download link)
let openOverWordForPlayerChallenge = "newGameRequest://?token="
gameState.gameSession?.getShareURL { (url, error) in
guard error == nil else { return }
// No opponent so we need to issue an invite
let encodedChallengeURL = url!.absoluteString.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
let nestedURLString = openOverWordForPlayerChallenge + encodedChallengeURL!
let nestedURL = URL(string: nestedURLString)!
}
send the URL in a message or email or WhatsApp or whatever. Then in your app delegate, add the following:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
var success = false
if let queryString = url.query {
if let urlStringToken = queryString.removingPercentEncoding {
let token = "token="
let startIndex = urlStringToken.startIndex
let stringRange = startIndex..<urlStringToken.index(startIndex, offsetBy: token.characters.count)
let urlString = urlStringToken.replacingOccurrences(of: token, with: "", options: .literal, range: stringRange)
if let url = URL(string: urlString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
success = true
}
}
}
}
return success
}
Now you can be sure the session:didAddPlayer: will be called. What's the betting this workarround is good for about 2 weeks, and they fix this in the next release of iOS showcased at WWDC 2017 ! Update: this problem hasn't been fixed - so the workaround above remains good!
I agree, the lack of documentation is frustrating. From what I can see, we have to:
add <GKGameSessionEventListener> protocol in the class' header
Then session:didAddPlayer: fires on the joining player's device after accepting an invite link.
update:
Unfortunately, I'm not surprised to hear your results. I hadn't tried all of those scenarios, but GKTurnBasedMatch had similar shortcomings. The way I got around it there was: I added a list of player statuses to match data (invited, active, quit, etc). I gave the player a view of "pending invitations." When they opened that view, I would load all of their matches and display the entries where the player was in invited state. With GKGameSession, that should work too.
Or, it might be easier if you could maintain a local list of sessions that you are aware of. Whenever the game becomes active, pull the entire list of sessions from the server and look for a new entry. The new entry would have to be the match the player just accepted by clicking the share URL.

while sharing content on facebook it looks different after sharing than content shared from php

I am using FBSDKShareDialog to share content from my iOS App but it doesn't attractive like content shared with PHP side.
Below is my code for FBSDKShareDialog to share content over facebook.
let shareLinkContent:FBSDKShareLinkContent = FBSDKShareLinkContent()
shareLinkContent.contentURL = NSURL(string: "http://www.google.com")
shareLinkContent.imageURL = strImageUrl
shareLinkContent.contentTitle = strName
shareLinkContent.contentDescription = "Ik heb ‘\(strName)’ beoordeeld en gaf deze whisky '\(strPercentage)' punten"
FBSDKShareDialog.showFromViewController(self, withContent: shareLinkContent, delegate: self)
Check image below to see difference between content shared using my iOS App and PHP site.
1. Post from iOS App.
1. Post from PHP Site.
Please tell me solution for this so I can post content to Facebook like PHP site.
I just want to share content like Second Image i.e content shared from PHP site.

how to send verification code by sms in swift 2

i build a register form for my app and i need to send the user a verifiation code by sms in order to complete the registration proccess.
i tried to use MFMessageComposeViewController but its open the dialog sms on the device so the user can see the code.
i also checked the web for 3party of sending sms but there is a problem with the country code. i know its posible becuse whatsapp do it to confirm the user.
what it the right way to do it?
this is the topic the i tried:
Sending SMS in iOS with Swift
The best way to achieve this is by creating some views for allowing the user to enter the phone number with the country code which can be used by a server to send a request for initiating the OTP verification. To achieve this you need to:
Create View Controllers.
Upload Phone Number and Country code to the server.
Validate the requests by verifying the OTP.
As mentioned by Dan, you can use Digits in Fabric for that purpose, and create custom views for a great UX.
On the other hand, you can also use a service called as SendOTP from MSG91 - you can use it for internal testing and development ideas as they provide you with 5,000 free OTP SMS. The service has a complete set of APIs which you can implement on the backend as well on the app front. Also, they provide a framework so that you don't need to create the views, but only presentViewController and call delegate methods for knowing what happened during the verification process - such as Cancelled or Verified or Failed.
Swift implementation of the same looks like this:
class OTPFrame: UIViewController, sendOTPAuthenticationViewControllerDelegate {
func loadOTPFramework() {
SendOTP.sharedManager().startWithApiId("yourAppID")
let frameworkPath: NSString = NSBundle.mainBundle().privateFrameworksPath!
let frameworkBundlePath: NSString = frameworkPath.stringByAppendingPathComponent("SendOTPFramework.framework")
let frameworkBundle : NSBundle
= NSBundle(path: frameworkBundlePath as String)!
let authenticationViewController: AuthenticationViewController = AuthenticationViewController(nibName: "AuthenticationViewController", bundle: frameworkBundle)
authenticationViewController.delegate = self self.presentViewController(authenticationViewController, animated: true, completion: nil)
}
func authenticationisSuccessfulForMobileNumber(mobNo: String!, withCountryCode countryCode: String!) {
print("Success")
}
func canceledAuthentication() {
print("Cancelled")
}
func authenticationisFailedForMobileNumber(mobNo: String!, withCountryCode countryCode: String!) {
print("Failed")
}
}
Disclaimer: I, in no way, endorse the services mentioned above - you are free to choose whatever you like.
Thank You!
I would give digits a try! It's part of the Twitter Fabric package and it's very simple to use. The user enters their phone number and Fabric takes care of validating the number.

Unable to share url on whatsapp for iPhone

I have the code below which i am using to share the url on whatsapp using whatsapp url scheme, but when i do this i see empty message on the whatsapp message screen.
let itunesLink = "http://google.com";
let text = itunesLink.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet());
let url = NSURL(string: "whatsapp://send?text=\(text!)")
UIApplication.sharedApplication().openURL(url!);
When this code runs, i see the contacts and after i select the whatsapp account option i dont see the url and see the empty screen.
I am always confused why there are so many allowed characters in these sets. I suspect you may need to add more percent-encoding. This is what I use to encode URLs embedded in URLs:
let set = NSCharacterSet(charactersInString: ".-0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~")
let text = itunesLink.stringByAddingPercentEncodingWithAllowedCharacters(set)
Because "/" is an allowed character in a URL Path, URLPathAllowedCharacterSet includes "/", and the "/" in itunesLink will not be percent-encoded, using the original program. That might be confusing Whatsapp. It certainly confuses me.

Resources