iOS - Open certain view controller with URL scheme

孤人 提交于 2019-12-07 07:53:28

问题


I'm playing around with URL schemes in my app. I easily made one to open my app, just adding the necessary items to info.plist. This current URL "myappname://" takes the user to the initial view controller, FirstTableViewController, but I was wondering if it would be possible to modify that URL scheme so it I can have one that takes the user to a certain view controller, such as ThirdTableViewController. I would use this as a handy feature in something like Launch Center.


回答1:


In ...AppDelegate.m

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
[self.viewController presentModalViewController:controller animated:YES];
[controller release];

return YES;        
}



回答2:


Try look at this: Custom Url Schemes

Hope this will be a useful




回答3:


This post is a little old but maybe useful for iOS 5 + because the checked answer is not correct.

AppDelegate doesn't have any navigationController property.

Instead you can do in AppDelegate.m :

enter code here

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    [navController presentModalViewController:controller animated:YES];

    return YES;        
}



回答4:


Hi here is my solution.

If you can call your navigation function that called in "application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool" event (with a delay) you can navigate a specific page in the app even is not running before called.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Actived keyboard avoider
        changeAppereance()
        delay(1) {
            deeplink = Deeplink()
            self.manageNavigation(launchOptions: launchOptions)
            self.navigate()
        }

        return true

    }


    private func manageNavigation(launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
        if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? URL { //Deeplink
            print(url.absoluteString)
            deeplink = Deeplink()
            deeplink?.url = url
        }
        else if let activityDictionary = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [AnyHashable: Any] { //Universal link
            for key in activityDictionary.keys {
                if let userActivity = activityDictionary[key] as? NSUserActivity {
                    if let url = userActivity.webpageURL {
                        deeplink = Deeplink()
                        deeplink?.url = url
                    }
                }
            }
        }

    }

open func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        print(url.absoluteString)
        deeplink = Deeplink()
        deeplink?.url = url
        navigate()
        return true
    }



回答5:


Yes it is possible to modify URL scheme so that you can jump user to any viewcontroller.I used and implement normal as well as https://hokolinks.com/ deep link.By hoko link deep linking you can modify your URL Scheme ,also you can send data with that URL.

Integrate iOS SDK Using Hoko Link - Add a URL Scheme to your App - SDK Setup To integrate HOKO open source SDK in your app (only iOS 5 and higher) you just have to follow 3 simple steps (either using cocoapods or doing it manually).

Using CocoaPods

1- Install CocoaPods in your system

2- Open your Xcode project folder and create a file called Podfile with the following content:

pod 'Hoko', '~> 2.3.0' 3- Run pod install and wait for CocoaPods to install HOKO SDK. From this moment on, instead of using .xcodeproj file, you should start using .xcworkspace.

Manual integration

1- Download the Hoko SDK.

2- Drag the Hoko folder to your project.

3- Be sure to also add SystemConfiguration.framework and zlib.dylib in case your project does not include it already.

Integrating the SDK with your Swift project

Because the HOKO SDK is written in Objective-C, you’ll have to manually add a Bridging Header file into your project in order to use it with your Swift code:

1- File > New > File... > iOS > Source > Header File

2- Name that header file YourAppName-Bridging-Header.h

3- Inside that header file, import #import

4- Go to your project > Build Settings > Search for Objective-C Bridging Header > Add the path to your bridging header file, from your root folder (e.g. MyApp/MyApp-Bridging-Header.h)

Add a URL Scheme to your App

Next, we need to define your app’s custom URL type, if you don’t have one already. Open your Xcode project settings and under the “Info” tab expand the “URL Types” section. You can skip this step if you already configured a URL type.

If this section is empty, click in the “+” icon to add a new URL type. Let’s say that we want to open the app via “hoko://”. Hence we need to enter “hoko” in URL Schemes.

We also should assign a unique Identifier to this URL type. Apple recommends that you use reverse DNS notation to ensure that there are no name collisions between types. In this example we are going to use “com.hoko.app”.

Take note of your URL Scheme because we will ask you for it, when you are creating an app through the dashboard, e.g. “hoko”.

URL Scheme

Setup Associated Domains (Universal Links) - iOS 9.0+

For your app to fully support the newly introduced Universal Links by Apple you’ll have to enable and add a new entry in the Associated Domains section, inside your application target’s Capabilities tab. Click on the ‘+’ button and add a new entry with the following value: applinks:myapp.hoko.link, being myapp the Hoko subdomain you chose for your app’s Hoko links. You can also have your own link domain (learn more about this on the subdomains section).

URL Scheme

SDK Setup

Add the following line to your applicationDidFinishLaunching method in your AppDelegate class (don’t forget to import the HOKO class by using #import if you’re working with Objective-C).

Objective-C Swift #import

- (BOOL)application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [Hoko setupWithToken:@"YOUR-APP-TOKEN"];
 // The rest of your code goes here...

  return YES;
}

If you are using a custom domain in your smart links, you must setup the iOS SDK using setupWithToken:customDomain: as following:

Objective-C Swift #import

- (BOOL)application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [Hoko setupWithToken:@"YOUR-APP-TOKEN"
     customDomain:@"your.custom.domain.com"];
  // The rest of your code goes here...

 return YES;

} NOTE: make sure to return YES in the application:didFinishLaunchingWithOptions: delegate method to allow incoming deep links that open your app to be processed. Returning NO will block the requests.

Setup your mobile deep linking by using Hoko Link SDK



来源:https://stackoverflow.com/questions/10925612/ios-open-certain-view-controller-with-url-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!