在obj-C中,当另一个iOS应用程序(邮件附件、web链接)被与我的应用程序关联的文件或链接点击时.然后我会在openURL或didFinishLaunchingWithOptions
上捕捉到这一点,并显示一个UIAlertView
来确认用户想要导入数据.既然UIAlertView
贬值了,我也在try 做同样的事情,但不确定最好的方法是什么?
当我的应用程序从另一个应用程序接收数据时,我无法显示简单alert .以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;
}
但当我try 切换到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
中的代码类似于冷启动),添加或不添加新数据,取决于alert Select .
我可以标记一个全局变量,我在所有viewWillAppear
func中都签入了它,但这会有很多重复,因为我有30多个视图.
如果你有任何 idea ,请告诉我.
谢谢
格雷格