ios 收藏app内容或网页到手机桌面

来源:互联网 发布:手机知乎怎么回答问题 编辑:程序博客网 时间:2024/04/29 17:31

最近看了一篇《保存app内容到手机桌面》感觉挺不错,于是自己参照着也做了一个demo。在此主要记录一些编写demo中遇到的问题。

先感谢下《保存app内容到手机桌面》的原作者,另再附上链接:http://pingyourid.github.io/AppWebClip/


首先,我们需要先生成一个webClip,

安装iphone configuration utility后打开,界面是这样的:


点击Configuration Profiles后点击左上角的New,创建一个Configuration Profiles:


接下来,需要配置这个新创建的profiles:

----先配置General:

Name:描述文件的名字,在通用-描述文件中显示的名字。

Identifier:身份标识,可以使用*或com.........。

Organization:组织名称,显示在Name下面的灰色字符串。

Description:描述,点击描述文件,进入详情页面时显示的描述字符串。

Security:安全配置,通过配置它来确定是否可以移除这个描述文件,有3种方式:Always(安装后可以随便移除),With Authorization(安装后需要输入配置的密码才能移除),Never(永远不能移除)。


----接下来需要配置Web Clips:

Label:Web Clip显示的名字,也就是在手机桌面显示的名字。

URL:点击桌面的这个Web Clip打开的URL,这里可以指向一个网址(http://www.baidu.com/),也可以指向你的应用程序(demo://test?a=5&b=3   详细操作下面会介绍)。

Removable:是否允许在手机桌面长安删除这个Web Clip,如果不开启这个选项,则不能长按删除,需要把对应的描述文件profiles删除,才能自动删除这个Web Clip。(如果在General中的Security配置了never且Web Clips中的Removable没有勾选,那么就真的不能删除了。。慎用)

Icon:图标,在桌面的Web Clip的图标。

Precomposed Icon:图标是否显示光阴,勾选则没有光影效果。

Full Screen:全屏显示,勾选后点击桌面的Web Clip后,全屏显示,然后跳转到相应的URL;不勾选的话先打开safari,然后跳转到URL。


完成配置后,点击右上角的Export导出描述文件:


此处也有一个Security选择:

None:不加密且适用于所有设备。

Sign configuration profile:加密且适用于所有设备。

Create and sign encrypted configuration profile for each selected device:加密且只能安装在制定的设备上。


下面图片是在安装时不加密和加密的区别:

       


还有一个比较大的区别是,未加密的描述文件我们可以把它当成一个模版,在项目当作任意拷贝然后修改一些基本信息后安装。而已加密的描述文件则不能随意修改内容。不然会提示“无法安装此描述文件”。下图是加密和未加密的描述文件内容,以及修改加密描述文件后安装的报错:










到此,我们已经成功的得到了一个描述文件,接下来就是编码了,把描述文件添加到项目中,把用到的第三方库添加到应用中,并且添加framework的以及配置,如图:


在Build Settings中添加搜索头文件的path:



接下来在AppDelegate中开启httpServer:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    [self startServer];    return YES;}

//开启httpServer服务- (void)startServer{    self.httpServer = [[RoutingHTTPServer alloc] init];    [self.httpServer setType:@"_http._tcp."];   //设置支持的协议    [self.httpServer setPort:12345];    //设置端口号    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];    [self.httpServer setDocumentRoot:documentsDirectory];   //设置root路径        if (self.httpServer.isRunning) [self.httpServer stop];        NSError *error;    if([self.httpServer start:&error])    {        NSLog(@"Started HTTP Server on port %hu", [_httpServer listeningPort]);    }    else    {        NSLog(@"Error starting HTTP Server: %@", error);        // Probably should add an escape - but in practice never loops more than twice (bug filed on GitHub https://github.com/robbiehanson/CocoaHTTPServer/issues/88)        [self startServer];    }}

在进入后台时向系统请求一些时间,以便继续运行httpSever服务,达到安装web clip的目的。

- (void)applicationDidEnterBackground:(UIApplication *)application {    backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{        [application endBackgroundTask:backgroundTask];        backgroundTask = UIBackgroundTaskInvalid;    }];}



我们在viewcontroller添加一个按钮,点击后,在桌面添加这个Web Clip:

- (IBAction)btnSaveTouchIn:(id)sender {        /*     phone_template和test1是没有使用签名的配置文件,可以随便修改里面的参数值     */        //模板位置    NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"testHadSign" ofType:@"mobileconfig"];    //目标位置    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"profile.mobileconfig"];    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {   //如果httpserver的root路径下不存在这个文件,则把文件拷贝过去        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];    }    BOOL success = [[NSFileManager defaultManager] copyItemAtPath:templatePath toPath:path error:nil];        __weak AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];    UInt16 port = appDelegate.httpServer.port;    if (success) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:%u/profile.mobileconfig", port]]];    } else {        NSLog(@"Error generating profile");    }}


  至此,已经完成了基本的编码,真机调试点击按钮后即可安装web clip。


如果我们在web clip想跳转到指定应用并传入参数,应该如何做呢?

例:

1/ 在配置Web Clips的URL时填入  demo://test?a=5&b=3

2/配置URL Types,如下图所示:

Identifier是你应用的Bundle identifier,URL Schemes就是“demo://test?a=5&b=3”前面的demo

3/ 3/ 在AppDelegate中截获从其他程序跳转到应用,

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation NS_AVAILABLE_IOS(4_2); 

这个方法返回一个bool值,根据这个bool值决定是否允许打开应用。

//其它程序打开聚米时,调用这个方法。通过返回值判断是否允许打开聚米- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{    if (url != nil && [url.scheme isEqualToString:@"demo"] && [url.host isEqualToString:@"test"]) {        NSString *query = url.query;    //得到参数  **  a=5&b=4&a=3        NSDictionary *dictparam = [self dictionaryFromQuery:query usingEncoding:NSUTF8StringEncoding];        NSLog(@"参数:%@",dictparam);        return YES;    } else {        NSLog(@"unknow host:%@",url.host);        return NO;    }}

//解析URL的参数值- (NSDictionary*)dictionaryFromQuery:(NSString*)query usingEncoding:(NSStringEncoding)encoding {    NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&;"];    NSMutableDictionary* pairs = [NSMutableDictionary dictionary];    NSScanner* scanner = [[NSScanner alloc] initWithString:query];    while (![scanner isAtEnd]) {        NSString* pairString = nil;        [scanner scanUpToCharactersFromSet:delimiterSet intoString:&pairString];        [scanner scanCharactersFromSet:delimiterSet intoString:NULL];        NSArray* kvPair = [pairString componentsSeparatedByString:@"="];        if (kvPair.count == 2) {            NSString* key = [[kvPair objectAtIndex:0]                             stringByReplacingPercentEscapesUsingEncoding:encoding];            NSString* value = [[kvPair objectAtIndex:1]                               stringByReplacingPercentEscapesUsingEncoding:encoding];            [pairs setObject:value forKey:key];        }    }        return [NSDictionary dictionaryWithDictionary:pairs];}

至此,所有工作完成,我们可以通过传入的参数来打开相应页面,这个就要根据具体需求来写了。


附上代码下载地址:http://download.csdn.net/detail/shengyumojian/8505917

0 0
原创粉丝点击