Ios 显示UIAlertController的简单应用程序委托方法(在Swift中)

Ios 显示UIAlertController的简单应用程序委托方法(在Swift中),ios,objective-c,swift,uialertview,Ios,Objective C,Swift,Uialertview,在obj-C中,当另一个iOS应用程序(邮件附件、web链接)被与我的应用程序关联的文件或链接点击时。然后,我会在openURL或didFinishLaunchingWithOptions上捕捉到这一点,并显示UIAlertView以确认用户想要导入数据。既然UIAlertView被贬低了,我也在尝试做同样的事情,但不确定最好的方法是什么 当我的应用程序从另一个应用程序接收数据时,我无法显示简单警报。此代码在Objective-C中使用UIAlertView运行良好: - (BOOL)appli

在obj-C中,当另一个iOS应用程序(邮件附件、web链接)被与我的应用程序关联的文件或链接点击时。然后,我会在openURL或
didFinishLaunchingWithOptions
上捕捉到这一点,并显示
UIAlertView
以确认用户想要导入数据。既然
UIAlertView
被贬低了,我也在尝试做同样的事情,但不确定最好的方法是什么

当我的应用程序从另一个应用程序接收数据时,我无法显示简单警报。此代码在Objective-C中使用
UIAlertView
运行良好:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if (url)
    {
        self.URLString = [url absoluteString];
        NSString *message = @"Received a data exchange request. Would you like to import it?";
        importAlert = [[UIAlertView alloc] initWithTitle:@"Data Received" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
        [importAlert show];
    }

    return YES;
}
但当我尝试切换到
UIAlertViewController
和Swift时,我似乎找不到一种简单的方式来显示消息:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    let URLString: String = url.absoluteString!
    let message: String = "Received data. Would you like to import it?"

    var importAlert: UIAlertController = UIAlertController(title: "Data Received", message: message, preferredStyle: UIAlertControllerStyle.Alert)
    importAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
    importAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler:
    { action in
        switch action.style {
        case .Default:
            println("default")  
        case .Cancel:
            println("cancel")   
        case .Destructive:
            println("destructive")
        }
    }))

    self.presentViewController(importAlert, animated: true, completion: nil)
    return true
}
我得到一个编译时错误,
AppDelegate
没有名为
presentViewController

我见过一些复杂的方法,可以让
AppDelegate
在StackOverflow上显示
UIAlertViewController
,但我希望有更简单的方法

我真正需要做的就是向用户显示一条快速消息,说明他们获得了一些数据,并让他们决定如何处理这些数据。完成后,我的应用程序将继续打开并进入前台(类似于“冷启动选项”中的“didFinishLaunchingWithOptions”中的代码),添加新数据或不基于警报选择

我可以标记一个全局变量,我签入的所有
视图都将出现
func,但这会有很多重复,因为我有30多个视图

如果你有任何想法,请告诉我

谢谢


Greg

来自应用程序代理内部

window.rootViweController.presentViewController..

尝试使用

self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil)
您只需要一个
viewController
对象来显示AlertController

在Swift 4中:

self.window?.rootViewController?.present(importAlert, animated: true, completion: nil)

使用此代码从appdelegate启动alertCV

    dispatch_async(dispatch_get_main_queue(), {
          let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet)
        self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil)
    })

希望这有帮助

我找到的最好的方法是:

        let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet) //.Alert .ActionSheet
        var hostVC = UIApplication.sharedApplication().keyWindow?.rootViewController
        while let next = hostVC?.presentedViewController {
            hostVC = next
        }
        hostVC?.presentViewController(importantAlert, animated: true, completion: nil)


        let delay = 1.5 * Double(NSEC_PER_SEC)
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(time, dispatch_get_main_queue()) {

            importantAlert.dismissViewControllerAnimated(true, completion: nil)

            return
        }
我已经添加了一个计时器,因此UIAlertController被解除,因为它没有任何按钮


感谢:关于如何从AppDelegate演示UIAlertController的精彩答案

Swift 3中的公认答案,以防对任何人有帮助:

self.window?.rootViewController?.present(importAlert, animated: true, completion: nil)

presentViewController是与UIViewController关联的方法,因此您无法在应用程序委托中运行此方法。您所描述的内容需要通知。您无法向应用程序之外的用户显示任何警报。您还将注意到,在调用openUrl委托方法时,应用程序已启动。在obj-C中,当另一个iOS应用程序(邮件附件、web链接)被点击时,该应用程序带有与我的应用程序关联的文件或链接。然后我会在openURL或didFinishLaunchingWithOptions上捕捉到这一点,并显示UIAlertView以确认用户想要导入数据。现在UIAlertView被贬值了,我也在尝试做同样的事情,但不确定最好的方法是什么?有什么想法吗?更新了问题的介绍,以澄清伊萨哈。。。我们每隔2秒发布相同的解决方案!多好的回答啊,但愿我能想到它谢谢,它的工作原理和我需要的完全一样,唯一的mod是:self.window?.rootViewController?.presentViewController(importAlert,动画:true,完成:nil)很好。如果在ObjC中执行此操作,则代码为--[self.window.rootViewController presentViewController:alert animated:true completion:nil];那么,如何解除警报?我尝试了这个var-alert2=UIAlertController(标题:“”,消息:“”,首选样式:UIAlertControllerStyle.Alert)alert2.addAction(UIAlertAction(标题:“确定”,样式:。默认,处理程序:{action in self.window?.rootViewController?.dismissViewControllerAnimated(true,完成:nil)})self.window?.rootViewController?.presentViewController(alert2,动画:true,完成:nil)但出现错误“找不到成员'Default'”。建议的解决方案仅在未启用数据恢复时有效。当数据恢复处于活动状态时,应用程序将向右跳转到用户离开它的位置,并且消息不会出现,将记录以下内容:“警告:尝试在其视图不在窗口层次结构中的位置显示!”。关于如何处理这种情况,请参见下面玛丽·阿米达的答案。最好的答案在这里,伊姆霍。原因:警告:
尝试显示其视图不在窗口层次结构中的对象=>太快了,所以
dispatch\u async(dispatch\u get\u main\u queue()…
帮助我们在正确的时间和正确的地点!在我的情况下,我没有收到任何关于窗口层次结构的消息…但我的viewController坚持不显示。谢谢,伙计们。这种方法很有效。但是,只要有
self.window?.rootViewController?.presentViewController(importAlert,动画:true,完成:nil)
didfishlaunchingwithoptions中的
方法对我不起作用,可能是因为调用视图控制器时未创建该视图控制器?在目标c中如何执行此操作?