Several jump situations between IOS applications _ios

Source: Internet
Author: User

Objective

In the process of developing iOS, we often encounter scenarios such as the need to jump from one application A to another B. This requires us to have knowledge of the jump between iOS applications. Below we are commonly used in several jump situations to introduce.

One, jump to another program's main interface

Each program should have a corresponding scheme to determine the corresponding URL


To jump to (open) another program, one program needs to add another program's scheme to its own application whitelist (configured in Info.plist: lsapplicationqueriesschemes, type array, Add the appropriate scheme in the array->ios9.0 start


Jump Code

Extension Viewcontroller {

 @IBAction func Jumptoxinwen (sender:anyobject) {
  OpenURL ("xinwen://")

 }
 Private func OpenURL (urlstring:string) {let
  url = nsurl (string:urlstring)!
  If Uiapplication.sharedapplication (). Canopenurl (URL) {
   uiapplication.sharedapplication (). OpenURL (URL)
  }

 }
}

Second, jump to another program's specified interface

Complete the corresponding settings for jumps between the above programs

Implement jump code (and jump to the home page, the URL is more than the parameters,?) is the target program want to jump the interface of the Segu tag,? The scheme for the current program.

 MARK:-Jump to the micro-trust Circle
 @IBAction func jumptowechattimeline (sender:anyobject) {
  OpenURL ("Wechat://timeline?xinwen ")

 }
 //MARK:-Jump micro-trust
 @IBAction func jumptowechatsession (sender:anyobject) {
  OpenURL (" wechat:// Session?xinwen ")

 }
 private func OpenURL (urlstring:string) {let
  url = nsurl (string:urlstring)!
  If Uiapplication.sharedapplication (). Canopenurl (URL) {
   uiapplication.sharedapplication (). OpenURL (URL)
  }

In the target program Appdelegate listen to the corresponding information used to jump, according to this information let the target program itself to achieve page switching

Extension Appdelegate {
 //listen for the current program to be opened by another program through what URL
 func application (application:uiapplication, OpenURL URL: Nsurl, sourceapplication:string, annotation:anyobject)-> Bool {
  //according to URL jump corresponding page
  //1.url convert to String
  let URLString = url.absolutestring
  //2. Get home Controller let
  ROOTVC = Application.keywindow? Rootviewcontroller let
  mainvc = ROOTVC? Childviewcontrollers[0] As! Viewcontroller
   //Send URL to mianvc
  mainvc.urlstring = urlstring
  //3. Complete the corresponding jumps according to the string content.
  Urlstring.containsstring ("session") {//Jump buddy
   Mainvc.performseguewithidentifier ("session", Sender:nil)
  } else if urlstring.containsstring ("TimeLine") {//Jump circle
   of Friends Mainvc.performseguewithidentifier ("TimeLine", Sender: Nil)
  } return
  true
 }
}

Third, how to from the target program's non-homepage interface back to the current (jump before) program?

thinking: as long as the target program's non-homepage interface to know the program before the jump URL can jump directly, so, here is the key is how to jump the program before the URL to the target program's non-homepage interface.

In the target controller appdelegate, the URL is passed to MIANVC (which defines the property that receives the data), as shown in the previous code, to obtain the URL information for the jump.

To pass a URL to a controller that needs to be switched in MIANVC (previously defined properties for receiving data)

 To switch interfaces, you need to come to this method. Able to get the switch before and after the controller
 override func Prepareforsegue (Segue:uistoryboardsegue, sender:anyobject?) {

  if Segue.identifier = = ' Session ' {let
   SESSIONVC = Segue.destinationviewcontroller as! Sessionviewcontroller
   //pass data
   sessionvc.urlstring = urlstring
  }
 }
}

In the target controller, according to the URL information, get the scheme of the controller before the jump, thus get the URL to jump back.

Class Sessionviewcontroller:uiviewcontroller {

 //Receive data
 var urlstring = ""
 override func Viewdidload () {
  super.viewdidload ()

  Navigationitem.leftbarbuttonitem = Uibarbuttonitem (title: "Apply before return jump", style:. Plain, target:self, Action: #selector (Backtostartapp))

 }

}
extension Sessionviewcontroller {
 func Backtostartapp () {
  //split URL, get program for jump before scheme let

  scheme = urlstring.componentsseparatedbystring ("?") [1]
  Print (Scheme)
  //stitching string let
  backstring = "\ Scheme://"
  //Open URL
  openurl (backstring)
 }


 private func OpenURL (urlstring:string) {let
  url = nsurl (string:urlstring)!
  If Uiapplication.sharedapplication (). Canopenurl (URL) {
   uiapplication.sharedapplication (). OpenURL (URL)
  }

 }

}

Summarize

This is all about the jump between iOS applications, I hope you can help the iOS developers, if you have any questions you can exchange messages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.