2016-06-10 4 views
1

so habe ich ein URL-Schema nämlich - appname//get Textzeichenfolge von URL-Schema funktioniert in AppDelegate aber nicht in Viewcontroller

und wenn ich auf eine Website in Safari gehen, die eine Antwort mit appname//jsonurl hat es fängt es richtig die AppDelegate aber wenn ich versuche, es in meiner App nutzen die Variablen leer sind, weil meine Anwendungsfunktion nach dem Sohn Funktion

Code AppDelegate genannt wird -

//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; 
} 

Viewcontroller Code

viewDidLoad

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

joon Funktion

 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(); 
     } 

wie kann ich die Werte aus meinem url scheme bekommen und es in meinen Ansicht-Controller verwenden, meine derzeitige Anwendungsfunktion nach der loadJson Funktion aufgerufen wird, und es sollte umgekehrt sein

Antwort

3

Änderung Ihrer Beobachter Code

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

und die Benachrichtigung in der Funktion AppDelegate openURL nennen

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

In App-Delegat erstellen Objekt So

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  
} 

In-View-Controller Verwenden AppDelegate Objekt wie dieses

pathScheme = String(appDelegate.pathScheme); 
queryScheme = String(appDelegate.queryScheme); 
Verwandte Themen