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:
- Open any link in safari browser
- 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.
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/
- See sender example
- 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
来源:https://stackoverflow.com/questions/14030823/sharing-a-link-opened-in-safari-in-ios-to-my-application