从 url 方案中获取文本字符串在 appdelegate 中有效,但在 viewcontroller 中无效

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

所以我有一个 url 方案,即 - appname//

当我在 safari 中访问一个响应为 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;
}

ViewController代码

viewdidload

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

joon 函数

       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 函数之后调用的,应该是相反的方式

在 App delegate 中像这样创建对象

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

将您的观察者代码更改为

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