在obj-C中,當另一個iOS應用程序(郵件附件,Web鏈接)被與我的應用程序關聯的文件或鏈接點擊時。然後,我會在openURL或didFinishLaunchingWithOptions
上捕獲這個數據,並顯示UIAlertView
以確認用戶想要導入數據。現在UIAlertView
已貶值,我正在嘗試做同樣的事情,但不確定要做到這一點的最佳方式?Simple App Delegate方法顯示UIAlertController(在Swift中)
我無法在我的應用程序從另一個應用程序接收數據時顯示簡單的警報。此代碼在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
和斯威夫特我似乎無法找到一個簡單的方法來顯示消息:
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
顯示S上一個UIAlertViewController
tackOverflow,但我希望有一些簡單的東西。
我真正需要做的就是向用戶顯示一條快速消息,告訴他們他們獲得了一些數據,並讓他們決定他們想要做什麼。一旦完成,我的應用程序將繼續打開並進入前臺(類似於didFinishLaunchingWithOptions
中用於冷啓動的代碼),並根據警報選擇添加或不添加新數據。
我可以標記一個全局變量,我檢查了我所有的viewWillAppear
函數,但是由於我有30多個視圖,這會造成很多重複。
讓我知道你是否有任何想法。
感謝
格雷格
presentViewController是UIViewController中相關聯的方法,因此你不能運行的應用程序委託此方法。 – ZeMoon 2014-10-29 10:19:22
你所描述的將需要通知。你不能在應用程序之外向用戶顯示任何警報。 – ZeMoon 2014-10-29 10:20:49
您還會注意到,在調用openUrl委託方法時,應用程序已啓動。 – ZeMoon 2014-10-29 10:24:04