Swift(iOS 9 Xcode7) 一个app打开另一个app内部的指定页面

两个app: Test 和 BasicGrammar

目的:从Test app 中打开 BasicGrammar app内指定的某个页面,并传参数过去:


BasicGrammar:

Swift(iOS 9 Xcode7) 一个app打开另一个app内部的指定页面_第1张图片
配置info.plist
Swift(iOS 9 Xcode7) 一个app打开另一个app内部的指定页面_第2张图片
AppDelegate
 func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
        
        //这里进行判断是哪一个app在打开此app,然后分别进行操作
        let scheme = url.scheme
        //不分大小写比较
        if scheme.caseInsensitiveCompare("OpenAppTest") == .OrderedSame{
        
            //执行跳转,跳转到你想要的页面
            let alert = UIAlertView(title: "\(scheme)", message: "\(url)", delegate: self, cancelButtonTitle: "确认")//iOS, introduced=2.0, deprecated=9.0
            alert.show()
            
            let vc = NextViewController()
            if let navVC = self.window?.rootViewController as? UINavigationController{
                navVC.pushViewController(vc, animated: true)
            }
            
        
            return true
            
        }
        
        return true
    }


Test:

Swift(iOS 9 Xcode7) 一个app打开另一个app内部的指定页面_第3张图片
记得设置info.plist里面的LSApplicationQueriesSchemes,iOS9之后需要,iOS9之后提高了app的安全性,需要给出一个类似白名单的东西,在白名单里面的才能打开app。不然报错: -canOpenURL: failed for URL: "OpenAppTest://mark?id=007" - error: "This app is not allowed to query for scheme OpenAppTest"
func openAppButton(){
    
        let button = UIButton()
        self.view.addSubview(button)//(必须要先把button加进来,才可以用去写它的布局)erminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'couldn't find a common superview for > and >'*** First throw call stack:
        button.mas_makeConstraints { (make) -> Void in
            make.left.equalTo()(self.view).offset()(20)
            make.right.equalTo()(self.view).offset()(-20)
            make.centerY.equalTo()(self.view)
            make.height.equalTo()(40)
        }
        
        button.setTitle("打开BasicGrammar", forState: .Normal)
        button.setTitleColor(UIColor.cyanColor(), forState: .Normal)
        button.setTitleColor(UIColor.greenColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        
        //设置button边框
        button.layer.borderColor = UIColor.greenColor().CGColor
        button.layer.borderWidth = 2
        button.layer.cornerRadius = 10
        //button.layer.masksToBounds = true
        
        button.addTarget(self, action: "openApp", forControlEvents: .TouchUpInside)//给button添加action
       
    
    }
func openApp(){
    
        
        //记得设置info.plist里面的LSApplicationQueriesSchemes,iOS9之后需要,iOS9之后提高了app的安全性,需要给出一个类似白名单的东西,在白名单里面的才能打开app。不然报错: -canOpenURL: failed for URL: "OpenAppTest://mark?id=007" - error: "This app is not allowed to query for scheme OpenAppTest"
        //OpenAppTest://mark?id=xxxx   (调用BasicGrammar app 拼接参数字符串,拼接的时候就像url那样子  OpenAppTest://标记名字?name=xiaomin&age=23)
        let urlStr = "OpenAppTest://mark?id=" + "007"
        let customUrl = NSURL(string: urlStr)
        
        if UIApplication.sharedApplication().canOpenURL(customUrl!) {
            
            UIApplication.sharedApplication().openURL(customUrl!)
            
        }else{
            
            //提示没有安装 BasicGrammar app
            
        }
    
    }
    

Test app

Swift(iOS 9 Xcode7) 一个app打开另一个app内部的指定页面_第4张图片
Test app

BasicGrammar app

Swift(iOS 9 Xcode7) 一个app打开另一个app内部的指定页面_第5张图片
BasicGrammar app

你可能感兴趣的:(Swift(iOS 9 Xcode7) 一个app打开另一个app内部的指定页面)