sharing a link opened in safari in ios to my application?

五迷三道 提交于 2019-12-01 11:03:59
Mohamed Elkassas

Starting from iOS 8 Using Share App Extension can achieve this task

I managed to do it by creating a Share extension app (File->New->Target->Application Extension)

Then from Targets->Info I specified support for web URLs

Then i spent time searching for a way to use original app files in the app extension and this post helped me a lot Sharing code between original iOS App and App Extension

If you are using cocoapods this can help you too https://stackoverflow.com/a/31989172/3033056 but be aware that not all pods are allowed to be used in app extensions

This is a link for a tutorial http://www.appcoda.com/ios8-share-extension-swift/

This is indeed possible using URL Schemes http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-working-with-url-schemes/

  1. See sender example
  2. See receiver example

iOS custom url scheme:

Example for html:

<p>Run the app<a href='BundleURLSchemes://BundleURLName?param=1'>iPhone/iPad</a></p>

CFBundleURLSchemes - An array of strings containing the URL scheme names—for example, http, mailto, tel, and sms.

BundleURLName - A string containing the abstract name of the URL scheme. To ensure uniqueness, it is recommended that you specify a reverse-DNS style of identifier, for example, com.acme.myscheme. The string you specify is also used as a key in your app’s InfoPlist.strings file. The value of the key is the human-readable scheme name.

via Apple's documentation: Implementing Custom URL Schemes

If your app can receive specially formatted URLs, you should register the corresponding URL schemes with the system. A customURL scheme is a mechanism through which third-party apps can communicate with each other. Apps often use custom URLschemes to vend services to other apps. For example, the Maps app supports URLs for displaying specific map locations.

Registering Custom URL Schemes

To register a URL type for your app, include the CFBundleURLTypes key in your app’s Info.plist file. The CFBundleURLTypes key contains an array of dictionaries, each of which defines a URL scheme the app supports.

In your AppDelegate:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

    NSString *query = [url query];

//    NSLog(@"query->%@",query);//param=1
//    NSLog(@"host->%@",host);//BundleURLName
//    NSLog(@"resourceSpecifier->%@",resourceSpecifier);//BundleURLName?param=1

    if(![[url scheme] isEqualToString:@"BundleURLSchemes"])
        return NO;
    else{
        NSArray* paramsData =[query componentsSeparatedByString:@"="];

        NSLog(@"param->%@", paramsData[1]);
    }

     return YES;

}

You can easily pass data to your app from safari by query inside app URL. i.e. Your application URL is scheme://host/path?Query where query is data that you can customize and parse. This URL is passed to application:handleOpenURL. See more about it here http://www.idev101.com/code/Objective-C/custom_url_schemes.html

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