从url scheme获取文本字符串在appdelegate中工作但在viewcontroller中不起作用

时间:2016-06-10 07:59:40

标签: ios json swift url url-scheme

所以我有一个网址方案,即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函数之后调用的,它应该是相反的方式

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

在App委托创建对象中像这样

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    
}

在视图控制器中使用app delegate Object,就像这样

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