首页
社区
课程
招聘
[原创][已开源] 一步一步编写Safari插件xImage到发布
2013-5-27 10:35 17997

[原创][已开源] 一步一步编写Safari插件xImage到发布

2013-5-27 10:35
17997
源码地址:
https://github.com/noolua/xImage

需求设计阶段
自从有了iPad,晚上很少使用电脑了,使用safari浏览网页的时候,发现safari自带的reader模式做得很好,非常适合阅读浏览,为此产生一个能否将reader中的内容转为图片发送到微博,分享给好友或者粉丝的想法.



准备阶段
设备与环境
  参考帖子 http://bbs.pediy.com/showthread.php?t=166009

补充安装cycript(iOS设备下)
    cydia中安装
补充安装iOSOpenDev(mac设备下)
    iOSOpenDev https://iosopendev.googlecode.com/files/iOSOpenDev-1.5.pkg
    在安装iOSOpenDev过程中如果遇到问题,请在安装界面按cmd+L查看all,获得失败信息
    我这里遇到的是xcode 证书问题,在bash中处理一下就好
iosod sshkey -h [iOS_device_IP]

分析阶段
将ios中的safari拷贝到mac下
使用class-dump-z 导出class的定义
使用ida6.4分析mobilesafari
分析得出BrowserController TabDocument等class在这个插件中开发有用
进一步分析最终得出在ActionPanelActivityItemsSource的_customActivities方法下安装hook
即可满足设计需求

为了验证可行性,使用cycript进行可行性测试 (命令 cycript  -p 进程号)
代码如下
function snap_view()
{
var browser = [BrowserController sharedBrowserController];
var tab = [browser tabController];
var doc = [tab activeTabDocument];
var view = [doc frontView];
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
var img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
var data = UIImagePNGRepresentation(img);
var ok = [data writeToFile:@"/var/tmp/out.png" atomically:NO];
return ok;
}
snap_view();


开发阶段
打开XCode,创建CaptainHook Tweak项目


由于我手上的iOS是6.12所以为了保证可靠性,将control中的firmware修改为>6.1
删除control.txt文件

修改项目同名.plist,写入需要注入dylib的目标程序bundle,这里是com.apple.mobilesafari

在生成的项目同名.mm文件中编写如下代码

@class ActionPanelActivityItemsSource;
//@class AddBookmarkUIActivity;

CHDeclareClass(ActionPanelActivityItemsSource); // declare class

CHOptimizedMethod(0, self, id, ActionPanelActivityItemsSource, _customActivities) {
    NSArray *old_array = CHSuper(0, ActionPanelActivityItemsSource, _customActivities);
    NSMutableArray *array = [NSMutableArray arrayWithArray:old_array];
    id browser = objc_msgSend(objc_getClass("BrowserController"), NSSelectorFromString(@"sharedBrowserController"));
    if(browser){
        if(objc_msgSend(browser, NSSelectorFromString(@"isShowingReader"))){
            WeiboActivity *weibo = [[[WeiboActivity alloc] init] autorelease];
            [array insertObject:weibo atIndex:0];
        }
    }
    return array;
}

CHConstructor // code block that runs immediately upon load
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  
  // listen for local notification (not required; for example only)
  CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
  CFNotificationCenterAddObserver(center, NULL, WillEnterForeground, CFSTR("UIApplicationWillEnterForegroundNotification"), NULL, CFNotificationSuspensionBehaviorCoalesce);
  
  // listen for system-side notification (not required; for example only)
  // this would be posted using: notify_post("com.gs-studio.xImage.eventname");
  CFNotificationCenterRef darwin = CFNotificationCenterGetDarwinNotifyCenter();
  CFNotificationCenterAddObserver(darwin, NULL, ExternallyPostedNotification, CFSTR("com.gs-studio.xImage.eventname"), NULL, CFNotificationSuspensionBehaviorCoalesce);
  
  // CHLoadClass(ClassToHook); // load class (that is "available now")
  CHLoadLateClass(ActionPanelActivityItemsSource);  // load class (that will be "available later")
    CHHook(0, ActionPanelActivityItemsSource, _customActivities);
  [pool drain];
}



参考UIActivity
编写WeiboActivity
然后就是调试调试,开发开发的过程
由于整个代码量比较大,这里就不一一列出

当这个这个插件的使用人数超过5000,我将公布项目源码到github,所以请大家多多转发宣传一下,谢谢

发布阶段
浏览器打开http://thebigboss.org/hosting-repository-cydia/submission
因为是新发布,所以选择new app 进入http://thebigboss.org/hosting-repository-cydia/submit-your-app
然后开始填表,安装包名称,描述,作者,联系地址、、、balabala一大堆,填好后提交
安装包在你的项目目录下Package中,xxx.deb就是
然后邮箱就会收到邮件,表示你的app已经提交了
然后等待,应该在24小时内,不出什么问题,再次收到一封邮件,告知你的app发布到了thebigboss
打开cydia,搜索一下app的名称,查看一下(注意,如果你在iOS设备上调试过,请先删除后再索搜,不然搜索到的是自己本机的)

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 5
打赏
分享
最新回复 (4)
雪    币: 90
活跃值: (91)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
xouou 2013-5-27 11:00
2
0
支持一下技术贴,

好贴自然会被转,不用楼主讲这么多

另外, opera一出,神马都是浮云!
雪    币: 86
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
threewind 2013-9-10 14:38
3
0
好贴,学习,顺便希望能尽快开源学习一下
雪    币: 280
活跃值: (40)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
rockee 2 2013-9-30 09:28
4
0
已开源,地址在一楼
雪    币: 29
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
xilet 2013-9-30 20:57
5
0
收藏了先,以备日后学习用
游客
登录 | 注册 方可回帖
返回