iOS应用间资源共享

app间共享(交互)方法:

1。iOS应用程序之间可以通过“URL Scheme”交互,调用[UIApplication openURL:URL]方法,使用“Scheme://openxxx?id=1”,类似于http的请求来传递简短数据。


2。应用间是可以访问其他app的Documents目录的。比如 /Users/Jiangcp/Library/Application Support/iPhone Simulator/7.0.3/Applications/应用A/Documents/ 。 在应用B里访问这个地址,是完全可以。(应用A代表系统分配的app唯一标识类似于99193050-39BA-496E-96AD- 7D02C17805F0)前提是需要知道这个唯一标识。(只能越狱机上进行,因为在非越狱机上每次重新编译或是安装应用之后“99193050-39BA-496E-96AD- 7D02C17805F0”这个标示都在一直更改,即使通过openURL方法传递过去也不行,沙箱限制
 因为在ios中对文件的操作主要都是使用NSFileManager对象,估计就是在这个类的底层对文件的访问进行了限制,而越狱之后就打破了这些底层的限制,因此在越狱机上,只要知道某个文件的路径都可以在app中进行访问,当然也不是所有的,大部分好像都可以,反正/var/mobile/Applications目录下全可以


3。app间可以通过KeyChain共享数据(keychain access group)。Project->build setting->Code Signing Entitlements 里选定制作好的keychain access文件。



4。利用系统接口UIDocumentInteractionController,可以选择预先注册进系统的应用来访问资源。有点类似于win下的打开方式里选定哪个应用打开


下面分析下,这几种方法的各种优缺点。


1。是系统通过在底层截获openURL的地址。如果存在已经注册进系统的,能响应该scheme的app,系统会自动唤起该app到前台。没有注册该 scheme的app,都以safari打开。优点:简单,无需做任何操作。缺点:能传输的数据少,且应用间交互必须先预先修改info.plist里的 URL types,不灵活。


2。应该是最全面,最优的应用间共享资源的方式了。缺点:共享的资源必须放在Documents或其子目录下,且交互的时候必须提前知道应用在系统里的identify.


3。缺点:能交互的应用必须是同一个开发者账号


4。缺点:在接受文件的应用A中需先注册支持的文件类型 在plist文件里设置如下:
       更多的设置及文件类型参考:http://blog.csdn.net/chengyingzhilian/article/details/8517193
             https://developer.apple.com/library/mac/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
     iOS应用间资源共享
    并在AppDelegate中实现:
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if (!url) {  return NO; }
    NSString *URLString = [url absoluteString];
    UIAlertView *v1 = [[UIAlertView alloc] initWithTitle:@"URLString" message:URLString delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [v1 show];

    NSString *ff = url.path;
    UIAlertView *v2 = [[UIAlertView alloc] initWithTitle:@"ff" message:ff delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [v2 show];
    if ([[NSFileManager defaultManager] fileExistsAtPath:ff]) {
        UIAlertView *v3 = [[UIAlertView alloc] initWithTitle:@"fileExistsAtPath" message:@"fileExistsAtPath" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [v3 show];
    }
    if ([[NSFileManager defaultManager] isReadableFileAtPath:ff]) {
        UIAlertView *v4 = [[UIAlertView alloc] initWithTitle:@"isReadableFileAtPath" message:@"isReadableFileAtPath" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [v4 show];
    }
    NSArray *arr = [NSArray arrayWithContentsOfFile:ff]; // 读取文件内容
    UIAlertView *v5 = [[UIAlertView alloc] initWithTitle:@"arrrrr" message:[arr description] delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [v5 show];
   
    return YES;
}
        在发起的应用B中需这样做:
- (IBAction)cccc:(id)sender {
    NSURL *url = [[NSBundle bundleWithPath:[NSString stringWithFormat:@"%@/Documents/",NSHomeDirectory()]] URLForResource:@"arr" withExtension:@"xml"];
    if (url) {
        _controller = [UIDocumentInteractionController  interactionControllerWithURL:url];
        _controller.delegate =self;
        [_controller presentPreviewAnimated:YES];
        [_controller presentOpenInMenuFromRect:[self.view frame] inView:self.view animated:YES];
    }
}

//UIDocumentInteractionController委托方法
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
    return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
    return  self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
   
    return self.view.frame;
}

     而且必须得经历如下两个的界面才能跳转到应用A中:
iOS应用间资源共享

iOS应用间资源共享


原文地址:https://www.cnblogs.com/cnsec/p/11515782.html