Gerwin Gerwin - 2 months ago 12
Swift Question

get string of text from url scheme works in appdelegate but not in viewcontroller

so I have a url scheme namely -

appname//


and when I go to a website in safari that has a response with
appname//jsonurl
it catches it correctly in the appDelegate but when I try to use it in my app the variables are empty because my application function is being called after the son function

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


ViewController code

viewdidload

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


joon function

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


how can I get the values from my
url scheme
and use it in my view controller, currently my application function is being called after the loadJSON function, and it should be the other way around

Answer

change your observer code to

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

and call the notification in the AppDelegate openURL function

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