iMessage app won't insert message into active conversation - swift

I'm having an issue with a Message Extension app I'm trying to create. Whenever I try to insert text into a conversation nothing happens. This is what I'm using:
import UIKit
import Messages
class MessagesViewController: MSMessagesAppViewController {
override func viewDidLoad() {
super.viewDidLoad()
let sendButton = UIButton(type: UIButtonType.custom)
sendButton.frame = CGRect(x: view.frame.midX, y: view.frame.midY, width: 100, height: 50)
sendButton.setTitle("Send", for: UIControlState.normal)
sendButton.addTarget(self, action: #selector(sendButtonTapped(sender:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(sendButton)
}
func sendButtonTapped(sender: UIButton!){
let layout = MSMessageTemplateLayout()
layout.caption = "My Survey"
layout.image = UIImage(named: "myImage")
let message = MSMessage()
message.layout = layout
self.activeConversation?.insert(message, completionHandler: nil)
}
}
Whenever I run it using this code nothing happens. The weird part is that if I change self.activeConversation? to self.activeConversation! the app crashes. I don't understand why this is happening because I'm in a conversation and my app has loaded.

Can you try this.
func sendButtonTapped(sender: UIButton!)
{
guard let conversation = activeConversation else { fatalError("Expected a conversation") }
let session = conversation.selectedMessage?.session ?? MSSession()
let message = MSMessage(session: session)
let layout = MSMessageTemplateLayout()
layout.image = createImageForMessage(value: GlobalData.sharedManager.cardUrl)
layout.caption = "Caption"
layout.imageTitle = GlobalData.sharedManager.cardTitle
layout.imageSubtitle = "Image subtitle"
layout.trailingCaption = "Trailing caption"
layout.subcaption = "Subcaption"
layout.trailingSubcaption = "Trailing subcaption"
message.layout = layout
conversation.insert(message) {error in
// empty for now
}
}}

Related

Dismiss alert view controller in a popover page

In a popover page, I'm trying to store some data in CoreData, and I need to show a loading view until the process is done.
I found a good and easy way, using a alert controller to show a loading label. In the function, I added a shouldPresent Boolean to make it false when the coreData process is done and hide the alert.
private func presentLoadingView(shouldPresent: Bool) {
let alert = UIAlertController(title: nil, message: "Retrieving Owner Data", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 3, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.medium
loadingIndicator.startAnimating()
alert.view.addSubview(loadingIndicator)
presentAnimated(alert)
if !shouldPresent {
alert.dismiss(animated: true)
}
}
The problem is, when I use
dismiss(animated: true)
the entire popover will be dismissed and when I use
alert.dismiss(animated: true)
Nothing happen, could anyone help me on this. Thanks in advance.
Try replaceing
presentAnimated(alert)
if !shouldPresent {
alert.dismiss(animated: true)
}
with
if shouldPresent {
presentAnimated(alert)
}
It seems it will do the work , accroding to your example code.
I think using alert view for loading is not good enough for your needs.
You can use this extension easily for showing a loading view.
extension UIViewController{
func showActivityIndicator(){
DispatchQueue.main.async {[weak self] in
guard let self = self else {return}
let indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
indicator.backgroundColor = UIColor.init(hexString: "#010101").withAlphaComponent(0.6)
indicator.layer.cornerRadius = 6
indicator.center = self.view.center
indicator.hidesWhenStopped = true
indicator.color = UIColor(hexString: "#FFEE00")
indicator.style = UIActivityIndicatorView.Style.large
indicator.startAnimating()
self.view.isUserInteractionEnabled = false
indicator.tag = 1000000
for subView in self.view.subviews{
if subView.tag == 1000000{
print("Already Added")
return
}
}
self.view.addSubview(indicator)
}
}
func hideActivityIndicator(){
DispatchQueue.main.async { [weak self] in
guard let self = self else {return}
let indicator = self.view.viewWithTag(1000000) as? UIActivityIndicatorView
indicator?.stopAnimating()
indicator?.removeFromSuperview()
self.view.isUserInteractionEnabled = true
indicator?.isHidden = true
}
}
}
In your view controller just call self.showActivityIndicator() when you want to show loader
and call self.hideActivityIndicator() for dismiss the loader.
You can use this technique
var alert:UIAlertController? = UIAlertController() //set variable outside in your popover
private func presentLoadingView(shouldPresent: Bool) {
if shouldPresent {
if self.alert == nil{
self.alert = UIAlertController(title: nil, message: "Retrieving Owner Data", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 3, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.medium
loadingIndicator.startAnimating()
alert.view.addSubview(loadingIndicator)
}
if !self.alert.isBeingPresented {
self.present(self.alert, animated: true, completion: nil)
}
}else{
self.alert.dismiss(animated: false, completion: nil)
}
}

Swift Firebase : How to add on new questions to the list?

Currently this app has a textfield and when you press return on the keyboard it displays the live data on the screen but It deletes the previous question. My question is how can I not delete the previous question and just add this new question to the list. Thank you so much!
import UIKit
import FirebaseFirestore
class ViewController: UIViewController, UITextFieldDelegate {
let database = Firestore.firestore()
private let label: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.numberOfLines = 0
return label
}()
private let field: UITextField = {
let field = UITextField()
field.placeholder = "enter text.."
field.layer.borderWidth = 1
field.layer.borderColor = UIColor.black.cgColor
return field
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(label)
view.addSubview(field)
field.delegate = self
let docRef = database.document("ios/ex")
docRef.addSnapshotListener {[weak self] snapshot, error in
guard let data = snapshot?.data(), error == nil else {
return
}
guard let text = data["text"] as? String else {
return
}
DispatchQueue.main.async {
self?.label.text = text
}
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
field.frame = CGRect(x: 1, y: view.safeAreaInsets.top+10, width: view.frame.size.width-20, height: 50)
label.frame = CGRect(x: 1, y: view.safeAreaInsets.top+10+60, width: view.frame.size.width-20, height: 100)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let text = textField.text, !text.isEmpty {
savedData(text: text)
}
return true
}
func savedData(text: String){
let docRef = database.document("ios/ex")
docRef.setData(["text": text])
}
}
save array in your "text" key instead of just string, and update array using this Firebase method called FieldValue.arrayUnion('your new value')

User profile information is not showing up using swift and firestore database

pic of database
I am trying to show a label with the user's status on their profile page. after logging in the user gets presented with a VC that has a side menu. on that side menu is a "profile" option. once choosing this they go to their profile controller. right now i simply need to search users/current uid/ "MembershipStatus" and present this result into a label called "welcomeLabel".
I am returning nul
import UIKit
import Firebase
class NonMemberProfileController: UIViewController {
// MARK: - Properties
var welcomeLabel: UILabel = {
let label = UILabel()
label.textColor = .white
label.font = UIFont.systemFont(ofSize: 28)
label.translatesAutoresizingMaskIntoConstraints = false
label.alpha = 0
return label
}()
// MARK: - Init
override func viewDidLoad()
{
super.viewDidLoad()
authenticateUserAndConfigureView()
}
func loadUserData()
{
guard let uid = Auth.auth().currentUser?.uid else {return}
//.child("MembershipStatus")
Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value) {
(snapshot) in
if snapshot.hasChild("MembershipStatus"){
print("true we have bingo")
} else {
print("no bueno")
dump(snapshot)
}
guard let status = snapshot.value as? String else { return }
self.welcomeLabel.text = "Welcome, \(status)"
print("this is lkjfdskjklsfad" + status)
UIView.animate(withDuration: 0.5, animations: {
self.welcomeLabel.alpha = 1
})
}
}
func authenticateUserAndConfigureView(){
if Auth.auth().currentUser == nil {
DispatchQueue.main.async {
let navController = UINavigationController(rootViewController: LoginViewController())
navController.navigationBar.barStyle = .black
self.present(navController, animated: true, completion: nil)
}
} else {
configureNavigationBar()
loadUserData()
}
}
// MARK: - Selectors
#objc func handleDismiss() {
dismiss(animated: true, completion: nil)
}
// MARK: - Helper Functions
func configureNavigationBar() {
view.backgroundColor = UIColor.lightGray
navigationItem.title = "Profile"
navigationController?.navigationBar.barTintColor = .darkGray
navigationController?.navigationBar.barStyle = .black
navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "Home_2x").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "baseline_settings_white_24dp").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
view.addSubview(welcomeLabel)
welcomeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
You are using Cloud Firestore for data storage but your code is reading data form RealTime Database. You have to read data like this:
let userRef = Firestore.firestore().collection("users").document(uid)
userRef.getDocument { (documentSnapshot, error) in guard
let document = documentSnapshot?.data() else {
print(error)
return
}
print(document)
}

Reload BarButtonItem after pressing (Change formatted string title)

I have added a bar button item programmatically that will change the temperature units on some variables between Celsius and Fahrenheit and have set it with attributed text via a customView.
It is half Bold and half standard text. Whichever unit is selected will be bold.
After trying each method of refreshing them I would remove them after they didn't work. So in the code below there is no "reload/refresh" being attempted.
I have a boolean which changes when the action is called. However I cannot get the barButtomItem to "Reload/refresh" upon being selected.
I have debugged and the action is being called, the variable is changing, and the formatted text is being changed, however it is not changing on the screen.
//MARK: - Create Navigation Bar
func createNavBar() {
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.backgroundColor = .clear
navigationController?.navigationBar.isTranslucent = true
let CFLabel = UILabel()
CFLabel.attributedText = formatCFButton()
CFLabel.sizeToFit()
CFLabel.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(CFChanger))
CFLabel.addGestureRecognizer(tap)
let CFButton = UIBarButtonItem.init(customView: CFLabel)
let addCityButton = UIBarButtonItem.init(barButtonSystemItem: .add, target: self, action: #selector(addCity))
addCityButton.tintColor = .black
self.navigationItem.rightBarButtonItem = addCityButton
self.navigationItem.leftBarButtonItem = CFButton
}
#objc func CFChanger() {
print("CF BUTTON PRESSED AND FUNCTION ACTIVATED!")
isCelsius = !isCelsius
}
//Change font(bold) change temperature units button
func formatCFButton() -> NSMutableAttributedString {
let changeTempString = NSMutableAttributedString()
let boldAttribute = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Bold", size: 18.0)!]
let regularAttribute = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 18.0)!]
if isCelsius == true {
let boldText = NSAttributedString(string: "C ", attributes: boldAttribute)
let regularText = NSAttributedString(string: "/ F", attributes: regularAttribute)
changeTempString.append(boldText)
changeTempString.append(regularText)
} else {
let boldText = NSAttributedString(string: "F ", attributes: boldAttribute)
let regularText = NSAttributedString(string: "C / ", attributes: regularAttribute)
changeTempString.append(regularText)
changeTempString.append(boldText)
}
return changeTempString
}
I was able to get this to work in the past when I did NOT create the button programmatically by adding this code into the function when the button is pressed. I am not sure how to do this programmatically. (sender stuff)
// MARK: - Change units function
//****************************************************************************
#IBAction func changeUnitsButtonPressed(_ sender: UIButton) {
isCelsius = !isCelsius
var tempUnitNumber = 0
if isCelsius == true {
tempUnitNumber = 1
} else {
tempUnitNumber = 2
}
defaults.set(tempUnitNumber, forKey: "tempUnit")
let changeTempString = formatCFButton()
sender.setAttributedTitle(changeTempString, for: [])
tableView.reloadData()
}

Create a Login Page for SKSprite Game

I am in the process of creating a game (Swift) in xcode using a number of SKScene and Sprite objects. I want to create a Scene (settings scene) that captures the player's name, email, gender etc. How can I go about this? How can I capture input from user. SKScenes do not allow input fields/values in the UI?
Thanks
You can build a custom login page that is conform with your game layout without try to rebuild in UIKit the same graphic assets.
Few days ago I've written an answer about SKSceneDelegate to communicate between the scene(SpriteKit) and the viewController (UIKit), take present this answer if you want to call other viewControllers because its the same concept of this answer..
Starting with this GameViewController we can develop some useful methods to handle the login form buttons and show some alerts:
import UIKit
import SpriteKit
class GameViewController: UIViewController, TransitionDelegate {
override func viewDidLoad() {
super.viewDidLoad()
guard let view = self.view as! SKView? else { return }
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
let scene = GameScene(size:view.bounds.size)
scene.scaleMode = .fill
scene.delegate = self as TransitionDelegate
scene.anchorPoint = CGPoint.zero
view.presentScene(scene)
}
func showAlert(title:String,message:String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default) { action in
print("handle Ok action...")
})
alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(alertController, animated: true)
}
func handleLoginBtn(username:String,password:String) {
print("handleLoginBtn")
print("username is: \(username) and password: \(password)")
}
func handleFacebookBtn() {
print("handleFacebookBtn")
}
func handleTwitterBtn() {
print("handleTwitterBtn")
}
}
Then we can make our scene trying to take the advantage of SpriteKit elements:
import SpriteKit
import UIKit
protocol TransitionDelegate: SKSceneDelegate {
func showAlert(title:String,message:String)
func handleLoginBtn(username:String,password:String)
func handleFacebookBtn()
func handleTwitterBtn()
}
class GameScene: SKScene,UITextFieldDelegate {
var usernameTextField:UITextField!
var passwordTextField:UITextField!
var loginBtn:SKShapeNode!
var facebookBtn:SKShapeNode!
var twitterBtn:SKShapeNode!
override func didMove(to view: SKView) {
//bg
let bg = SKSpriteNode(imageNamed: "appleWallpaper")
addChild(bg)
bg.position = CGPoint(x:self.size.width/2,y:self.size.height/2)
//title
let title = SKLabelNode.init(fontNamed: "AppleSDGothicNeo-Bold")
title.text = "xyzGame"; title.fontSize = 25
title.fontColor = .orange
addChild(title)
title.zPosition = 1
title.position = CGPoint(x:self.size.width/2,y:self.size.height-80)
//textfields
guard let view = self.view else { return }
let originX = (view.frame.size.width - view.frame.size.width/1.5)/2
usernameTextField = UITextField(frame: CGRect.init(x: originX, y: view.frame.size.height/4.5, width: view.frame.size.width/1.5, height: 30))
customize(textField: usernameTextField, placeholder: "Enter your username")
view.addSubview(usernameTextField)
usernameTextField.addTarget(self, action:#selector(GameScene.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
passwordTextField = UITextField(frame: CGRect.init(x: originX, y: view.frame.size.height/4.5+60, width: view.frame.size.width/1.5, height: 30))
customize(textField: passwordTextField, placeholder: "Enter your password", isSecureTextEntry:true)
view.addSubview(passwordTextField)
//buttons
let myBlue = SKColor(colorLiteralRed: 59/255, green: 89/255, blue: 153/255, alpha: 1)
loginBtn = getButton(frame: CGRect(x:self.size.width/4,y:self.size.height/2,width:self.size.width/2,height:30),fillColor:myBlue,title:"Login",logo:nil,name:"loginBtn")
addChild(loginBtn)
loginBtn.zPosition = 1
let label = SKLabelNode.init(fontNamed: "AppleSDGothicNeo-Regular")
label.text = "or connect with"; label.fontSize = 15
label.fontColor = .gray
addChild(label)
label.zPosition = 1
label.position = CGPoint(x:self.size.width/2,y:self.size.height/2-30)
let logoFb = SKSpriteNode.init(imageNamed: "facebook-icon")
logoFb.setScale(0.5)
facebookBtn = getButton(frame: CGRect(x:self.size.width/4,y:self.size.height/2-80,width:self.size.width/4.5,height:30),fillColor:myBlue,logo:logoFb,name:"facebookBtn")
addChild(facebookBtn)
facebookBtn.zPosition = 1
let myCyan = SKColor(colorLiteralRed: 85/255, green: 172/255, blue: 239/255, alpha: 1)
let logoTw = SKSpriteNode.init(imageNamed: "twitter-icon")
logoTw.setScale(0.5)
twitterBtn = getButton(frame: CGRect(x:self.size.width/2,y:self.size.height/2-80,width:self.size.width/4.5,height:30),fillColor:myCyan,logo:logoTw,name:"twitterBtn")
addChild(twitterBtn)
twitterBtn.zPosition = 1
}
func customize(textField:UITextField, placeholder:String , isSecureTextEntry:Bool = false) {
let paddingView = UIView(frame:CGRect(x:0,y: 0,width: 10,height: 30))
textField.leftView = paddingView
textField.keyboardType = UIKeyboardType.emailAddress
textField.leftViewMode = UITextFieldViewMode.always
textField.attributedPlaceholder = NSAttributedString(string: placeholder,attributes: [NSForegroundColorAttributeName: UIColor.gray])
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.layer.borderColor = UIColor.gray.cgColor
textField.layer.borderWidth = 0.5
textField.layer.cornerRadius = 4.0
textField.textColor = .white
textField.isSecureTextEntry = isSecureTextEntry
textField.delegate = self
}
func getButton(frame:CGRect,fillColor:SKColor,title:String = "",logo:SKSpriteNode!,name:String)->SKShapeNode {
let btn = SKShapeNode(rect: frame, cornerRadius: 10)
btn.fillColor = fillColor
btn.strokeColor = fillColor
if let l = logo {
btn.addChild(l)
l.zPosition = 2
l.position = CGPoint(x:frame.origin.x+(frame.size.width/2),y:frame.origin.y+(frame.size.height/2))
l.name = name
}
if !title.isEmpty {
let label = SKLabelNode.init(fontNamed: "AppleSDGothicNeo-Regular")
label.text = title; label.fontSize = 15
label.fontColor = .white
btn.addChild(label)
label.zPosition = 3
label.position = CGPoint(x:frame.origin.x+(frame.size.width/2),y:frame.origin.y+(frame.size.height/4))
label.name = name
}
btn.name = name
return btn
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let positionInScene = touch!.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name {
switch name {
case "loginBtn":
self.run(SKAction.wait(forDuration: 0.1),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
(delegate as! TransitionDelegate).handleLoginBtn(username:self.usernameTextField.text!,password: self.passwordTextField.text!)
})
case "facebookBtn":
self.run(SKAction.wait(forDuration: 0.1),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
(delegate as! TransitionDelegate).handleFacebookBtn()
})
case "twitterBtn":
self.run(SKAction.wait(forDuration: 0.1),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
(delegate as! TransitionDelegate).handleTwitterBtn()
})
default:break
}
}
}
func textFieldDidChange(textField: UITextField) {
//print("everytime you type something this is fired..")
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == usernameTextField { // validate email syntax
let emailRegEx = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %#", emailRegEx)
let result = emailTest.evaluate(with: textField.text)
let title = "Alert title"
let message = result ? "This is a correct email" : "Wrong email syntax"
if !result {
self.run(SKAction.wait(forDuration: 0.01),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
(delegate as! TransitionDelegate).showAlert(title:title,message: message)
})
}
}
}
deinit {
print("\n THE SCENE \((type(of: self))) WAS REMOVED FROM MEMORY (DEINIT) \n")
}
}
Output:
Animated output:
As you can see we can handle both framework with their delegate methods, I've tested this page with iPhone 5 and iPhone 7 plus.