【问题标题】:get string of text from url scheme works in appdelegate but not in viewcontroller从 url 方案获取文本字符串在 appdelegate 中有效,但在 viewcontroller 中无效
【发布时间】:2016-06-10 07:59:40
【问题描述】:

所以我有一个网址方案,即 - 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 代码

查看已加载

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

【问题讨论】:

    标签: ios json swift url url-scheme


    【解决方案1】:

    将您的观察者代码更改为

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

    【讨论】:

      【解决方案2】:

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

      在视图控制器中像这样使用应用委托对象

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多