2016-06-10 43 views
1

所以我有一个URL方案,即 - appname//GET字符串工程的appdelegate但不是在视图 - 控制

,当我去到一个网站,有一个响应与appname//jsonurl狩猎它正确地抓住它在的appDelegate但是当我尝试使用它在我的应用程序中的变量是空的,因为我的应用程序的功能是儿子功能后,被称为

代码的appDelegate -

//url scheme 
var urlScheme: String!; 
var pathScheme: String!; 
var queryScheme: String!; 

var checkbool : Bool = false; 


func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool { 
    //get contents of url 
    urlScheme = url.scheme; 
    pathScheme = url.path; 
    queryScheme = url.query; 
    NSLog("URLLLL"); 
    //NSLog(urlScheme); 
    //NSLog(pathScheme); 
    //NSLog(queryScheme); 

    return true; 
} 

视图控制器代码

viewDidLoad中

NSNotificationCenter.defaultCenter().addObserver(self, selector: ("loadJSON:"), name: UIApplicationWillEnterForegroundNotification, object: nil); 

俊功能

 func loadJSON(notification: NSNotification){ 
      NSLog("app entered foreground"); 

      getValidJson(); 

      //play video using specific player 
      if(videoPlayer == 0){ 
       //call inlineVideo(); 
       inlineVideo(); 
       resizeScreen(); 
      }else{ 
       //call 360 video player 
       makeSphere(); 
       resizeScreen(); 
      } 

      createImageView(videoPlayer); 

      //add seeker 
      view.addSubview(timeRemainingLabel); 
      view.addSubview(seekSlider) 

      //add target listeners for the seeker 
      seekSlider.addTarget(self, action: "sliderBeganTracking:", forControlEvents: UIControlEvents.TouchDown); 
      seekSlider.addTarget(self, action: "sliderEndedTracking:", forControlEvents: [UIControlEvents.TouchUpInside, UIControlEvents.TouchUpOutside]); 

      self.view.backgroundColor = UIColor.blackColor(); 
     } 

?我怎样才能从我的url scheme的值,并在我的视图控制器,目前我的应用功能是loadJSON功能后,被称为使用它,它应该是相反的

回答

3

改变你的观察代码

NSNotificationCenter.defaultCenter().addObserver(self, selector: ("loadJSON:"), name: "customNotif", object: nil); 

,并致电通知在AppDelegate中openURL功能

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool { 
    //get contents of url 
    urlScheme = url.scheme; 
    pathScheme = url.path; 
    queryScheme = url.query; 
    NSLog("URLLLL"); 
    //NSLog(urlScheme); 
    //NSLog(pathScheme); 
    //NSLog(queryScheme); 

    NSNotificationCenter.defaultCenter().postNotificationName("customNotif", object: nil) 
    return true; 
} 
0

在应用程序委托创建对象像这样

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var urlScheme : String = ""; 
var pathScheme : String = ""; 
var queryScheme : String = ""; 

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    return true  
} 

在视图控制器使用的应用程序委托对象这样

pathScheme = String(appDelegate.pathScheme); 
queryScheme = String(appDelegate.queryScheme); 
相关问题