4

I need help about this problem.

I have a functionality in my application to share links with other users, what I need to do is:

  1. Open any link in safari browser
  2. Directly some action on safari browser to share that link in my application.

Is this possible in iOS to share a link from browser to the app directly.

Need help about this.

Regards.

Barbara R
  • 1,057
  • 5
  • 18
Badr
  • 10,384
  • 15
  • 70
  • 104
  • how can i use this method to get links from safari and how to add an option to safari to share a link with my application? – Badr Dec 25 '12 at 13:34
  • Why don't just create you own webview in a viewController in your app, and open the link in it than share it, without leaving your app? – Mongi Zaidi Apr 25 '14 at 09:32
  • Using Share Extension starting from iOS 8 and this is a link to a tutorial http://www.appcoda.com/ios8-share-extension-swift/ and you will be willing to take a look at this link for sharing data between original app and app extension http://stackoverflow.com/questions/28797242/sharing-code-between-original-ios-app-and-app-extension – Mohamed Elkassas Jun 26 '16 at 09:00

4 Answers4

4

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)

enter image description here

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

enter image description here

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/

Community
  • 1
  • 1
Mohamed Elkassas
  • 829
  • 1
  • 13
  • 33
1

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
Stavash
  • 14,244
  • 5
  • 52
  • 80
  • 2
    how can i get data from safari with this method? and how i can add an option in safari to share the link with my application? – Badr Dec 25 '12 at 13:15
1

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;

}
Sergey Neskoromny
  • 1,188
  • 8
  • 15
  • Sergey, thanks alot for the very descriptive answer. Can I share any URL I browse on Safari this way? Say I see a good blog post, can I share it into my application from Safari this way? I think custom url's isn't the way to do it :-( – Ravindranath Akila Apr 26 '14 at 08:43
  • @RavindranathAkila There is no way to do it on iOS with safari (non-jailbreak). To do this you would need custom safari plugin to place button to share with redirect to your app, But like you know i think, there is no support for plugins in safari. On a the other hand to achieve something similar you can build your own browser, than you can add custom script to each website and place share button, but i know this is not exactly what you are looking for. – Łukasz Tomaszewski Apr 26 '14 at 11:08
  • So correct. Just that I am not fully aware of the APIs to conclude this. Seems there is no way of sharing URLs between applications without custom implementation on BOTH applications in concern. In android as you may know a share will bring up all the applications which are eligible for URL reception. Thanks a lot for clarifying. This is what I want. I want a URL to be shared into my app. – Ravindranath Akila Apr 27 '14 at 11:09
  • There is only one way to do what you want - surfing inside your own app. You will always notified from UIWebView where is you are (URL of website that you surfing), so you can do with URL's is what you want. – Sergey Neskoromny Apr 27 '14 at 12:26
0

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

  • 1
    Thanks Lukasz, but I want to share any url into my app. Say http:www.facebook.com/someprofile was being browsed by me on Safari, now how can I share this link into my application? In android sharing into other applications is easily done but in iOS I am clueless how it is done. – Ravindranath Akila Apr 26 '14 at 08:42
  • So I don't understand what you are planning to do. Do you want to track urls opened in safari and base on that open your app automatically (without direct user action)? – Łukasz Tomaszewski Apr 26 '14 at 10:54