Facebook app invitation iOS SDK can't display the Invitation VC - ios

So this's my code for app invite :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}
This code works fine but if I try to add the promotional code like this :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
content.promotionCode = "preview"
content.promotionText = "Use the *preview* code to unlock the app"
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}
The the invite VC is not shown any more (the function is called but nothing is showing). What did I missed here ?

The issue was that I've used special character like * so removing it make the app works fine my final code is like this :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
content.promotionCode = "preview"
content.promotionText = "Use the preview code to unlock the app"
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}

Related

Send Text and Link to Snapchat from my Swift app without the SDK

I am integrating sharing options from my app to Snapchat.
I have a dynamic URL obtained in an object and clicking the Snapchat's share button directly opens the app if Snapchat is there on the device and show the text with the link. I am using the below code to share which gives an error on Snapchat. Below is my Code.
func shareTextOnSnapchat(obj:VideoData) {
let shaUrl = URL(string: obj.share_url ?? "")
if let myURL:URL = shaUrl{
let promoText = "Check out this great new video from \(obj.name ?? ""), I found on talent app"
let shareString = "snapchat://text=\(promoText)&url=\(myURL)"
let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let url = URL(string: escapedShareString)
UIApplication.shared.openURL(url!)
}
}
I have used this to post video to snapchat. You have option to either post text or a video.
Pod used
pod 'SnapSDK', :subspecs => ['SCSDKCreativeKit']
import SCSDKCreativeKit
var scInstalled = false
override func viewDidLoad() {
super.viewDidLoad()
scInstalled = schemeAvailable(scheme: "snapchat://")
}
func ShowSnapchat(){
if scInstalled {
//shareTextOnSnapchat(obj:videoObj ?? VideoData())
shareFileOnSnapchat(obj:videoObj ?? VideoData())
}else{
downloadSharingAppAlert(appName:"Snapchat")
}
}
func shareTextOnSnapchat(obj:VideoData) {
let shaUrl = URL(string: obj.share_url ?? "")
if let myURL:URL = shaUrl{
let originalString = "\(myURL)"
let escapedString = originalString.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed)
//let url = URL(string: "snapchat://snap?text=\(escapedString!)")
let url = URL(string: "https://www.snapchat.com/send?text=\(escapedString!)")
if UIApplication.shared.canOpenURL(url! as URL)
{
UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil)
}
}
}
func shareFileOnSnapchat(obj:VideoData){
//// SHARE VIDEO
LoadingOverlay.shared.showLoaderView(view: self.view)
let shaUrl = URL(string: obj.output_vid ?? "")
if let myURL:URL = shaUrl{
let snapVideo = SCSDKSnapVideo(videoUrl: myURL)
let snapContent = SCSDKVideoSnapContent(snapVideo: snapVideo)
// Send it over to Snapchat
snapAPI.startSending(snapContent) { (error) in
if let error = error {
print(error.localizedDescription)
LoadingOverlay.shared.hideLoaderView()
MyCustomAlert.sharedInstance.ShowAlert(vc: self, myTitle: "", myMessage: StringClass.sharedInstance.lcStr_oopsSomethingWentwrong)
} else {
// success
print("Posted to snapchat")
LoadingOverlay.shared.hideLoaderView()
MyCustomAlert.sharedInstance.ShowAlert(vc: self, myTitle: "", myMessage: StringClass.sharedInstance.lcStr_postedToSnapchat)
}
}
}
}
}
func downloadSharingAppAlert(appName:String){
var appStoreURL = "https://apps.apple.com/in/app/snapchat/id447188370"
//Open Appstore for Download
}

WKWebView load request in device not working, but simulator is working

I have a question about WKWebview.
First, I have a UITabbarViewController(tab1, tab2), and two ViewControllers.
In ViewController1 have two button (button1, button2) which action is click the tabbar tab2 to present the ViewController2 and bring different url string.
In ViewController2 have a WKWebView, and load the request of url string.
I work fine in simulator, but build in device and work fine at first time and the then I tap the tabbar tab1 and back to ViewController1.
I click the button2 and present the ViewController2.
And I also set breakpoint, the WKWebview have run the load request function.
But the WKWebview also show me the button1 url content, not button2 url content. When I back and click button2 again, it's work. Why the WKWebView don't refresh at first time?
Thanks.
Xcode 9.4.1, device iOS 12.0.1(16A404)
class ViewController1: UIViewController {
#objc func btnTapped(sender: UIButton) {
if let vc = self.navigationController?.parent as? ViewController {
if let number = item?.number {
let domainPath = “\(customURL!.absoluteString)"
let extPath = "detail/\(number)/&topPageBack=list"
let encodestr = extPath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let finalPath = domainPath.appending("/").appending(encodestr!)
if let url = URL(string: finalPath) {
vc.loadSpecificURL = url
}
vc.tabBarView.buttonClick(tag: 1)
}
}
}
}
class ViewController2: UIViewController {
lazy var webViewController: CustomWKWebViewController = {
let controller = CustomWKWebViewController()
controller.wKWebViewPage = .searchPage
return controller
}()
var loadURL: URL?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let url = loadURL {
webViewController.reloadWebView(url: url)
loadURL = nil
}
}
}
class CustomWKWebViewController: UIViewController {
private func settingWebView() -> WKWebView {
let userScript = WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: true)
let userContentController = WKUserContentController()
userContentController.add(self, name: "Login")
userContentController.add(self, name: "Other")
userContentController.add(self, name: “LD”)
userContentController.addUserScript(userScript)
let config = WKWebViewConfiguration()
config.userContentController = userContentController
webView = WKWebView(frame: .zero, configuration: config)
webView!.navigationDelegate = self
webView!.uiDelegate = self
webView!.scrollView.bounces = false
webView!.allowsBackForwardNavigationGestures = true
webView!.backgroundColor = .clear
webView!.scrollView.backgroundColor = .clear
if let request = getRequest() {
webView!.load(request)
}
return webView!
}
func reloadWebView(url: URL?) {
if let url = url, let webView = self.webView {
var requestForBlank = URLRequest(url: URL(string: "about:blank")!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 3)
requestForBlank.httpMethod = "POST"
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 3)
request.httpMethod = "POST"
webView.load(requestForBlank)
webView.load(request)//breakpoint always break here, but the view don't load request.
}
}
}
First you check if web url not nil, then also call the else part of if.
if let url = webview.url {
webview.reload()
} else {
webview.load(URLRequest(url: originalURL))
}

Deep linking for Facebook shareLinkContent

I'm working with deep link in iOS. I am going to share a link in Facebook using `FBSDKShareLinkContent. I have created deep linking URL in Facebook like https://fb.me/****************.
I have already done AppInviteContent and it works good like this:
let content : FBSDKAppInviteContent = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "https://fb.me/****************")!
content.appInvitePreviewImageURL = NSURL(string: "http://***.***.***.***/shareImage.png" as String)!
FBSDKAppInviteDialog.showWithContent(content, delegate: self)
Now, I am sharing link in Facebook like this:
let shareLinkContent : FBSDKShareLinkContent = FBSDKShareLinkContent()
shareLinkContent.contentURL = NSURL(string: "https://example.com/a2d69835ae")!
shareLinkContent.contentTitle = "App_Name"
shareLinkContent.contentDescription = "Description"
let dialog : FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = shareLinkContent
dialog.mode = FBSDKShareDialogMode.Web
dialog.show()
How to set deep link URL (e.g. https://fb.me/****************) in this shareLinkContent.
A very apt idea is to use Branch framework for the deep linking feature. You can get to know how to use this framework from here https://branch.io/
It can be used to share your app contents to any of the social networking sites. it has the feature on universal linking and deep linking.
Remove this below code, this have been modified in latest pod version.
let dialog : FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = shareLinkContent
dialog.mode = FBSDKShareDialogMode.Web
dialog.show()
Add this code will show Facebook Share dialog:
FBSDKShareDialog.show(from: self, with: shareLinkContent, delegate: self)
This will fix your FBSDK share issue.
For APP invite try some thing like this:
{
let content : FBSDKAppInviteContent = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "https://fb.me/****************")!
content.appInvitePreviewImageURL = NSURL(string: "http://***.***.***.***/shareImage.png" as String)!
FBSDKAppInviteDialog.show(from: self, with: content, delegate: self)
}

Open eMail from App with predefined text in iOS

Hello i want to open the eMail program from my App and the body should already be defined. I can open the eMail but don't know how to define the body of the eMail as a given Parameter to show a given standard text. Anyone can help? Heres the code i use to open Email:
//EMAIL
let email = "foo#bar.com"
let urlEMail = NSURL(string: "mailto:\(email)")
if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}
If you'd like to open the built-in email app as opposed to showing the MFMailComposeViewController as has been mentioned by others, you could construct a mailto: link like this:
let subject = "My subject"
let body = "The awesome body of my email."
let encodedParams = "subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let url = "mailto:foo#bar.com?\(encodedParams)"
if let emailURL = NSURL(url) {
if UIApplication.sharedApplication().canOpenURL(emailURL) {
UIApplication.sharedApplication().openURL(emailURL)
}
}
Just to save anyone typing, for 2016 the syntax has changed slightly:
let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:blah#blah.com?subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.sharedApplication().canOpenURL(emailURL)
{
UIApplication.sharedApplication().openURL(emailURL)
}
Swift 3 Version
let subject = "Some subject"
let body = "Plenty of email body."
let coded = "mailto:blah#blah.com?subject=\(subject)&body=\(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL: NSURL = NSURL(string: coded!) {
if UIApplication.shared.canOpenURL(emailURL as URL) {
UIApplication.shared.openURL(emailURL as URL)
}
}
You can do it using MFMailComposeViewController:
import MessageUI
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["email#email.com"])
mailComposerVC.setSubject("Subject")
mailComposerVC.setMessageBody("Body", isHTML: false)
self.presentViewController(mailComposerVC, animated: true, completion: nil)
Also, you need to implement mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate where you should dismiss MFMailComposeViewController
Swift 4.0
let email = "feedback#company.com"
let subject = "subject"
let bodyText = "Please provide information that will help us to serve you better"
if MFMailComposeViewController.canSendMail() {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients([email])
mailComposerVC.setSubject(subject)
mailComposerVC.setMessageBody(bodyText, isHTML: true)
self.present(mailComposerVC, animated: true, completion: nil)
} else {
let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL = URL(string: coded!)
{
if UIApplication.shared.canOpenURL(emailURL)
{
UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in
if !result {
// show some Toast or error alert
//("Your device is not currently configured to send mail.")
}
})
}
}
}
Use the MFMailComposeViewController like this:
Import the MessageUI
import MessageUI
Add the delegate to your class:
class myClass: UIViewController, MFMailComposeViewControllerDelegate {}
Configure the email preset you want to have
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("Subject")
mail.setMessageBody("Body", isHTML: true)
mail.setToRecipients(["my#email.com"])
presentViewController(mail, animated: true, completion: nil)
Put this method in your code:
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
dismissViewControllerAnimated(true, completion: nil)
}
There you go, works now.
Similar to url construction in the other answers, but instead of calling addingPercentEncoding you can use URLComponents:
var components = URLComponents(string: "youremail#test.com")
components?.queryItems = [URLQueryItem(name: "subject", value: "Your Subject")]
if let mailUrl = components?.url {
UIApplication.shared.open(mailUrl, options: [:], completionHandler: nil)
}
Updated for Xcode 12.5
let url = NSURL(string: "mailto:mailto:someone#example.com")
UIApplication.shared.open(url! as URL)
or if you would like to add an embedded subject
let url = NSURL(string: "mailto:someone#example.com?subject=This%20is%20the%20subject&cc=someone_else#example.com&body=This%20is%20the%20body")
or if you want to add multiple email addresses
let url = NSURL(string: "mailto:mailto:someone#example.com,someoneelse#example.com")
UIApplication.shared.open(url! as URL)
I suggest to use Apple's way, which you can find in official documentation about MFMailComposeViewController. It opens a modal view controller with the email, which is dismissed after sending. Thus user stays in your app.
Swift 5 version of #mclaughj answer
let email = "foo#bar.com"
let subject = "Your Subject"
let body = "Plenty of email body."
let coded = "mailto:\(email)?subject=\(subject)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let emailURL:NSURL = NSURL(string: coded!)
{
if UIApplication.shared.canOpenURL(emailURL as URL){
UIApplication.shared.open(emailURL as URL)
}
}
Cheers!

blank screen appears when click on UILabel in swift

i'm trying to open iPhone call screen with selected number when click on UIlabel which is hyperlink in swift.
a blank screen appears for 1 second and goes. No iPhone call screen appears. Don't know what is wrong in my code.
Code in viewDidLoad
let strPhone = "080 2854 2105"
let attributedPhoneStr = NSMutableAttributedString(string:strPhone, attributes:[NSLinkAttributeName: NSURL(string: "http://aerospace.honeywell.com")!])
lblPhoneContact.attributedText = attributedPhoneStr
lblPhoneContact.userInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed"))
lblPhoneContact.addGestureRecognizer(gestureRecognizer)
Function labelPressed
func labelPressed() {
let url:NSURL = NSURL(string: "tel://08028542105")!
UIApplication.sharedApplication().openURL(url)
}
what is wrong or missing in my code.
I tried running on my iPhone device.
for swift use
let url:NSURL = NSURL(string: "telprompt:08028542105")!
instead of
let url:NSURL = NSURL(string: "tel://08028542105")!
You can try to use the GCD's dispatch_async to invoke the "openURL" method:
dispatch_async(dispatch_get_main_queue()) { () -> Void in
UIApplication.sharedApplication().openURL(url)
}
You should omit the // from the uri - I.e. It should just be
let url:NSURL = NSURL(string: "tel:08028542105")!

Resources