2015-11-19 59 views
0

嗨,我有我的應用程序我有添加3D觸控快速操作的問題,我的問題是,當我的應用程序是不是在多任務處理,然後在我的應用程序無法打開共享表已任何人一個想法如何解決這個問題,我的代碼問題只是在信息視圖和問題快速行動工作很好的共享快速行動。3D快速行動分享功能不工作時,在應用程序關閉

// AppDelegate.swift

進口的UIKit 進口MessageUI

@UIApplicationMain 類的AppDelegate:UIResponder,UIApplicationDelegate,MFMailComposeViewControllerDelegate {

var window: UIWindow? 

enum QuickActionType : String { 
    case viewControllerInfo = "com.example.infoView" 
    case share = "com.example.share" 
    case problem = "com.example.problem" 

} 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    var QuickAction = false 

    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem{ 
     QuickAction = true 
     handleQuickAction(shortcutItem) 
    } 

    return !QuickAction 

} 

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { 
    let handledQuickAction = handleQuickAction(shortcutItem) 
    completionHandler(handledQuickAction) 

} 

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
    if error != nil { 
     print(error?.localizedDescription) 
    } 

    self.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil) 

} 


func applicationWillResignActive(application: UIApplication) { 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 

func applicationDidEnterBackground(application: UIApplication) { 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 

func applicationWillEnterForeground(application: UIApplication) { 
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
} 

func applicationDidBecomeActive(application: UIApplication) { 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 

func applicationWillTerminate(application: UIApplication) { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool { 

    var handled = false 

    if let shortCutType = QuickActionType.init(rawValue: shortcutItem.type){ 
     let rootNavigationViewController = window!.rootViewController as? UINavigationController 
     let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController? 

     rootNavigationViewController?.popToRootViewControllerAnimated(false) 

     switch shortCutType { 

      // Quick Action Info 

     case .viewControllerInfo: 
      handled = true 
      rootViewController?.performSegueWithIdentifier("infoView", sender: nil) 


     case .share: 
      handled = true 

      let shareItems = ["hello world"] 

      let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil) 

      self.window?.rootViewController?.presentViewController(activityViewController, animated: true, completion: nil) 

      // Quick Action Problem 

     case .problem: 
      handled = true 

      let mailController = MFMailComposeViewController() 
      mailController.mailComposeDelegate = self 
      mailController.setToRecipients(["[email protected]"]) 
      mailController.setSubject("App Bug") 
      mailController.setMessageBody("(Your Problem) 

      self.window?.rootViewController?.presentViewController(mailController, animated: true, completion: nil) 

     } 
    } 

    return handled 

} 

}

+0

任何人都可以幫助我嗎? – chris188

回答

0

嘗試添加輕微在處理你的快速行動之前延遲,看看是否幫助。當我執行我的快速行動時,我發現他們不會在沒有很小的延遲的情況下100%可靠地工作。我目前使用0.5秒。你可以做這樣的事情(在OBJ-C):

[self.performSelector:@selector(doAction:) withObject:obj afterDelay:0.5]; 

或SWIFT:

self.performSelector("doAction:", withObject: obj, afterDelay: 0.5) 

這可能會或可能不會在你的情況有所幫助,但它在我沒有和我提起了關於它的雷達,我還沒有聽說過。

0

嘿,你需要顯示的ViewController在當前視圖控制器的頂部嘗試。例如,這是一個簡單的擴展在你的AppDelegate上添加這個。眼下

extension UIWindow { 

func visibleViewController() -> UIViewController? { 
    if let rootViewController: UIViewController = self.rootViewController { 
     return UIWindow.getVisibleViewControllerFrom(rootViewController) 
    } 
    return nil 
} 

class func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController { 

    if vc.isKindOfClass(UINavigationController.self) { 

     let navigationController = vc as! UINavigationController 
     return UIWindow.getVisibleViewControllerFrom(navigationController.visibleViewController!) 

    } else if vc.isKindOfClass(UITabBarController.self) { 

     let tabBarController = vc as! UITabBarController 
     return UIWindow.getVisibleViewControllerFrom(tabBarController.selectedViewController!) 

    } else { 

     if let presentedViewController = vc.presentedViewController { 

      return UIWindow.getVisibleViewControllerFrom(presentedViewController.presentedViewController!) 

     } else { 

      return vc; 
     } 
    } 
}} 

可以檢查可見的ViewController像

self.window?.visibleViewController()?.presentViewController(yourviewcontroller, animated: true, completion: nil) 

我希望這會幫助你。

+0

isKindOfClass?你是認真的嗎? :D這是迄今爲止我所見過的最類似objc的代碼。你從哪裏學到的? ;) – Ben

+0

是的,因爲你可以看到它的工作原理來檢查當前可見的視圖控制器 – BilalReffas

+0

也許,但代碼風格是可怕的 – Ben

相關問題