Calling .presenttoViewController in SKScene in Swift - ios

I'm making a game with Sprite Kit in Swift. When you die in the game it presents the option to share your score on Twitter, Facebook, etc. But since I'm in a SKScene, it doesn't have the member .presentViewController . I need to present the Twitter compose field, is there any way to do this?
Here's the code:
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
let tweetviewcontroller = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetviewcontroller.setInitialText("I Scored \(score) on Cube Falling")
self.presentViewController(tweetviewcontroller, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to your Twitter account", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Settings", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) in
let settingsURL = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsURL {
UIApplication.sharedApplication().openURL(url)
}
}))
self.presentViewController(alert, animated: true, completion: nil)
}
Here's the error: image of code
Thanks

Related

Use of unresolved identifier 'NSAlert' Swift 4

Can't I use NSAlert in a SKScene ?
let alert = NSAlert()
alert.addButtonWithTitle("Close")
alert.messageText = "Game over"
alert.informativeText = "You won!"
alert.runModal()
ERROR : unresolved identifier 'NSAlert' Swift 4
NSAlert is an OS X API. Since you are on iOS, use UIAlertController:
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { _ in print("You clicked OK") }
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in print("You clicked Cancel") }
alert.addAction(okAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
If you are inside an SKView, try this:
if let vc = self.window?.rootViewController {
vc.present(alert, animated: true, completion: nil)
}

Adding a share action to a UIAlertController in Swift

I am building an app with a UIAlertController, and I want to have a button that says "Share" and then opens up something like
it seems easy to do, if it wasn't a sub-button.
My Code:
}
#IBAction func buttonTapped(sender: UIButton) {
var title: String
if sender.tag == correctAnswer {
title = "Correct"
++score
} else {
title = "Wrong"
let ac = UIAlertController(title: title, message: "Your score was \(score).", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "Try Again", style: .Default, handler: askQuestion))
ac.addAction(UIAlertAction(title: "Share", style: .Default, handler: share))
presentViewController(ac, animated: true, completion: nil)
gameOver = true
score = 0
}
My share function is currently not defined.
You can define in your completion handler to open an UIActivityController after the UIAlertAction is tapped, nevertheless by default Apple in iPhone not show the share as you want, in iPad yes. With the following code you can show the share after the button is tapped:
let message = "The message"
let alertController = UIAlertController(title: "The title", message: message, preferredStyle: .Alert)
let shareAction = UIAlertAction(title: "Share", style: .Default, handler: { x -> Void in
let firstActivityItem = "Text you want"
let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")!
// If you want to put an image
let image : UIImage = UIImage(named: "image.jpg")!
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)
// Anything you want to exclude
activityViewController.excludedActivityTypes = [
UIActivityTypePostToWeibo,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo
]
self.presentViewController(activityViewController, animated: true, completion: nil)
})
alertController.addAction(shareAction)
self.presentViewController(alertController, animated: true, completion: nil)
In the above code I summarize the add more buttons, and I assume that you want the code for iPhone, if you want it too for iPad you can check my answers in this questions:
How do i add this sharing button? IOS8 with swift
Sharing Extensions in IOS Apps
I hope this help you.
Do something like this, if your share method doesn't work
ac.addAction(UIAlertAction(title: "Share", style: .Default, handler: { (action) -> Void in
// Do the Facebook sharing here
let content = FBSDKSharePhotoContent()
content.photo = ....
....
}))
Here is an answer:
I defined the share class as:
func share(action: UIAlertAction!){
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
let facebook:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebook.setInitialText("I just scored \(score) in Flagpoles.")
self.presentViewController(facebook, animated: true, completion: nil)
score = 0
} 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.presentViewController(alert, animated: true, completion: nil)
}

Adding a action to a UIAlert that takes the user to settings

I have a UIAlert that notifies the user that they do not have an internet connection and that they need one in order to use the app. As well as letting them dismiss the alert by tapping the ok action I also want to have a action that when tapped takes the user to the settings app.
func displayAlert(title: String, message: String){
var formEmpty = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
formEmpty.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
})))
Use this code . May be help it.
override func viewDidAppear(animated: Bool) {
var alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .Alert)
var settingsAction = UIAlertAction(title: "Settings", style: .Default) { (_) -> Void in
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.sharedApplication().openURL(url)
}
}
var cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
alertController.addAction(settingsAction)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil);
}
Please note UIApplicationOpenSettingsURLString is only available on iOS8.0 and after so if your app should support iOS7 you'll have to check for availability of the constant (or if using Swift 2.0 use the #availability keyword).
You can navigate to the setting with this code:
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(settingsUrl!)
After adding this code in your function your function will look like:
func displayAlert(title: String, message: String){
var formEmpty = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
formEmpty.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
//This will call when you press ok in your alertview
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(settingsUrl!)
})))
}
For iOS 10, Swift 3:
let alert = UIAlertController(title: "Alert!", message: "your message here", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.shared.open(settingsUrl as! URL, options: [:], completionHandler: nil)
alert.addAction(settingsAction)
present(alert, animated: true, completion: nil)

UIActionSheet crashes on iPad swift

I have the following button action in a toolbar:
#IBAction func share(sender: AnyObject) {
let modifiedURL1 = "http://www.declassifiedandratified.com/search.html?q=\(self.searchBar.text)"
let modifiedURL = modifiedURL1.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: nil)
let alert = UIAlertController(title: "Share", message: "Share your findings", preferredStyle: UIAlertControllerStyle.ActionSheet)
let twBtn = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default) { (alert) -> Void in
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
var twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
twitterSheet.setInitialText("Look what I found on Declassified and Ratified: \(modifiedURL)")
self.presentViewController(twitterSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
let fbBtn = UIAlertAction(title: "Facebook", style: UIAlertActionStyle.Default) { (alert) -> Void in
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("Look what I found on Declassified and Ratified: \(modifiedURL)")
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)
}
}
let safariBtn = UIAlertAction(title: "Open in Safari", style: UIAlertActionStyle.Default) { (alert) -> Void in
let URL = NSURL(string: modifiedURL)
UIApplication.sharedApplication().openURL(URL!)
}
let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alert) -> Void in
println("Cancel Pressed")
}
let textBtn = UIAlertAction(title: "Message", style: UIAlertActionStyle.Default) { (alert) -> Void in
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "Look what I found on Declassified and Ratified: \(modifiedURL)"
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
}
alert.addAction(twBtn)
alert.addAction(fbBtn)
alert.addAction(safariBtn)
alert.addAction(textBtn)
alert.addAction(cancelButton)
self.presentViewController(alert, animated: true, completion: nil)
}
However, this code crashes when called on an iPad with a sigbart crash (that is all I can see in the console). I see this is a common problem but the other solutions have not worked for me. I even set the version to the latest iOS and that didn't fix it. Can someone explain?
On an iPad, an action sheet is a popover. Therefore you must give its UIPopoverPresentationController a sourceView and sourceRect, or barButtonItem, so that it has something to attach its arrow to.

Link OK button to settings panel to add an account

I've got a picture that can be shared through Facebook and Twitter.
If Facebook or Twitter isn't connected with your phone, a UIAlertAction pops up saying you have to connect.
When I click the OK button, I would want to navigate to the settings panel from the iPhone itself and once done it should go back to the app.
How can I do this?
This is the code I have once there has been clicked on the Twitter button to share the picture.
func twitterClicked( sender:UIBarButtonItem ){
if ( SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter)) {
let twitterVC = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
presentViewController(twitterVC, animated: true, completion: {() -> Void in})
twitterVC.setInitialText("Picture shared through code made in Native Development")
twitterVC.addImage(theView.imageView.image)
twitterVC.addURL(NSURL(string: "http://www.devine.be"))
} else {
println("connecteer je twitter")
let alert = UIAlertController(title: "Connect", message: "Connect your twitter", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
println("Ok geklikt")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in
println("Cancel geklikt")
}
alert.addAction(okAction)
alert.addAction(cancelAction)
}
Here is how you can invoke opening Settings:
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
let url = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(url!)
}
After that the user will have to manually return to the application.

Resources