0

I am working with Xcode 6.0 and swift. UIAlertView and UIAlertController (example:following 2 functions) work fine on myViewController inherited from UIViewController, but they crash on KeyboardViewController inherited from UIInputViewController. Doesn't Apple allow alertview on custom keyboard or is there any mistake in my coding? Any responses are welcome and appreciated.

func viewAlert() {
    var alertView = UIAlertView() <———
    alertView.addButtonWithTitle("Ok")
    alertView.title = "title"
    alertView.message = "message"
    alertView.show()
}    
func viewAlert0() {
    var alert = UIAlertController() <———
    alert.title = "title"
    alert.message = "are disabled in your device"
    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}
 <——— debugger error point:

0x325ca19:  calll  0x327e620                 ; symbol stub for: pthread_kill
0x325ca1e:  movl   $0x2710, (%esp)
0x325ca25:  calll  0x327ec50                 ; symbol stub for: usleep$NOCANCEL
0x325ca2a:  movl   $0xffffffe7, -0xc(%ebp)
0x325ca31:  movl   %esi, 0x4(%esp)
0x325ca35:  movl   $0x0, 0x8(%esp)
0x325ca3d:  movl   $0x3, (%esp)
0x325ca44:  calll  0x327e476                 ; symbol stub for: sigprocmask
0x325ca49:  ud2    <====== Thread 1 :EXC_BAD_INSTRUCTION(code=EXC_i386_INVOP,subcode=0x0)
Pang
  • 9,564
  • 146
  • 81
  • 122
fred
  • 1
  • 1
  • `UIAlertView`s have been deprecated in iOS 8 so why are using them at all. – Popeye Aug 01 '14 at 11:42
  • Popyeye, thank you for response. more important is that UIAlertController -recommended in iOS 8 - is the same. The point is that they all work fine in UIViewController but not in UIInputviewController. – fred Aug 01 '14 at 13:58
  • In all honesty I haven't looked into swift that much so I'm not entirely sure. All I know really is that `UIAlertView`s have been deprecated in iOS8 for both objective-c and swift. If I see anything that might help I will come back and pass it onto you. – Popeye Aug 01 '14 at 14:05

2 Answers2

0

From iOS 8, we have new macro:

NS_EXTENSION_UNAVAILABLE_IOS

iOS doesn't permit to use some class in EXTENSION

iOS doesn't permit to use AlertView or AlertController if you are building an CUSTOM KEYBOARD EXTENSION

See more:

Tony
  • 4,311
  • 26
  • 49
0

In ther hope it can help someone in Swift:

suppose You have a lib that must work with iOS AND extensions (in may case I use UIApplication.sharedApplication().keyWindow that is unavailable to extensions)

#if NS_EXTENSION_UNAVAILABLE_IOS


func captureScreen() -> UIImage {

    let window = UIApplication.sharedApplication().keyWindow
    let scale = UIScreen.mainScreen().scale

    UIGraphicsBeginImageContextWithOptions(window!.bounds.size, false, scale)

    window?.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image : UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image
}


#else
#endif

using #ifdef allow us to compile this file both for iOS and extension.

ingconti
  • 10,876
  • 3
  • 61
  • 48